From 6a9e1c338e599815560b41bb3b9db4a0d9a95d01 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 20 Nov 2023 19:53:40 +0100 Subject: [PATCH] feat: add zipkin exporter and additional tracing options --- Server/Program.cs | 89 +++++++++++++++++++++++---------------- Server/Server.csproj | 2 + Server/docker-compose.yml | 11 ++++- 3 files changed, 64 insertions(+), 38 deletions(-) diff --git a/Server/Program.cs b/Server/Program.cs index 20d2db2..b1bb3dc 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Npgsql; using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; @@ -34,46 +35,58 @@ var loggerFactory = LoggerFactory.Create(loggingBuilder => }); var configuration = builder.Configuration; - -Action 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 => +if (configuration.GetValue("Tracing:Enabled")) { - var resourceBuilder = ResourceBuilder.CreateDefault(); - resourceBuilderAction(resourceBuilder); - logging.SetResourceBuilder(resourceBuilder); - logging.IncludeFormattedMessage = true; - logging.IncludeScopes = true; -}); -builder.Services.Configure(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))); + Action 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(); + 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(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() .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); -builder.Services.AddSingleton( - JsonSerializer.Deserialize( - File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException()); builder.Services.AddDbContextPool(o => { @@ -82,6 +95,10 @@ builder.Services.AddDbContextPool(o => .EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory); }); +builder.Services.AddSingleton( + JsonSerializer.Deserialize( + File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException()); + builder.Services.AddSingleton(loggerFactory); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/Server/Server.csproj b/Server/Server.csproj index 1ef7a0a..a3ff269 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -85,6 +85,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -92,6 +93,7 @@ + diff --git a/Server/docker-compose.yml b/Server/docker-compose.yml index 0619bdb..ea442da 100644 --- a/Server/docker-compose.yml +++ b/Server/docker-compose.yml @@ -13,7 +13,9 @@ services: - DB:Username=continuity - DB:Password=continuity - Game:Data:Path=/app/data/ + - Tracing:Enabled=true - OTLP:Endpoint=http://jaeger:4317 + - Zipkin:Endpoint=http://zipkin:9411/api/v2/spans networks: - continuity ports: @@ -68,11 +70,16 @@ services: - 14250:14250 - 14268:14268 - 14269:14269 - - 9411:9411 environment: - - COLLECTOR_ZIPKIN_HOST_PORT=:9411 - COLLECTOR_OTLP_ENABLED=true + zipkin: + image: openzipkin/zipkin + ports: + - 9411:9411 + networks: + - continuity + networks: continuity: