using Microsoft.Extensions.Configuration; using ZB.MOM.WW.ScadaBridge.Host; using ZB.MOM.WW.Telemetry; namespace ZB.MOM.WW.ScadaBridge.Host.Tests; /// /// scadaproj#55: the logger and the AddZbTelemetry registration fill their options from one place, /// so ScadaBridge:Telemetry:Exporter=Otlp reaches the pre-host logger (OTLP log sink) as well as /// metrics and traces, and the node identity is unchanged. /// public class ScadaBridgeTelemetryConfigTests { private static IConfiguration Config(Dictionary values) => new ConfigurationBuilder().AddInMemoryCollection(values).Build(); /// Exporter and endpoint come from ScadaBridge:Telemetry; identity from the arguments. [Fact] public void Apply_CarriesExporterAndIdentity() { var options = new ZbTelemetryOptions(); ScadaBridgeTelemetryConfig.Apply(options, Config(new() { ["ScadaBridge:Telemetry:Exporter"] = "Otlp", ["ScadaBridge:Telemetry:OtlpEndpoint"] = "http://collector:4317", }), siteId: "site-a", nodeRole: "Site"); Assert.Equal("scadabridge", options.ServiceName); Assert.Equal(ZbExporter.Otlp, options.Exporter); Assert.Equal("http://collector:4317", options.OtlpEndpoint); Assert.Equal("site-a", options.SiteId); Assert.Equal("Site", options.NodeRole); Assert.Null(options.ServiceVersion); // library default: entry-assembly informational version } /// A Telemetry-section SiteId/NodeRole cannot make the Resource diverge from the log enrichers. [Fact] public void Apply_NodeIdentityWinsOverTelemetrySection() { var options = new ZbTelemetryOptions(); ScadaBridgeTelemetryConfig.Apply(options, Config(new() { ["ScadaBridge:Telemetry:SiteId"] = "other", ["ScadaBridge:Telemetry:NodeRole"] = "other", }), siteId: "central", nodeRole: "Central"); Assert.Equal("central", options.SiteId); Assert.Equal("Central", options.NodeRole); Assert.Equal(ZbExporter.Prometheus, options.Exporter); } /// The OTLP logger path builds (validation passes) and still logs to the configured sinks. [Fact] public void CreateLogger_WithOtlpExporter_Builds() { var config = Config(new() { ["ScadaBridge:Telemetry:Exporter"] = "Otlp", ["ScadaBridge:Telemetry:OtlpEndpoint"] = "http://127.0.0.1:1", }); using var logger = ScadaBridgeLogging.CreateLogger(config, "Site", "site-a", "node-a", TextWriter.Null, additionalConfiguration: null); logger.Information("otlp wiring smoke test"); } /// A scheme-less endpoint now fails the logger at startup (Telemetry 0.3.0 validation). [Fact] public void CreateLogger_WithSchemelessEndpoint_Throws() { var config = Config(new() { ["ScadaBridge:Telemetry:Exporter"] = "Otlp", ["ScadaBridge:Telemetry:OtlpEndpoint"] = "collector:4317", }); Assert.Throws(() => ScadaBridgeLogging.CreateLogger(config, "Site", "site-a", "node-a", TextWriter.Null, additionalConfiguration: null)); } }