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:
@@ -5,7 +5,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Version>0.2.1</Version>
|
||||
<Version>0.3.0</Version>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ follow-on (tracked in `components/observability/GAPS.md`).
|
||||
|---|---|
|
||||
| **Metrics** | App Meters (via `options.Meters[]`) + standard: ASP.NET Core, HttpClient, .NET runtime, process. Exported via Prometheus (always on) with OTLP as an additive overlay. |
|
||||
| **Traces** | App ActivitySources (via `options.ActivitySources[]`) + standard: ASP.NET Core, HttpClient, gRPC client. Exported via OTLP when `Exporter = ZbExporter.Otlp`. |
|
||||
| **Logs** | Wired by `AddZbSerilog` (companion call). Serilog is used as the log sink; logs are bridged to OpenTelemetry via `Serilog.Sinks.OpenTelemetry` when configured. |
|
||||
| **Logs** | Wired by `AddZbSerilog` (companion call). Serilog is used as the log sink; logs are bridged to OpenTelemetry via `Serilog.Sinks.OpenTelemetry` when OTLP is selected. **0.3.0+:** the OTLP log sink follows the `AddZbTelemetry` registration in the same container, so `Exporter = Otlp` on `AddZbTelemetry` alone sends logs to the same endpoint with the same Resource (`ExportLogs = false` opts out). Options passed to `AddZbSerilog` that select OTLP themselves still win (for a logger built outside the host container, e.g. ScadaBridge). |
|
||||
|
||||
Trace↔log correlation is automatic: `TraceContextEnricher` reads `Activity.Current` for each
|
||||
log event and attaches `trace_id` and `span_id`, so log events produced inside a traced request
|
||||
@@ -71,6 +71,18 @@ intact (not reallocated). See the `ILogRedactor` XML doc for the full contract.
|
||||
|
||||
---
|
||||
|
||||
## Resource identity (0.3.0+)
|
||||
|
||||
| Attribute | Source |
|
||||
|---|---|
|
||||
| `service.name` / `service.namespace` | `ServiceName` (required) / `ServiceNamespace` (default `ZB.MOM.WW`) |
|
||||
| `service.version` | `ServiceVersion`; **`null` (default) = the entry assembly's `AssemblyInformationalVersion`** (the family apps stamp the git SHA there), `""` = omit, any other value = override |
|
||||
| `site.id` / `node.role` | `SiteId` / `NodeRole` (omitted when null/empty) |
|
||||
| `service.instance.id` / `host.name` | computed: `MachineName:ProcessId` / `MachineName` |
|
||||
| anything else | `ResourceAttributes` (e.g. `["otopcua.cluster.id"] = "c1"`); the keys above are reserved and rejected at startup |
|
||||
|
||||
The same attribute set goes to metrics, traces and the OTLP log sink.
|
||||
|
||||
## Exporter options
|
||||
|
||||
Prometheus is **always wired** for metrics regardless of the `Exporter` setting. OTLP is an
|
||||
@@ -144,9 +156,11 @@ backend):
|
||||
|
||||
| Assembly | Tests |
|
||||
|---|---|
|
||||
| `ZB.MOM.WW.Telemetry.Tests` | 12 |
|
||||
| `ZB.MOM.WW.Telemetry.Serilog.Tests` | 30 |
|
||||
| **Total** | **42** |
|
||||
| `ZB.MOM.WW.Telemetry.Tests` | 35 |
|
||||
| `ZB.MOM.WW.Telemetry.Serilog.Tests` | 39 |
|
||||
| **Total** | **74** |
|
||||
|
||||
The log-export tests start a loopback OTLP/gRPC receiver on Kestrel (no external collector).
|
||||
|
||||
---
|
||||
|
||||
@@ -159,8 +173,8 @@ dotnet pack ZB.MOM.WW.Telemetry.slnx -c Release -o ./artifacts
|
||||
Produces two `.nupkg` files in `artifacts/`:
|
||||
|
||||
```
|
||||
ZB.MOM.WW.Telemetry.0.2.0.nupkg
|
||||
ZB.MOM.WW.Telemetry.Serilog.0.2.0.nupkg
|
||||
ZB.MOM.WW.Telemetry.0.3.0.nupkg
|
||||
ZB.MOM.WW.Telemetry.Serilog.0.3.0.nupkg
|
||||
```
|
||||
|
||||
`GeneratePackageOnBuild` is off — pack explicitly as above. Both packages are versioned
|
||||
@@ -170,12 +184,11 @@ lockstep from `Directory.Build.props`.
|
||||
|
||||
## Status
|
||||
|
||||
**`0.1.0` is on the Gitea feed and adopted by all four apps** (OtOpcUa, MxAccessGateway, ScadaBridge,
|
||||
HistorianGateway): each pins `ZB.MOM.WW.Telemetry` + `ZB.MOM.WW.Telemetry.Serilog` `0.1.0` and calls
|
||||
`AddZbTelemetry`; all but ScadaBridge call `AddZbSerilog` (ScadaBridge keeps its own
|
||||
`LoggerConfigurationFactory` and adds only `TraceContextEnricher`). Verified against each repo's origin default
|
||||
branch 2026-11-09. **`0.2.0`** (the `AddZbSerilog` minimum-level governance hook, scadaproj#12) is built here but
|
||||
**not published**. Adoption history is in the component backlog:
|
||||
**`0.3.0`** (scadaproj#55) is the current release on the Gitea feed: `service.version` defaults from the
|
||||
entry assembly, `ResourceAttributes` for extra Resource attributes, logs follow `AddZbTelemetry`'s OTLP
|
||||
exporter, and the 0.2.1 review fixes (including the stricter OTLP endpoint check: a scheme-less
|
||||
`host:port` now fails at startup when `Exporter = Otlp`). Feed versions: `0.1.0`, `0.2.0`, `0.3.0`
|
||||
(`0.2.1` was never published). Consumer pins: see `CLAUDE.md`. Adoption history is in the component backlog:
|
||||
|
||||
- `~/Desktop/scadaproj/components/observability/GAPS.md`
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>ZB.MOM.WW.Telemetry.Serilog.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>ZB.MOM.WW.Telemetry.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using OpenTelemetry.Resources;
|
||||
|
||||
namespace ZB.MOM.WW.Telemetry;
|
||||
@@ -19,12 +20,70 @@ public static class ZbResource
|
||||
public static string InstanceId =>
|
||||
$"{System.Environment.MachineName}:{System.Environment.ProcessId}";
|
||||
|
||||
/// <summary>
|
||||
/// Resource keys owned by first-class <see cref="ZbTelemetryOptions"/> members (or computed by
|
||||
/// the library). <see cref="ZbTelemetryOptions.ResourceAttributes"/> may not set them.
|
||||
/// </summary>
|
||||
public static IReadOnlySet<string> ReservedAttributeKeys { get; } = new HashSet<string>(StringComparer.Ordinal)
|
||||
{
|
||||
"service.name",
|
||||
"service.namespace",
|
||||
"service.version",
|
||||
"service.instance.id",
|
||||
"host.name",
|
||||
"site.id",
|
||||
"node.role",
|
||||
};
|
||||
|
||||
private static readonly Lazy<string?> EntryAssemblyVersion =
|
||||
new(() => VersionOf(Assembly.GetEntryAssembly()));
|
||||
|
||||
/// <summary>
|
||||
/// The <c>service.version</c> used when <see cref="ZbTelemetryOptions.ServiceVersion"/> is
|
||||
/// <see langword="null"/>: the entry assembly's <c>AssemblyInformationalVersion</c> (the family
|
||||
/// apps stamp the git SHA there), else its assembly version, else <see langword="null"/>
|
||||
/// (attribute omitted). Computed once per process.
|
||||
/// </summary>
|
||||
public static string? DefaultServiceVersion => EntryAssemblyVersion.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Version string of <paramref name="assembly"/>: its <c>AssemblyInformationalVersion</c> when
|
||||
/// present and non-blank, else its assembly version, else <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <param name="assembly">The assembly to read, typically the entry assembly.</param>
|
||||
internal static string? VersionOf(Assembly? assembly)
|
||||
{
|
||||
if (assembly is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var informational = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
|
||||
if (!string.IsNullOrWhiteSpace(informational))
|
||||
{
|
||||
return informational;
|
||||
}
|
||||
|
||||
return assembly.GetName().Version?.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The effective <c>service.version</c> for <paramref name="options"/>: the explicit value when
|
||||
/// set (<c>""</c> means omit, returned as <see langword="null"/>), else
|
||||
/// <paramref name="defaultVersion"/>.
|
||||
/// </summary>
|
||||
internal static string? ResolveServiceVersion(ZbTelemetryOptions options, string? defaultVersion) =>
|
||||
options.ServiceVersion is null
|
||||
? defaultVersion
|
||||
: options.ServiceVersion.Length == 0 ? null : options.ServiceVersion;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ResourceBuilder"/> pre-populated with <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>, and <c>host.name</c> (always
|
||||
/// <see cref="System.Environment.MachineName"/>). Attributes with null values are omitted
|
||||
/// from the Resource.
|
||||
/// <c>site.id</c>, <c>node.role</c>, <c>host.name</c> (always
|
||||
/// <see cref="System.Environment.MachineName"/>) and every
|
||||
/// <see cref="ZbTelemetryOptions.ResourceAttributes"/> entry. Optional attributes with null
|
||||
/// values are omitted; a null <c>ServiceVersion</c> defaults to <see cref="DefaultServiceVersion"/>.
|
||||
/// </summary>
|
||||
/// <param name="options">The telemetry options describing the service identity.</param>
|
||||
public static ResourceBuilder Build(ZbTelemetryOptions options) =>
|
||||
@@ -36,26 +95,44 @@ public static class ZbResource
|
||||
/// Serilog OTLP log sink — derives its attributes from this one map, so logs can never drift
|
||||
/// from metrics/traces. Required attributes (<c>service.name</c>, <c>service.namespace</c>,
|
||||
/// <c>service.instance.id</c>, <c>host.name</c>) are always present; optional ones
|
||||
/// (<c>service.version</c>, <c>site.id</c>, <c>node.role</c>) are included only when the
|
||||
/// corresponding option is non-null/non-empty, matching the Resource's omission rules.
|
||||
/// (<c>site.id</c>, <c>node.role</c>) are included only when the corresponding option is
|
||||
/// non-null/non-empty; <c>service.version</c> follows <see cref="ZbTelemetryOptions.ServiceVersion"/>
|
||||
/// (null = <see cref="DefaultServiceVersion"/>, <c>""</c> = omitted); every
|
||||
/// <see cref="ZbTelemetryOptions.ResourceAttributes"/> entry is included.
|
||||
/// </summary>
|
||||
/// <param name="options">The telemetry options describing the service identity.</param>
|
||||
/// <returns>The canonical attribute map carried by all three signals.</returns>
|
||||
public static IReadOnlyDictionary<string, object> BuildAttributes(ZbTelemetryOptions options)
|
||||
public static IReadOnlyDictionary<string, object> BuildAttributes(ZbTelemetryOptions options) =>
|
||||
BuildAttributes(options, DefaultServiceVersion);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="BuildAttributes(ZbTelemetryOptions)"/> with an explicit automatic
|
||||
/// <c>service.version</c> (the test seam for the entry-assembly default).
|
||||
/// </summary>
|
||||
internal static IReadOnlyDictionary<string, object> BuildAttributes(ZbTelemetryOptions options, string? defaultVersion)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
var attributes = new Dictionary<string, object>(StringComparer.Ordinal)
|
||||
{
|
||||
["service.name"] = options.ServiceName,
|
||||
["service.namespace"] = options.ServiceNamespace,
|
||||
["service.instance.id"] = InstanceId,
|
||||
["host.name"] = System.Environment.MachineName,
|
||||
};
|
||||
var attributes = new Dictionary<string, object>(StringComparer.Ordinal);
|
||||
|
||||
if (!string.IsNullOrEmpty(options.ServiceVersion))
|
||||
// Caller-supplied extras first; the first-class keys below are reserved (the validator
|
||||
// rejects them in ResourceAttributes), so nothing here can be shadowed.
|
||||
if (options.ResourceAttributes is { } extras)
|
||||
{
|
||||
attributes["service.version"] = options.ServiceVersion;
|
||||
foreach (var (key, value) in extras)
|
||||
{
|
||||
attributes[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
attributes["service.name"] = options.ServiceName;
|
||||
attributes["service.namespace"] = options.ServiceNamespace;
|
||||
attributes["service.instance.id"] = InstanceId;
|
||||
attributes["host.name"] = System.Environment.MachineName;
|
||||
|
||||
if (ResolveServiceVersion(options, defaultVersion) is { } version)
|
||||
{
|
||||
attributes["service.version"] = version;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(options.SiteId))
|
||||
|
||||
@@ -48,6 +48,11 @@ public static class ZbTelemetryExtensions
|
||||
// same fail-fast treatment (Telemetry-011).
|
||||
ValidateRequired(options, nameof(options));
|
||||
|
||||
// Published for the Serilog companion: AddZbSerilog's OTLP log sink follows this
|
||||
// registration's exporter/endpoint/identity so that logs go where metrics and traces go
|
||||
// without the app repeating the settings (scadaproj#55).
|
||||
services.AddSingleton(new ZbTelemetryRegistration(options));
|
||||
|
||||
services.AddOpenTelemetry()
|
||||
.ConfigureResource(rb => ZbResource.Configure(rb, options))
|
||||
.WithMetrics(metrics =>
|
||||
|
||||
@@ -35,8 +35,11 @@ public sealed class ZbTelemetryOptions
|
||||
public string ServiceNamespace { get; set; } = "ZB.MOM.WW";
|
||||
|
||||
/// <summary>
|
||||
/// Optional. Populate from <c>AssemblyInformationalVersion</c>.
|
||||
/// Populates OTel Resource <c>service.version</c>.
|
||||
/// Populates OTel Resource <c>service.version</c>. <see langword="null"/> (the default) means
|
||||
/// <em>automatic</em>: the entry assembly's <c>AssemblyInformationalVersion</c> (every family app
|
||||
/// stamps the git SHA there, e.g. <c>1.0.0+424db155…</c>), falling back to its assembly version
|
||||
/// (see <see cref="ZbResource.DefaultServiceVersion"/>). Set a value to override it; set
|
||||
/// <c>""</c> to omit the attribute. Before 0.3.0 <see langword="null"/> omitted it.
|
||||
/// </summary>
|
||||
public string? ServiceVersion { get; set; }
|
||||
|
||||
@@ -52,6 +55,17 @@ public sealed class ZbTelemetryOptions
|
||||
/// </summary>
|
||||
public string? NodeRole { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Extra OTel Resource attributes carried by every signal (metrics, traces and the Serilog OTLP
|
||||
/// log sink), e.g. <c>["otopcua.cluster.id"] = "c1"</c>. Values must be non-null; use strings,
|
||||
/// booleans, integers or doubles. Keys owned by a first-class option — <c>service.name</c>,
|
||||
/// <c>service.namespace</c>, <c>service.version</c>, <c>service.instance.id</c>,
|
||||
/// <c>host.name</c>, <c>site.id</c>, <c>node.role</c> — are rejected at startup; set the option
|
||||
/// instead (<see cref="ZbResource.ReservedAttributeKeys"/>).
|
||||
/// </summary>
|
||||
public IDictionary<string, object> ResourceAttributes { get; set; } =
|
||||
new Dictionary<string, object>(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// App-specific Meter names to register with the OTel MeterProvider. Standard instrumentation
|
||||
/// meters are added automatically (ASP.NET Core, HttpClient, runtime, process).
|
||||
@@ -73,4 +87,13 @@ public sealed class ZbTelemetryOptions
|
||||
/// OTLP gRPC endpoint, e.g. <c>"http://collector:4317"</c>.
|
||||
/// </summary>
|
||||
public string? OtlpEndpoint { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// When <see cref="Exporter"/> = <see cref="ZbExporter.Otlp"/>, also export logs to the same
|
||||
/// <see cref="OtlpEndpoint"/> through the <c>ZB.MOM.WW.Telemetry.Serilog</c> OTLP log sink.
|
||||
/// Default <see langword="true"/>. Set <see langword="false"/> to keep logs on the local sinks
|
||||
/// only while metrics and traces go to OTLP. Ignored for the Prometheus exporter, and ignored
|
||||
/// when the Serilog companion package is not used.
|
||||
/// </summary>
|
||||
public bool ExportLogs { get; set; } = true;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ internal static class ZbTelemetryOptionsValidator
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
ValidateResourceAttributes(options, paramName);
|
||||
|
||||
if (options.Exporter != ZbExporter.Otlp)
|
||||
{
|
||||
return;
|
||||
@@ -46,4 +48,36 @@ internal static class ZbTelemetryOptionsValidator
|
||||
paramName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateResourceAttributes(ZbTelemetryOptions options, string paramName)
|
||||
{
|
||||
if (options.ResourceAttributes is not { Count: > 0 } extras)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var (key, value) in extras)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"ZbTelemetryOptions.ResourceAttributes contains a blank key.", paramName);
|
||||
}
|
||||
|
||||
if (ZbResource.ReservedAttributeKeys.Contains(key))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"ZbTelemetryOptions.ResourceAttributes may not set '{key}'; it is owned by a " +
|
||||
"first-class option (ServiceName, ServiceNamespace, ServiceVersion, SiteId, NodeRole) " +
|
||||
"or computed by the library.",
|
||||
paramName);
|
||||
}
|
||||
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"ZbTelemetryOptions.ResourceAttributes['{key}'] is null.", paramName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace ZB.MOM.WW.Telemetry;
|
||||
|
||||
/// <summary>
|
||||
/// The options an <c>AddZbTelemetry</c> call registered, published in DI so the Serilog companion
|
||||
/// (<c>AddZbSerilog</c>) can send logs to the same OTLP endpoint with the same Resource as metrics
|
||||
/// and traces. Internal: the companion assembly sees it through <c>InternalsVisibleTo</c>.
|
||||
/// </summary>
|
||||
/// <param name="Options">The validated options passed to <c>AddZbTelemetry</c>.</param>
|
||||
internal sealed record ZbTelemetryRegistration(ZbTelemetryOptions Options);
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+5
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user