cfc7168ace
- 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
200 lines
7.8 KiB
C#
200 lines
7.8 KiB
C#
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);
|
|
}
|
|
}
|