fix(telemetry): identical resource across all 3 signals (symmetric OTLP trigger + deterministic service.instance.id)

Fix 1 — symmetric OTLP trigger: ZbSerilogConfig.ApplyOpenTelemetryExport now activates only
when options.Exporter == ZbExporter.Otlp, matching the core OTel metrics/traces path. The
previous fallback that also triggered on a bare OtlpEndpoint is removed; OtlpEndpoint is the
address to use when Otlp is selected, not an independent enable.

Fix 2 — deterministic service.instance.id: ZbResource.InstanceId (MachineName:ProcessId) is
a new public property that produces a stable, process-unique id without a random GUID.
ZbResource.Configure passes autoGenerateServiceInstanceId:false + serviceInstanceId:InstanceId
so metrics and traces never get a random auto-generated id. ZbSerilogConfig.BuildResourceAttributes
adds service.instance.id from ZbResource.InstanceId so the Serilog OTLP log sink carries the
exact same value — all three signals now share an identical resource for cross-signal joins.

Tests: +2 in ZbResourceTests (InstanceId determinism, no-GUID check), +2 in RedactionTests
(service.instance.id parity assertion in BuildResourceAttributes, symmetric OTLP trigger tests).
Total: 9 + 14 = 23 tests, all green.
This commit is contained in:
Joseph Doherty
2026-06-01 08:26:09 -04:00
parent 8311912f40
commit 88c557dee8
4 changed files with 123 additions and 15 deletions
@@ -81,20 +81,21 @@ internal static class ZbSerilogConfig
}
/// <summary>
/// Adds a <c>WriteTo.OpenTelemetry</c> log sink when an OTLP exporter is configured
/// (<see cref="ZbTelemetryOptions.Exporter"/> = <see cref="ZbExporter.Otlp"/> or
/// <see cref="ZbTelemetryOptions.OtlpEndpoint"/> set). The sink carries the same Resource
/// attributes as <c>ZbResource</c> (<c>service.name</c>/<c>service.namespace</c>/
/// <c>service.version</c>/<c>site.id</c>/<c>node.role</c>/<c>host.name</c>) so logs correlate
/// with metrics and traces in the backend.
/// Adds a <c>WriteTo.OpenTelemetry</c> log sink when an OTLP exporter is explicitly
/// selected (<see cref="ZbTelemetryOptions.Exporter"/> = <see cref="ZbExporter.Otlp"/>).
/// <see cref="ZbTelemetryOptions.OtlpEndpoint"/> is the address used when OTLP is selected
/// — it is NOT an independent enable. This matches the core OTel path behaviour so that
/// an endpoint-only config (without <c>Exporter=Otlp</c>) exports nothing to OTLP on any
/// signal. The sink carries the same Resource attributes as <c>ZbResource</c>
/// (<c>service.name</c>/<c>service.namespace</c>/<c>service.version</c>/
/// <c>service.instance.id</c>/<c>site.id</c>/<c>node.role</c>/<c>host.name</c>) so logs
/// correlate with metrics and traces in the backend.
/// </summary>
private static void ApplyOpenTelemetryExport(
LoggerConfiguration loggerConfiguration,
ZbTelemetryOptions options)
{
var otlpRequested = options.Exporter == ZbExporter.Otlp
|| !string.IsNullOrEmpty(options.OtlpEndpoint);
if (!otlpRequested)
if (options.Exporter != ZbExporter.Otlp)
{
return;
}
@@ -115,8 +116,11 @@ internal static class ZbSerilogConfig
/// <summary>
/// Builds the OTLP Resource-attribute map mirroring <c>ZbResource</c>. Null/empty optional
/// attributes are omitted, matching the shared Resource's omission rules. Internal so it can
/// be asserted by the test assembly without being part of the public NuGet API.
/// attributes are omitted, matching the shared Resource's omission rules. The
/// <c>service.instance.id</c> is sourced from <see cref="ZbResource.InstanceId"/> — the
/// same deterministic <c>MachineName:ProcessId</c> value used by the OTel SDK path — so
/// all three signals carry an identical instance identifier. Internal so it can be asserted
/// by the test assembly without being part of the public NuGet API.
/// </summary>
internal static IDictionary<string, object> BuildResourceAttributes(ZbTelemetryOptions options)
{
@@ -124,6 +128,7 @@ internal static class ZbSerilogConfig
{
["service.name"] = options.ServiceName,
["service.namespace"] = options.ServiceNamespace,
["service.instance.id"] = ZbResource.InstanceId,
["host.name"] = Environment.MachineName,
};
@@ -10,11 +10,21 @@ namespace ZB.MOM.WW.Telemetry;
/// </summary>
public static class ZbResource
{
/// <summary>
/// Deterministic, process-stable service instance identifier. Formatted as
/// <c>MachineName:ProcessId</c> so that every signal (metrics, traces, logs) from the same
/// process carries the exact same <c>service.instance.id</c>, enabling cross-signal
/// correlation without a random GUID that changes on each startup.
/// </summary>
public static string InstanceId =>
$"{System.Environment.MachineName}:{System.Environment.ProcessId}";
/// <summary>
/// Returns a <see cref="ResourceBuilder"/> pre-populated with <c>service.name</c>,
/// <c>service.namespace</c>, <c>service.version</c>, <c>site.id</c>, <c>node.role</c>, and
/// <c>host.name</c> (always <see cref="System.Environment.MachineName"/>). Attributes with
/// null values are omitted from the Resource.
/// <c>service.namespace</c>, <c>service.version</c>, <c>service.instance.id</c>,
/// <c>site.id</c>, <c>node.role</c>, and <c>host.name</c> (always
/// <see cref="System.Environment.MachineName"/>). Attributes with null values are omitted
/// from the Resource.
/// </summary>
/// <param name="options">The telemetry options describing the service identity.</param>
public static ResourceBuilder Build(ZbTelemetryOptions options) =>
@@ -30,7 +40,9 @@ public static class ZbResource
builder.AddService(
serviceName: options.ServiceName,
serviceNamespace: options.ServiceNamespace,
serviceVersion: options.ServiceVersion);
serviceVersion: options.ServiceVersion,
autoGenerateServiceInstanceId: false,
serviceInstanceId: InstanceId);
var attributes = new List<KeyValuePair<string, object>>
{