feat(telemetry.serilog): ILogRedactor seam + OTel log export
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Serilog;
|
||||
using Serilog.Configuration;
|
||||
using Serilog.Sinks.OpenTelemetry;
|
||||
using ZB.MOM.WW.Telemetry;
|
||||
|
||||
namespace ZB.MOM.WW.Telemetry.Serilog;
|
||||
@@ -66,6 +68,77 @@ public static class ZbSerilogConfig
|
||||
|
||||
enrich.With(new TraceContextEnricher());
|
||||
|
||||
if (serviceProvider is not null)
|
||||
{
|
||||
enrich.With(new RedactionEnricher(serviceProvider));
|
||||
}
|
||||
|
||||
ApplyOpenTelemetryExport(loggerConfiguration, options);
|
||||
|
||||
return loggerConfiguration;
|
||||
}
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
private static void ApplyOpenTelemetryExport(
|
||||
LoggerConfiguration loggerConfiguration,
|
||||
ZbTelemetryOptions options)
|
||||
{
|
||||
var otlpRequested = options.Exporter == ZbExporter.Otlp
|
||||
|| !string.IsNullOrEmpty(options.OtlpEndpoint);
|
||||
if (!otlpRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var resourceAttributes = BuildResourceAttributes(options);
|
||||
|
||||
loggerConfiguration.WriteTo.OpenTelemetry(sink =>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(options.OtlpEndpoint))
|
||||
{
|
||||
sink.Endpoint = options.OtlpEndpoint;
|
||||
}
|
||||
|
||||
sink.Protocol = OtlpProtocol.Grpc;
|
||||
sink.ResourceAttributes = resourceAttributes;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the OTLP Resource-attribute map mirroring <c>ZbResource</c>. Null/empty optional
|
||||
/// attributes are omitted, matching the shared Resource's omission rules.
|
||||
/// </summary>
|
||||
private static IDictionary<string, object> BuildResourceAttributes(ZbTelemetryOptions options)
|
||||
{
|
||||
var attributes = new Dictionary<string, object>
|
||||
{
|
||||
["service.name"] = options.ServiceName,
|
||||
["service.namespace"] = options.ServiceNamespace,
|
||||
["host.name"] = Environment.MachineName,
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(options.ServiceVersion))
|
||||
{
|
||||
attributes["service.version"] = options.ServiceVersion;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(options.SiteId))
|
||||
{
|
||||
attributes["site.id"] = options.SiteId;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(options.NodeRole))
|
||||
{
|
||||
attributes["node.role"] = options.NodeRole;
|
||||
}
|
||||
|
||||
return attributes;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user