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,199 @@
using System.Collections.Concurrent;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using ZB.MOM.WW.Telemetry;
using ZB.MOM.WW.Telemetry.Serilog;
namespace ZB.MOM.WW.Telemetry.Serilog.Tests;
/// <summary>
/// scadaproj#55: logs follow <c>AddZbTelemetry</c>'s OTLP exporter. Before 0.3.0 the sink keyed only
/// on the options passed to <c>AddZbSerilog</c>, which every app filled with the service name alone,
/// so no app ever exported a log record.
/// </summary>
public sealed class OtlpLogExportTests
{
private const string LogsPath = "/opentelemetry.proto.collector.logs.v1.LogsService/Export";
private const string MetricsPath = "/opentelemetry.proto.collector.metrics.v1.MetricsService/Export";
// --- the decision ---------------------------------------------------------------------
private static ZbTelemetryOptions Otlp(string name, bool exportLogs = true) => new()
{
ServiceName = name,
Exporter = ZbExporter.Otlp,
OtlpEndpoint = "http://collector:4317",
ExportLogs = exportLogs,
};
[Fact]
public void Serilog_options_selecting_Otlp_win()
{
var own = Otlp("own");
Assert.Same(own, ZbSerilogConfig.ResolveLogExport(own, Otlp("registered")));
}
[Fact]
public void Registered_telemetry_options_are_followed_when_Serilog_options_do_not_select_Otlp()
{
var registered = Otlp("registered");
Assert.Same(registered, ZbSerilogConfig.ResolveLogExport(new ZbTelemetryOptions { ServiceName = "own" }, registered));
}
[Fact]
public void No_sink_when_nothing_selects_Otlp()
{
Assert.Null(ZbSerilogConfig.ResolveLogExport(new ZbTelemetryOptions { ServiceName = "own" }, null));
Assert.Null(ZbSerilogConfig.ResolveLogExport(
new ZbTelemetryOptions { ServiceName = "own" },
new ZbTelemetryOptions { ServiceName = "registered", OtlpEndpoint = "http://collector:4317" }));
}
[Fact]
public void ExportLogs_false_keeps_logs_local_on_either_path()
{
Assert.Null(ZbSerilogConfig.ResolveLogExport(new ZbTelemetryOptions { ServiceName = "own" }, Otlp("registered", exportLogs: false)));
Assert.Null(ZbSerilogConfig.ResolveLogExport(Otlp("own", exportLogs: false), null));
}
// --- end to end against a loopback OTLP gRPC receiver -----------------------------------
/// <summary>Minimal OTLP/gRPC receiver: records each export call's path and body, answers OK.</summary>
private sealed class FakeOtlpReceiver : IAsyncDisposable
{
private readonly WebApplication _app;
public ConcurrentBag<(string Path, byte[] Body)> Calls { get; } = new();
public string Endpoint { get; }
private FakeOtlpReceiver(WebApplication app, string endpoint)
{
_app = app;
Endpoint = endpoint;
}
public static async Task<FakeOtlpReceiver> StartAsync()
{
var builder = WebApplication.CreateSlimBuilder();
builder.Logging.ClearProviders();
builder.WebHost.ConfigureKestrel(k => k.Listen(System.Net.IPAddress.Loopback, 0,
l => l.Protocols = HttpProtocols.Http2));
var app = builder.Build();
FakeOtlpReceiver? receiver = null;
app.Run(async context =>
{
using var buffer = new MemoryStream();
await context.Request.Body.CopyToAsync(buffer);
receiver!.Calls.Add((context.Request.Path.Value ?? "", buffer.ToArray()));
context.Response.ContentType = "application/grpc";
context.Response.AppendTrailer("grpc-status", "0");
// One empty, uncompressed gRPC message (the Export*ServiceResponse).
await context.Response.Body.WriteAsync(new byte[5]);
});
await app.StartAsync();
var address = app.Urls.Single().Replace("[::1]", "127.0.0.1");
receiver = new FakeOtlpReceiver(app, address);
return receiver;
}
public bool Received(string path) => Calls.Any(c => c.Path == path);
public string BodiesOf(string path) =>
string.Join("\n", Calls.Where(c => c.Path == path).Select(c => Encoding.UTF8.GetString(c.Body)));
public async ValueTask DisposeAsync()
{
await _app.StopAsync();
await _app.DisposeAsync();
}
}
/// <summary>
/// The app shape every family app uses: <c>AddZbSerilog</c> with the service name only, and the
/// exporter set on <c>AddZbTelemetry</c>. Disposing the host flushes the log batch and the
/// final metric export.
/// </summary>
private static async Task RunAppAsync(FakeOtlpReceiver receiver, ZbExporter exporter, bool exportLogs)
{
var builder = Host.CreateApplicationBuilder();
builder.AddZbSerilog(o => o.ServiceName = "log-export-test");
builder.Services.AddZbTelemetry(o =>
{
o.ServiceName = "log-export-test";
o.ServiceVersion = "9.8.7+e2e";
o.SiteId = "site-e2e";
o.NodeRole = "role-e2e";
o.ResourceAttributes["zb.test.marker"] = "marker-e2e";
o.Exporter = exporter;
o.OtlpEndpoint = receiver.Endpoint;
o.ExportLogs = exportLogs;
});
using (var host = builder.Build())
{
// Resolve the meter provider so a metrics export can serve as the positive control.
_ = host.Services.GetService<OpenTelemetry.Metrics.MeterProvider>();
host.Services.GetRequiredService<global::Serilog.ILogger>().Information("hello over otlp {Marker}", "log-line-e2e");
}
// The sink's batch and the metric reader flush on dispose; allow the h2c round-trips to land.
for (var i = 0; i < 50 && !receiver.Received(exporter == ZbExporter.Otlp ? MetricsPath : LogsPath); i++)
{
await Task.Delay(100);
}
}
[Fact]
public async Task Logs_follow_AddZbTelemetry_Otlp_with_the_same_Resource()
{
await using var receiver = await FakeOtlpReceiver.StartAsync();
await RunAppAsync(receiver, ZbExporter.Otlp, exportLogs: true);
for (var i = 0; i < 50 && !receiver.Received(LogsPath); i++)
{
await Task.Delay(100);
}
Assert.True(receiver.Received(LogsPath), "no OTLP logs export call arrived; paths: " +
string.Join(", ", receiver.Calls.Select(c => c.Path).Distinct()));
var body = receiver.BodiesOf(LogsPath);
Assert.Contains("log-line-e2e", body);
Assert.Contains("log-export-test", body);
Assert.Contains("9.8.7+e2e", body);
Assert.Contains("site-e2e", body);
Assert.Contains("role-e2e", body);
Assert.Contains("marker-e2e", body);
}
[Fact]
public async Task ExportLogs_false_sends_metrics_but_no_logs()
{
await using var receiver = await FakeOtlpReceiver.StartAsync();
await RunAppAsync(receiver, ZbExporter.Otlp, exportLogs: false);
await Task.Delay(500);
// Positive control: the same receiver, reachable, got the metrics export.
Assert.True(receiver.Received(MetricsPath), "positive control failed: no metrics export arrived");
Assert.False(receiver.Received(LogsPath));
}
[Fact]
public async Task Prometheus_exporter_sends_no_logs()
{
await using var receiver = await FakeOtlpReceiver.StartAsync();
await RunAppAsync(receiver, ZbExporter.Prometheus, exportLogs: true);
await Task.Delay(500);
Assert.Empty(receiver.Calls);
}
}
@@ -16,6 +16,11 @@
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<!-- The OTLP log-export tests host a loopback h2c receiver on Kestrel. -->
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ZB.MOM.WW.Telemetry.Serilog\ZB.MOM.WW.Telemetry.Serilog.csproj" />
</ItemGroup>
@@ -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);
}
}
@@ -36,7 +36,9 @@ public sealed class ZbResourceTests
var options = new ZbTelemetryOptions
{
ServiceName = "mxgateway",
// ServiceVersion / SiteId / NodeRole left null
// "" = omit service.version (null now means the entry-assembly default, scadaproj#55);
// SiteId / NodeRole left null.
ServiceVersion = "",
};
var resource = ZbResource.Build(options).Build();