diff --git a/Server/Program.cs b/Server/Program.cs index 0c67d9b..28b4052 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -5,8 +5,13 @@ using MassTransit; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; using Server.DB; using Server.Services; using Wonderking.Game.Mapping; @@ -19,9 +24,6 @@ builder.Environment.EnvironmentName = "Development"; builder.Configuration.AddJsonFile("settings.json", true, true) .AddJsonFile($"settings.{builder.Environment.EnvironmentName}.json", true) .AddEnvironmentVariables().Build(); -builder.Services.AddSingleton( - JsonSerializer.Deserialize( - File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException()); builder.Services.AddLogging(); var loggerFactory = LoggerFactory.Create(loggingBuilder => @@ -31,9 +33,45 @@ var loggerFactory = LoggerFactory.Create(loggingBuilder => loggingBuilder.AddConsole(); }); +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); + }) + .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(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( + JsonSerializer.Deserialize( + File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException()); + builder.Services.AddDbContextPool(o => { - using var configuration = builder.Configuration; o.UseNpgsql( $"Host={configuration["DB:Host"]};Username={configuration["DB:Username"]};Password={configuration["DB:Password"]};Database={configuration["DB:Database"]};Port={configuration["DB:Port"]}") .EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory); diff --git a/Server/Server.csproj b/Server/Server.csproj index 77a4438..df61ab8 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -89,6 +89,15 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Server/docker-compose.yml b/Server/docker-compose.yml index 15191a8..0619bdb 100644 --- a/Server/docker-compose.yml +++ b/Server/docker-compose.yml @@ -13,6 +13,7 @@ services: - DB:Username=continuity - DB:Password=continuity - Game:Data:Path=/app/data/ + - OTLP:Endpoint=http://jaeger:4317 networks: - continuity ports: @@ -51,8 +52,30 @@ services: timeout: 3s 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: continuity: volumes: db-data: + prom-data: