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; /// /// scadaproj#55: service.version defaults from the entry assembly, and caller-supplied /// Resource attributes reach the Resource the SDK pipeline actually uses. /// public sealed class ResourceIdentityTests { private static Dictionary PipelineResource(Action configure) { var builder = WebApplication.CreateBuilder(); builder.AddZbTelemetry(configure); using var app = builder.Build(); var tracer = app.Services.GetRequiredService(); var meter = app.Services.GetRequiredService(); 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()!.InformationalVersion; Assert.Equal(expected, ZbResource.VersionOf(assembly)); // The package version (Directory.Build.props), not a fixed literal that every release must edit. var v = assembly.GetName().Version!; Assert.StartsWith($"{v.Major}.{v.Minor}.{v.Build}+", 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(() => 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(() => new ServiceCollection().AddZbTelemetry(options)); Assert.Contains("custom", ex.Message); } [Fact] public void Blank_key_in_ResourceAttributes_is_rejected() { var builder = WebApplication.CreateBuilder(); Assert.Throws(() => 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(); Assert.Equal("telemetry-test", registration.Options.ServiceName); Assert.Equal("http://collector:4317", registration.Options.OtlpEndpoint); } }