2011c8d6b9
Exporter, OtlpEndpoint, ServiceVersion, SiteId, NodeRole, ExportLogs and ResourceAttributes:<key> from <App>:Telemetry, absent keys keep code defaults, so the four apps stop re-implementing the exporter parse and gain the identity keys. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using ZB.MOM.WW.Telemetry;
|
|
|
|
namespace ZB.MOM.WW.Telemetry.Tests;
|
|
|
|
public sealed class ApplyConfigurationTests
|
|
{
|
|
private static IConfiguration Section(Dictionary<string, string?> values) =>
|
|
new ConfigurationBuilder().AddInMemoryCollection(values).Build().GetSection("App:Telemetry");
|
|
|
|
[Fact]
|
|
public void Applies_every_key()
|
|
{
|
|
var o = new ZbTelemetryOptions { ServiceName = "x" }.ApplyConfiguration(Section(new()
|
|
{
|
|
["App:Telemetry:Exporter"] = "otlp",
|
|
["App:Telemetry:OtlpEndpoint"] = "http://collector:4317",
|
|
["App:Telemetry:ServiceVersion"] = "5.0.0",
|
|
["App:Telemetry:SiteId"] = " plant-1 ",
|
|
["App:Telemetry:NodeRole"] = "standalone",
|
|
["App:Telemetry:ExportLogs"] = "false",
|
|
["App:Telemetry:ResourceAttributes:deployment.environment.name"] = "dev",
|
|
}));
|
|
|
|
Assert.Equal(ZbExporter.Otlp, o.Exporter);
|
|
Assert.Equal("http://collector:4317", o.OtlpEndpoint);
|
|
Assert.Equal("5.0.0", o.ServiceVersion);
|
|
Assert.Equal("plant-1", o.SiteId);
|
|
Assert.Equal("standalone", o.NodeRole);
|
|
Assert.False(o.ExportLogs);
|
|
Assert.Equal("dev", o.ResourceAttributes["deployment.environment.name"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void Absent_or_blank_keys_keep_code_defaults()
|
|
{
|
|
var o = new ZbTelemetryOptions { ServiceName = "x", SiteId = "code-site", NodeRole = "code-role" }
|
|
.ApplyConfiguration(Section(new()
|
|
{
|
|
["App:Telemetry:SiteId"] = " ",
|
|
["App:Telemetry:Exporter"] = "not-an-exporter",
|
|
}));
|
|
|
|
Assert.Equal("code-site", o.SiteId);
|
|
Assert.Equal("code-role", o.NodeRole);
|
|
Assert.Equal(ZbExporter.Prometheus, o.Exporter);
|
|
Assert.Null(o.OtlpEndpoint);
|
|
Assert.Null(o.ServiceVersion);
|
|
Assert.True(o.ExportLogs);
|
|
Assert.Empty(o.ResourceAttributes);
|
|
}
|
|
|
|
[Fact]
|
|
public void Empty_ServiceVersion_omits_the_attribute()
|
|
{
|
|
var o = new ZbTelemetryOptions { ServiceName = "x" }
|
|
.ApplyConfiguration(Section(new() { ["App:Telemetry:ServiceVersion"] = "" }));
|
|
|
|
Assert.Equal("", o.ServiceVersion);
|
|
Assert.False(ZbResource.BuildAttributes(o, "1.0.0+abc").ContainsKey("service.version"));
|
|
}
|
|
}
|