Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: otel suggestions #1586

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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