From f11e024306e9ef6c46a3a2f456f6fec41dd1c689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Wed, 11 Dec 2024 09:59:17 +0100 Subject: [PATCH] chore: otel suggestions (#1586) --- .../OpenTelemetryEventListener.cs | 1 - .../OpenApiDocumentExtensions.cs | 1 - .../AspNetUtilitiesExtensions.cs | 40 +++++++++---------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.GraphQL/OpenTelemetryEventListener.cs b/src/Digdir.Domain.Dialogporten.GraphQL/OpenTelemetryEventListener.cs index 67beed8bb..b851f63f6 100644 --- a/src/Digdir.Domain.Dialogporten.GraphQL/OpenTelemetryEventListener.cs +++ b/src/Digdir.Domain.Dialogporten.GraphQL/OpenTelemetryEventListener.cs @@ -2,7 +2,6 @@ using HotChocolate.Execution; using HotChocolate.Execution.Instrumentation; using Microsoft.AspNetCore.Http.Extensions; -using OpenTelemetry.Trace; namespace Digdir.Domain.Dialogporten.GraphQL; diff --git a/src/Digdir.Domain.Dialogporten.WebApi/OpenApiDocumentExtensions.cs b/src/Digdir.Domain.Dialogporten.WebApi/OpenApiDocumentExtensions.cs index 384199f89..94ef325f1 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/OpenApiDocumentExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/OpenApiDocumentExtensions.cs @@ -1,4 +1,3 @@ -using Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.ServiceOwner.Dialogs.Update; using NJsonSchema; using NSwag; diff --git a/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs b/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs index 394fe7e43..4fe656085 100644 --- a/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs +++ b/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs @@ -1,4 +1,3 @@ -using Azure.Monitor.OpenTelemetry.AspNetCore; using Digdir.Library.Utils.AspNet.HealthChecks; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Builder; @@ -7,14 +6,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Npgsql; using OpenTelemetry.Trace; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry; using OpenTelemetry.Exporter; -using OpenTelemetry.Logs; using System.Diagnostics; using Azure.Monitor.OpenTelemetry.Exporter; @@ -69,29 +66,28 @@ public static WebApplicationBuilder ConfigureTelemetry( var resourceBuilder = resource.AddService(serviceName: settings.ServiceName ?? builder.Environment.ApplicationName); var resourceAttributes = settings.ResourceAttributes; - if (!string.IsNullOrEmpty(resourceAttributes)) + if (string.IsNullOrEmpty(resourceAttributes)) return; + + try { - try - { - var attributes = resourceAttributes - .Split(',', StringSplitOptions.RemoveEmptyEntries) - .Select(pair => pair.Split('=', 2)) - .Where(parts => parts.Length == 2 && !string.IsNullOrEmpty(parts[0])) - .Select(parts => new KeyValuePair(parts[0].Trim(), parts[1].Trim())); + var attributes = resourceAttributes + .Split(',', StringSplitOptions.RemoveEmptyEntries) + .Select(pair => pair.Split('=', 2)) + .Where(parts => parts.Length == 2 && !string.IsNullOrEmpty(parts[0])) + .Select(parts => new KeyValuePair(parts[0].Trim(), parts[1].Trim())); - foreach (var attribute in attributes) - { - resourceBuilder.AddAttributes(new[] { attribute }); - } - } - catch (Exception ex) + foreach (var attribute in attributes) { - throw new InvalidOperationException( - "Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2", - ex - ); + resourceBuilder.AddAttributes([attribute]); } } + catch (Exception ex) + { + throw new InvalidOperationException( + "Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2", + ex + ); + } }); if (!string.IsNullOrEmpty(settings.Endpoint) && !string.IsNullOrEmpty(settings.Protocol)) @@ -131,7 +127,7 @@ public static WebApplicationBuilder ConfigureTelemetry( .AddHttpClientInstrumentation(o => { o.RecordException = true; - o.FilterHttpRequestMessage = (_) => + o.FilterHttpRequestMessage = _ => { var parentActivity = Activity.Current?.Parent; if (parentActivity != null && parentActivity.Source.Name.Equals("Azure.Core.Http", StringComparison.Ordinal))