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:
@@ -49,7 +49,20 @@ internal static class ZbSerilogConfig
|
||||
public static LoggerConfiguration Apply(
|
||||
LoggerConfiguration loggerConfiguration,
|
||||
ZbTelemetryOptions options,
|
||||
IServiceProvider? serviceProvider)
|
||||
IServiceProvider? serviceProvider) =>
|
||||
Apply(loggerConfiguration, options, serviceProvider, registeredTelemetry: null);
|
||||
|
||||
/// <summary>
|
||||
/// Full overload: <paramref name="registeredTelemetry"/> is the options an <c>AddZbTelemetry</c>
|
||||
/// call registered in the same container (or <see langword="null"/>). The OTLP log sink follows
|
||||
/// it when <paramref name="options"/> does not select OTLP itself — see
|
||||
/// <see cref="ResolveLogExport"/>. Identity enrichers always come from <paramref name="options"/>.
|
||||
/// </summary>
|
||||
public static LoggerConfiguration Apply(
|
||||
LoggerConfiguration loggerConfiguration,
|
||||
ZbTelemetryOptions options,
|
||||
IServiceProvider? serviceProvider,
|
||||
ZbTelemetryOptions? registeredTelemetry)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(loggerConfiguration);
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
@@ -75,7 +88,10 @@ internal static class ZbSerilogConfig
|
||||
ApplyRedaction(loggerConfiguration, serviceProvider);
|
||||
}
|
||||
|
||||
ApplyOpenTelemetryExport(loggerConfiguration, options);
|
||||
if (ResolveLogExport(options, registeredTelemetry) is { } export)
|
||||
{
|
||||
ApplyOpenTelemetryExport(loggerConfiguration, export);
|
||||
}
|
||||
|
||||
return loggerConfiguration;
|
||||
}
|
||||
@@ -90,6 +106,33 @@ internal static class ZbSerilogConfig
|
||||
internal static void ApplyRedaction(LoggerConfiguration loggerConfiguration, IServiceProvider serviceProvider) =>
|
||||
loggerConfiguration.Enrich.With(new RedactionEnricher(serviceProvider));
|
||||
|
||||
/// <summary>
|
||||
/// Chooses the options the OTLP log sink is built from, or <see langword="null"/> for no sink:
|
||||
/// <list type="number">
|
||||
/// <item><paramref name="serilogOptions"/> when it selects <see cref="ZbExporter.Otlp"/> itself
|
||||
/// (the pre-0.3.0 behaviour; an app that builds its logger outside the host container, such as
|
||||
/// ScadaBridge, passes the exporter here);</item>
|
||||
/// <item>else the <c>AddZbTelemetry</c> registration in the same container when it selects
|
||||
/// OTLP — so <c><App>:Telemetry:Exporter=Otlp</c> switches logs along with metrics and
|
||||
/// traces, with the identical Resource (scadaproj#55);</item>
|
||||
/// <item>else none.</item>
|
||||
/// </list>
|
||||
/// Whichever is chosen, its <see cref="ZbTelemetryOptions.ExportLogs"/> = <see langword="false"/>
|
||||
/// keeps logs local.
|
||||
/// </summary>
|
||||
internal static ZbTelemetryOptions? ResolveLogExport(
|
||||
ZbTelemetryOptions serilogOptions,
|
||||
ZbTelemetryOptions? registeredTelemetry)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(serilogOptions);
|
||||
|
||||
var source = serilogOptions.Exporter == ZbExporter.Otlp
|
||||
? serilogOptions
|
||||
: registeredTelemetry is { Exporter: ZbExporter.Otlp } ? registeredTelemetry : null;
|
||||
|
||||
return source is { ExportLogs: true } ? source : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <c>WriteTo.OpenTelemetry</c> log sink when an OTLP exporter is explicitly
|
||||
/// selected (<see cref="ZbTelemetryOptions.Exporter"/> = <see cref="ZbExporter.Otlp"/>).
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
@@ -14,6 +15,14 @@ namespace ZB.MOM.WW.Telemetry.Serilog;
|
||||
/// <c>AddZbTelemetry</c> in the core package for that.
|
||||
///
|
||||
/// <para>
|
||||
/// <b>OTLP logs (0.3.0+).</b> An OTLP log sink is added when the options passed here select
|
||||
/// <c>Exporter = Otlp</c>, or otherwise when an <c>AddZbTelemetry</c> call in the same service
|
||||
/// collection does — the sink then uses that registration's endpoint and Resource, so logs go where
|
||||
/// metrics and traces go. <c>ZbTelemetryOptions.ExportLogs = false</c> on the chosen options keeps
|
||||
/// logs local. Levels and the configuration-driven sinks are unaffected.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This method intentionally does <strong>not</strong> set the process-global
|
||||
/// <see cref="Log.Logger"/> (via <c>CreateBootstrapLogger</c> or otherwise). Mutating
|
||||
/// process-global state in a shared library causes "logger is already frozen" exceptions
|
||||
@@ -119,7 +128,11 @@ public static class ZbSerilogExtensions
|
||||
// enrichers in registration order, so a RedactionEnricher registered before
|
||||
// ConfigureLogger would let hook-added enrichers stamp properties that reach every
|
||||
// sink unredacted. Apply() is told there is no provider so it skips redaction here.
|
||||
ZbSerilogConfig.Apply(loggerConfiguration, options, serviceProvider: null);
|
||||
// The OTLP log sink follows the AddZbTelemetry registration in this container when
|
||||
// these options do not select OTLP themselves (scadaproj#55). Resolved here, inside
|
||||
// the AddSerilog callback, so the AddZbTelemetry/AddZbSerilog call order is irrelevant.
|
||||
var registered = serviceProvider.GetService<ZbTelemetryRegistration>()?.Options;
|
||||
ZbSerilogConfig.Apply(loggerConfiguration, options, serviceProvider: null, registered);
|
||||
|
||||
serilogOptions.ConfigureLogger?.Invoke(loggerConfiguration, serviceProvider);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user