feat: add zipkin exporter and additional tracing options

This commit is contained in:
Timothy Schenk 2023-11-20 19:53:40 +01:00
parent 157dbbc2f5
commit 6a9e1c338e
3 changed files with 64 additions and 38 deletions

View file

@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Npgsql;
using OpenTelemetry.Logs; using OpenTelemetry.Logs;
using OpenTelemetry.Metrics; using OpenTelemetry.Metrics;
using OpenTelemetry.Resources; using OpenTelemetry.Resources;
@ -34,46 +35,58 @@ var loggerFactory = LoggerFactory.Create(loggingBuilder =>
}); });
var configuration = builder.Configuration; var configuration = builder.Configuration;
if (configuration.GetValue<bool>("Tracing:Enabled"))
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);
tracing.AddHttpClientInstrumentation();
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation();
metrics.AddHttpClientInstrumentation();
});
builder.Logging.AddOpenTelemetry(logging =>
{ {
var resourceBuilder = ResourceBuilder.CreateDefault(); Action<ResourceBuilder> resourceBuilderAction = r => r
resourceBuilderAction(resourceBuilder); .AddService("Continuity", serviceInstanceId: Environment.MachineName);
logging.SetResourceBuilder(resourceBuilder);
logging.IncludeFormattedMessage = true; builder.Services.AddOpenTelemetry()
logging.IncludeScopes = true; .ConfigureResource(resourceBuilderAction)
}); .WithTracing(tracing =>
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => {
logging.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty))); tracing.AddSource(nameof(Server));
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => tracing.SetSampler(new AlwaysOnSampler());
metrics.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty))); tracing.AddMassTransitInstrumentation();
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true);
tracing.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty))); tracing.AddHttpClientInstrumentation();
tracing.AddNpgsql();
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation();
metrics.AddHttpClientInstrumentation();
});
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.AddZipkinExporter(options =>
options.Endpoint = new Uri(configuration["Zipkin:Endpoint"] ?? string.Empty));
tracing.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty));
});
}
builder.Services.AddHealthChecks() builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); .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 =>
{ {
@ -82,6 +95,10 @@ builder.Services.AddDbContextPool<WonderkingContext>(o =>
.EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory); .EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory);
}); });
builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
JsonSerializer.Deserialize<CharacterStatsMappingConfiguration>(
File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException());
builder.Services.AddSingleton<ILoggerFactory>(loggerFactory); builder.Services.AddSingleton<ILoggerFactory>(loggerFactory);
builder.Services.AddSingleton<PacketDistributorService>(); builder.Services.AddSingleton<PacketDistributorService>();
builder.Services.AddSingleton<ItemObjectPoolService>(); builder.Services.AddSingleton<ItemObjectPoolService>();

View file

@ -85,6 +85,7 @@
<PackageReference Include="NetCoreServer" Version="8.0.3" /> <PackageReference Include="NetCoreServer" Version="8.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/> <PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2"/> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2"/>
<PackageReference Include="Npgsql.OpenTelemetry" Version="7.0.6" />
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.10.4539"> <PackageReference Include="Nullable.Extended.Analyzer" Version="1.10.4539">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@ -92,6 +93,7 @@
<PackageReference Include="OpenTelemetry" Version="1.7.0-alpha.1" /> <PackageReference Include="OpenTelemetry" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" 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.Exporter.OpenTelemetryProtocol" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" 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.EntityFrameworkCore" Version="1.0.0-beta.8" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.6.0-beta.3" /> <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.6.0-beta.3" />

View file

@ -13,7 +13,9 @@ services:
- DB:Username=continuity - DB:Username=continuity
- DB:Password=continuity - DB:Password=continuity
- Game:Data:Path=/app/data/ - Game:Data:Path=/app/data/
- Tracing:Enabled=true
- OTLP:Endpoint=http://jaeger:4317 - OTLP:Endpoint=http://jaeger:4317
- Zipkin:Endpoint=http://zipkin:9411/api/v2/spans
networks: networks:
- continuity - continuity
ports: ports:
@ -68,11 +70,16 @@ services:
- 14250:14250 - 14250:14250
- 14268:14268 - 14268:14268
- 14269:14269 - 14269:14269
- 9411:9411
environment: environment:
- COLLECTOR_ZIPKIN_HOST_PORT=:9411
- COLLECTOR_OTLP_ENABLED=true - COLLECTOR_OTLP_ENABLED=true
zipkin:
image: openzipkin/zipkin
ports:
- 9411:9411
networks:
- continuity
networks: networks:
continuity: continuity: