feat: opentelemetry tracing support

This commit is contained in:
Timothy Schenk 2023-11-20 14:23:04 +01:00 committed by rainote
parent 8f6886a6cf
commit 4823c352dc
3 changed files with 74 additions and 4 deletions

View file

@ -5,8 +5,13 @@ using MassTransit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Server.DB; using Server.DB;
using Server.Services; using Server.Services;
using Wonderking.Game.Mapping; using Wonderking.Game.Mapping;
@ -19,9 +24,6 @@ builder.Environment.EnvironmentName = "Development";
builder.Configuration.AddJsonFile("settings.json", true, true) builder.Configuration.AddJsonFile("settings.json", true, true)
.AddJsonFile($"settings.{builder.Environment.EnvironmentName}.json", true) .AddJsonFile($"settings.{builder.Environment.EnvironmentName}.json", true)
.AddEnvironmentVariables().Build(); .AddEnvironmentVariables().Build();
builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
JsonSerializer.Deserialize<CharacterStatsMappingConfiguration>(
File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException());
builder.Services.AddLogging(); builder.Services.AddLogging();
var loggerFactory = LoggerFactory.Create(loggingBuilder => var loggerFactory = LoggerFactory.Create(loggingBuilder =>
@ -31,9 +33,45 @@ var loggerFactory = LoggerFactory.Create(loggingBuilder =>
loggingBuilder.AddConsole(); loggingBuilder.AddConsole();
}); });
var configuration = builder.Configuration;
Action<ResourceBuilder> resourceBuilderAction = r => r
.AddService("Continuity", serviceInstanceId: Environment.MachineName);
builder.Services.AddOpenTelemetry()
.ConfigureResource(resourceBuilderAction)
.WithTracing(tracing =>
{
tracing.AddSource(nameof(Server));
tracing.SetSampler(new AlwaysOnSampler());
tracing.AddMassTransitInstrumentation();
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true);
})
.WithMetrics(metrics => { metrics.AddRuntimeInstrumentation(); });
builder.Logging.AddOpenTelemetry(logging =>
{
var resourceBuilder = ResourceBuilder.CreateDefault();
resourceBuilderAction(resourceBuilder);
logging.SetResourceBuilder(resourceBuilder);
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging =>
logging.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty)));
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics =>
metrics.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty)));
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing =>
tracing.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty)));
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
JsonSerializer.Deserialize<CharacterStatsMappingConfiguration>(
File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException());
builder.Services.AddDbContextPool<WonderkingContext>(o => builder.Services.AddDbContextPool<WonderkingContext>(o =>
{ {
using var configuration = builder.Configuration;
o.UseNpgsql( o.UseNpgsql(
$"Host={configuration["DB:Host"]};Username={configuration["DB:Username"]};Password={configuration["DB:Password"]};Database={configuration["DB:Database"]};Port={configuration["DB:Port"]}") $"Host={configuration["DB:Host"]};Username={configuration["DB:Username"]};Password={configuration["DB:Password"]};Database={configuration["DB:Database"]};Port={configuration["DB:Port"]}")
.EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory); .EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory);

View file

@ -89,6 +89,15 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="OpenTelemetry" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.8" />
<PackageReference Include="OpenTelemetry.Instrumentation.MassTransit" Version="1.0.0-beta.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" />
<PackageReference Include="OpenTelemetry.PersistentStorage.FileSystem" Version="1.0.0" />
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.4" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/> <PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/>
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
</ItemGroup> </ItemGroup>

View file

@ -13,6 +13,7 @@ services:
- DB:Username=continuity - DB:Username=continuity
- DB:Password=continuity - DB:Password=continuity
- Game:Data:Path=/app/data/ - Game:Data:Path=/app/data/
- OTLP:Endpoint=http://jaeger:4317
networks: networks:
- continuity - continuity
ports: ports:
@ -51,8 +52,30 @@ services:
timeout: 3s timeout: 3s
retries: 3 retries: 3
jaeger:
container_name: continuity-jaeger
image: jaegertracing/all-in-one:1.51.0
restart: always
networks:
- continuity
ports:
- 6831:6831/udp
- 6832:6832/udp
- 5778:5778
- 16686:16686
- 4317:4317
- 4318:4318
- 14250:14250
- 14268:14268
- 14269:14269
- 9411:9411
environment:
- COLLECTOR_ZIPKIN_HOST_PORT=:9411
- COLLECTOR_OTLP_ENABLED=true
networks: networks:
continuity: continuity:
volumes: volumes:
db-data: db-data:
prom-data: