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

[DRAFT] Chore(webapi): Add Opentelemetry tracing to webapi #1202

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 @@ -70,8 +70,11 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi

services.AddFusionCacheNeueccMessagePackSerializer();

services.AddStackExchangeRedisCache(options =>
options.ConnectionMultiplexerFactory = () => Task.FromResult<IConnectionMultiplexer>(
ConnectionMultiplexer.Connect(infrastructureSettings.Redis.ConnectionString)
));

services.AddStackExchangeRedisCache(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString);
services.AddFusionCacheStackExchangeRedisBackplane(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString);

services.AddGraphQlRedisSubscriptions(infrastructureSettings.Redis.ConnectionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.1"/>
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.29.0"/>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="7.3.0"/>
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.4" />
<PackageReference Include="OpenTelemetry" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.9.0-beta.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2"/>
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0"/>
</ItemGroup>
Expand All @@ -25,4 +33,4 @@
<ProjectReference Include="..\Digdir.Tool.Dialogporten.GenerateFakeData\Digdir.Tool.Dialogporten.GenerateFakeData.csproj"/>
</ItemGroup>

</Project>
</Project>
43 changes: 35 additions & 8 deletions src/Digdir.Domain.Dialogporten.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Digdir.Domain.Dialogporten.Application;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions;
Expand All @@ -20,9 +21,12 @@
using FastEndpoints;
using FastEndpoints.Swagger;
using FluentValidation;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authorization;
using Npgsql;
using NSwag;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Metrics;
using Serilog;
using Microsoft.Extensions.Options;

Expand All @@ -31,9 +35,7 @@
.MinimumLevel.Warning()
.Enrich.FromLogContext()
.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
.WriteTo.ApplicationInsights(
TelemetryConfiguration.CreateDefault(),
TelemetryConverter.Traces)
.WriteTo.ApplicationInsights(TelemetryConverter.Traces)
.CreateBootstrapLogger();

try
Expand All @@ -59,9 +61,7 @@ static void BuildAndRun(string[] args)
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.ApplicationInsights(
services.GetRequiredService<TelemetryConfiguration>(),
TelemetryConverter.Traces));
.WriteTo.ApplicationInsights(TelemetryConverter.Traces));

builder.Configuration
.AddAzureConfiguration(builder.Environment.EnvironmentName)
Expand Down Expand Up @@ -100,7 +100,6 @@ static void BuildAndRun(string[] args)
.AddHttpContextAccessor()
.AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true)
.AddAzureAppConfiguration()
.AddApplicationInsightsTelemetry()
.AddEndpointsApiExplorer()
.AddFastEndpoints()
.SwaggerDocument(x =>
Expand Down Expand Up @@ -135,6 +134,34 @@ static void BuildAndRun(string[] args)
.Select(z => z.WellKnown)
.ToList() ?? [])

// OpenTelemetry
.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService(serviceName: builder.Environment.ApplicationName))
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
tracing.SetSampler(new AlwaysOnSampler());
}

tracing.AddAspNetCoreInstrumentation(options =>
{
options.Filter = (httpContext) =>
!httpContext.Request.Path.StartsWithSegments("/health");
});

tracing.AddHttpClientInstrumentation();
tracing.AddNpgsql();
tracing.AddRedisInstrumentation(options => options.SetVerboseDatabaseStatements = true);
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation();
})
.UseAzureMonitor()
.Services

// Auth
.AddDialogportenAuthentication(builder.Configuration)
.AddAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@
}
},
"LocalDevelopment": {
"UseLocalDevelopmentUser": true,
"UseLocalDevelopmentResourceRegister": true,
"UseLocalDevelopmentOrganizationRegister": true,
"UseLocalDevelopmentNameRegister": true,
"UseLocalDevelopmentAltinnAuthorization": true,
"UseLocalDevelopmentUser": false,
"UseLocalDevelopmentResourceRegister": false,
"UseLocalDevelopmentOrganizationRegister": false,
"UseLocalDevelopmentNameRegister": false,
"UseLocalDevelopmentAltinnAuthorization": false,
"UseLocalDevelopmentCloudEventBus": true,
"UseLocalDevelopmentCompactJwsGenerator": true,
"DisableShortCircuitOutboxDispatcher": true,
"DisableCache": true,
"DisableAuth": true
"DisableCache": false,
"DisableAuth": false
}
}
3 changes: 3 additions & 0 deletions src/Digdir.Domain.Dialogporten.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"AzureMonitor": {
"ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-APP-SETTING"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task FailIfSwaggerSnapshotDoesNotMatch()
// The order of the properties in the swagger.json file is not cross-platform deterministic.
// Running client.GetAsync("/swagger/v1/swagger.json"); on Windows and Mac will produce
// different ordering of the results (although the content is the same). So we force an
// alphabetical ordering of the properties to make the test deterministic.
// alphabetical ordering of the properties to make the test deterministic.
// Ref: https://github.com/digdir/dialogporten/issues/996
var orderedSwagger = SortJson(newSwagger);

Expand Down
Loading