-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/OpenTelemetryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using Azure.Monitor.OpenTelemetry.Exporter; | ||
using Npgsql; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Exporter; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
using System.Diagnostics; | ||
|
||
namespace Digdir.Domain.Dialogporten.WebApi.Common.Extensions; | ||
|
||
internal static class OpenTelemetryExtensions | ||
{ | ||
public static IServiceCollection AddDialogportenTelemetry( | ||
this IServiceCollection services, | ||
IConfiguration configuration, | ||
IHostEnvironment environment) | ||
{ | ||
if (string.IsNullOrEmpty(configuration["OTEL_EXPORTER_OTLP_ENDPOINT"])) | ||
return services; | ||
|
||
var otlpProtocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]?.ToLowerInvariant() switch | ||
{ | ||
"grpc" => OtlpExportProtocol.Grpc, | ||
"http/protobuf" => OtlpExportProtocol.HttpProtobuf, | ||
"http" => OtlpExportProtocol.HttpProtobuf, | ||
_ => throw new ArgumentException($"Unsupported protocol: {configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]}") | ||
}; | ||
|
||
var endpoint = new Uri(configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]!); | ||
|
||
return services.AddOpenTelemetry() | ||
.ConfigureResource(resource => | ||
{ | ||
resource.AddService( | ||
serviceName: configuration["OTEL_SERVICE_NAME"] ?? environment.ApplicationName); | ||
}) | ||
.WithTracing(tracing => | ||
{ | ||
if (environment.IsDevelopment()) | ||
{ | ||
tracing.SetSampler(new AlwaysOnSampler()); | ||
} | ||
|
||
tracing | ||
.AddAspNetCoreInstrumentation(opts => | ||
{ | ||
opts.RecordException = true; | ||
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health"); | ||
}) | ||
.AddHttpClientInstrumentation(o => | ||
{ | ||
o.RecordException = true; | ||
o.FilterHttpRequestMessage = _ => | ||
{ | ||
var parentActivity = Activity.Current?.Parent; | ||
if (parentActivity != null && parentActivity.Source.Name.Equals("Azure.Core.Http", StringComparison.Ordinal)) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
}; | ||
}) | ||
.AddEntityFrameworkCoreInstrumentation() | ||
.AddNpgsql() | ||
.AddFusionCacheInstrumentation() | ||
.AddOtlpExporter(options => | ||
{ | ||
options.Endpoint = new Uri(endpoint, "/v1/traces"); | ||
options.Protocol = otlpProtocol; | ||
}); | ||
}) | ||
.WithMetrics(metrics => | ||
{ | ||
metrics.AddRuntimeInstrumentation() | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation(); | ||
|
||
var appInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]; | ||
if (!string.IsNullOrEmpty(appInsightsConnectionString)) | ||
{ | ||
metrics.AddAzureMonitorMetricExporter(options => | ||
{ | ||
options.ConnectionString = appInsightsConnectionString; | ||
}); | ||
} | ||
else | ||
{ | ||
metrics.AddOtlpExporter(options => | ||
{ | ||
options.Endpoint = new Uri(endpoint, "/v1/metrics"); | ||
options.Protocol = otlpProtocol; | ||
}); | ||
} | ||
}) | ||
.Services; | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/Digdir.Domain.Dialogporten.WebApi/Common/Middleware/RequestLoggingMiddleware.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.