feat(telemetry): 0.3.0 service.version default, ResourceAttributes, logs follow AddZbTelemetry OTLP (#55)

- service.version: null ServiceVersion now defaults to the entry assembly's
  AssemblyInformationalVersion (the family apps stamp the git SHA there); "" omits it.
- ResourceAttributes: extra Resource attributes on every signal; first-class keys
  (service.*, host.name, site.id, node.role) are reserved and rejected at startup.
- Logs over OTLP: AddZbTelemetry publishes its options in DI and AddZbSerilog's OTLP
  log sink follows them when its own options do not select Otlp, so
  <App>:Telemetry:Exporter=Otlp now switches logs along with metrics and traces, with
  the same endpoint and Resource. ExportLogs=false opts out.
- Ships the unpublished 0.2.1 review fixes, including the stricter OTLP endpoint check
  (scheme-less host:port rejected when Exporter=Otlp); no app config sets an endpoint.
- Tests 42 -> 74, incl. a loopback OTLP/gRPC receiver proving logs arrive with the
  Resource, and an ExportLogs=false case with a metrics positive control.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-12 20:58:59 -05:00
parent 616e913f3e
commit cfc7168ace
14 changed files with 639 additions and 34 deletions
@@ -0,0 +1,179 @@
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using ZB.MOM.WW.Telemetry;
namespace ZB.MOM.WW.Telemetry.Tests;
/// <summary>
/// scadaproj#55: <c>service.version</c> defaults from the entry assembly, and caller-supplied
/// Resource attributes reach the Resource the SDK pipeline actually uses.
/// </summary>
public sealed class ResourceIdentityTests
{
private static Dictionary<string, object> PipelineResource(Action<ZbTelemetryOptions> configure)
{
var builder = WebApplication.CreateBuilder();
builder.AddZbTelemetry(configure);
using var app = builder.Build();
var tracer = app.Services.GetRequiredService<TracerProvider>();
var meter = app.Services.GetRequiredService<MeterProvider>();
var traceAttrs = tracer.GetResource().Attributes.ToDictionary(a => a.Key, a => a.Value);
var meterAttrs = meter.GetResource().Attributes.ToDictionary(a => a.Key, a => a.Value);
// Metrics and traces carry the identical Resource.
Assert.Equal(traceAttrs.OrderBy(a => a.Key), meterAttrs.OrderBy(a => a.Key));
return traceAttrs;
}
[Fact]
public void VersionOf_reads_the_informational_version()
{
var assembly = typeof(ZbResource).Assembly;
var expected = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion;
Assert.Equal(expected, ZbResource.VersionOf(assembly));
Assert.StartsWith("0.3.0", ZbResource.VersionOf(assembly));
Assert.Null(ZbResource.VersionOf(null));
}
[Fact]
public void DefaultServiceVersion_is_the_entry_assembly_version()
{
Assert.NotNull(Assembly.GetEntryAssembly());
Assert.Equal(ZbResource.VersionOf(Assembly.GetEntryAssembly()), ZbResource.DefaultServiceVersion);
Assert.False(string.IsNullOrEmpty(ZbResource.DefaultServiceVersion));
}
[Fact]
public void Null_ServiceVersion_takes_the_default()
{
var attributes = ZbResource.BuildAttributes(new ZbTelemetryOptions { ServiceName = "x" }, "1.0.0+abc123");
Assert.Equal("1.0.0+abc123", attributes["service.version"]);
}
[Fact]
public void Explicit_ServiceVersion_overrides_the_default()
{
var attributes = ZbResource.BuildAttributes(
new ZbTelemetryOptions { ServiceName = "x", ServiceVersion = "2.0.0" }, "1.0.0+abc123");
Assert.Equal("2.0.0", attributes["service.version"]);
}
[Fact]
public void Empty_ServiceVersion_omits_the_attribute_even_with_a_default()
{
var attributes = ZbResource.BuildAttributes(
new ZbTelemetryOptions { ServiceName = "x", ServiceVersion = "" }, "1.0.0+abc123");
Assert.False(attributes.ContainsKey("service.version"));
}
[Fact]
public void No_default_and_null_ServiceVersion_omits_the_attribute()
{
var attributes = ZbResource.BuildAttributes(new ZbTelemetryOptions { ServiceName = "x" }, defaultVersion: null);
Assert.False(attributes.ContainsKey("service.version"));
}
[Fact]
public void Pipeline_Resource_carries_the_default_service_version()
{
var attributes = PipelineResource(o => o.ServiceName = "telemetry-test");
Assert.Equal(ZbResource.DefaultServiceVersion, attributes["service.version"]);
}
[Fact]
public void Pipeline_Resource_carries_identity_and_extra_attributes()
{
var attributes = PipelineResource(o =>
{
o.ServiceName = "telemetry-test";
o.ServiceVersion = "7.7.7";
o.SiteId = "site-a";
o.NodeRole = "driver";
o.ResourceAttributes["otopcua.cluster.id"] = "c1";
o.ResourceAttributes["zb.node.ordinal"] = 2L;
});
Assert.Equal("7.7.7", attributes["service.version"]);
Assert.Equal("site-a", attributes["site.id"]);
Assert.Equal("driver", attributes["node.role"]);
Assert.Equal("c1", attributes["otopcua.cluster.id"]);
Assert.Equal(2L, attributes["zb.node.ordinal"]);
}
[Fact]
public void Extra_attributes_are_in_BuildAttributes()
{
var options = new ZbTelemetryOptions { ServiceName = "x" };
options.ResourceAttributes["deployment.environment.name"] = "dev";
Assert.Equal("dev", ZbResource.BuildAttributes(options)["deployment.environment.name"]);
}
[Theory]
[InlineData("service.name")]
[InlineData("service.namespace")]
[InlineData("service.version")]
[InlineData("service.instance.id")]
[InlineData("host.name")]
[InlineData("site.id")]
[InlineData("node.role")]
public void Reserved_keys_in_ResourceAttributes_are_rejected(string key)
{
var builder = WebApplication.CreateBuilder();
var ex = Assert.Throws<ArgumentException>(() => builder.AddZbTelemetry(o =>
{
o.ServiceName = "telemetry-test";
o.ResourceAttributes[key] = "spoof";
}));
Assert.Equal("configure", ex.ParamName);
Assert.Contains(key, ex.Message);
}
[Fact]
public void Null_value_in_ResourceAttributes_is_rejected_on_the_prebuilt_overload_too()
{
var options = new ZbTelemetryOptions { ServiceName = "telemetry-test" };
options.ResourceAttributes["custom"] = null!;
var ex = Assert.Throws<ArgumentException>(() => new ServiceCollection().AddZbTelemetry(options));
Assert.Contains("custom", ex.Message);
}
[Fact]
public void Blank_key_in_ResourceAttributes_is_rejected()
{
var builder = WebApplication.CreateBuilder();
Assert.Throws<ArgumentException>(() => builder.AddZbTelemetry(o =>
{
o.ServiceName = "telemetry-test";
o.ResourceAttributes[" "] = "v";
}));
}
[Fact]
public void AddZbTelemetry_publishes_its_options_for_the_Serilog_companion()
{
var services = new ServiceCollection();
services.AddZbTelemetry(o =>
{
o.ServiceName = "telemetry-test";
o.Exporter = ZbExporter.Otlp;
o.OtlpEndpoint = "http://collector:4317";
});
using var provider = services.BuildServiceProvider();
var registration = provider.GetRequiredService<ZbTelemetryRegistration>();
Assert.Equal("telemetry-test", registration.Options.ServiceName);
Assert.Equal("http://collector:4317", registration.Options.OtlpEndpoint);
}
}