diff --git a/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs b/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs index 1d0da8ea3..5808a8408 100644 --- a/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs +++ b/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs @@ -74,32 +74,8 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]; settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]; + settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"]; settings.TraceSources.Add(DialogportenGraphQLSource); - - var resourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"]; - if (!string.IsNullOrEmpty(resourceAttributes)) - { - try - { - var attributes = System.Web.HttpUtility.ParseQueryString( - resourceAttributes.Replace(',', '&') - ); - foreach (string key in attributes.Keys) - { - if (!string.IsNullOrEmpty(key)) - { - settings.ResourceAttributes[key] = attributes[key] ?? string.Empty; - } - } - } - catch (Exception ex) - { - throw new InvalidOperationException( - "Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2", - ex - ); - } - } }); builder.Services diff --git a/src/Digdir.Domain.Dialogporten.Service/Program.cs b/src/Digdir.Domain.Dialogporten.Service/Program.cs index 9fa088077..28a1c3a39 100644 --- a/src/Digdir.Domain.Dialogporten.Service/Program.cs +++ b/src/Digdir.Domain.Dialogporten.Service/Program.cs @@ -57,31 +57,7 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]; settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]; - - var resourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"]; - if (!string.IsNullOrEmpty(resourceAttributes)) - { - try - { - var attributes = System.Web.HttpUtility.ParseQueryString( - resourceAttributes.Replace(',', '&') - ); - foreach (string key in attributes.Keys) - { - if (!string.IsNullOrEmpty(key)) - { - settings.ResourceAttributes[key] = attributes[key] ?? string.Empty; - } - } - } - catch (Exception ex) - { - throw new InvalidOperationException( - "Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2", - ex - ); - } - } + settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"]; }); builder.Services diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index bc980ec39..05b9f82c4 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -84,31 +84,7 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]; settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]; - - var resourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"]; - if (!string.IsNullOrEmpty(resourceAttributes)) - { - try - { - var attributes = System.Web.HttpUtility.ParseQueryString( - resourceAttributes.Replace(',', '&') - ); - foreach (string key in attributes.Keys) - { - if (!string.IsNullOrEmpty(key)) - { - settings.ResourceAttributes[key] = attributes[key] ?? string.Empty; - } - } - } - catch (Exception ex) - { - throw new InvalidOperationException( - "Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2", - ex - ); - } - } + settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"]; }); builder.Services diff --git a/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs b/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs index 50f67d0f6..ed1d1b990 100644 --- a/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs +++ b/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs @@ -72,9 +72,29 @@ public static WebApplicationBuilder ConfigureTelemetry( { var resourceBuilder = resource.AddService(serviceName: settings.ServiceName ?? builder.Environment.ApplicationName); - foreach (var attr in settings.ResourceAttributes) + var resourceAttributes = settings.ResourceAttributes; + if (!string.IsNullOrEmpty(resourceAttributes)) { - resourceBuilder.AddAttributes(new[] { new KeyValuePair(attr.Key, attr.Value) }); + try + { + var attributes = System.Web.HttpUtility.ParseQueryString( + resourceAttributes.Replace(',', '&') + ); + foreach (string key in attributes.Keys) + { + if (!string.IsNullOrEmpty(key)) + { + resourceBuilder.AddAttributes(new[] { new KeyValuePair(key, attributes[key] ?? string.Empty) }); + } + } + } + catch (Exception ex) + { + throw new InvalidOperationException( + "Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2", + ex + ); + } } }); @@ -158,7 +178,7 @@ public class TelemetrySettings public string? Endpoint { get; set; } public string? Protocol { get; set; } public string? AppInsightsConnectionString { get; set; } - public Dictionary ResourceAttributes { get; set; } = new(); + public string? ResourceAttributes { get; set; } public HashSet TraceSources { get; set; } = new() { AzureSource,