Skip to content

Commit

Permalink
chore: otel suggestions (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad authored Dec 11, 2024
1 parent ab2bedb commit f11e024
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using HotChocolate.Execution;
using HotChocolate.Execution.Instrumentation;
using Microsoft.AspNetCore.Http.Extensions;
using OpenTelemetry.Trace;

namespace Digdir.Domain.Dialogporten.GraphQL;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.ServiceOwner.Dialogs.Update;
using NJsonSchema;
using NSwag;

Expand Down
40 changes: 18 additions & 22 deletions src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Digdir.Library.Utils.AspNet.HealthChecks;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
Expand All @@ -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;

Expand Down Expand Up @@ -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<string, object>(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<string, object>(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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f11e024

Please sign in to comment.