Skip to content

feat: expand telemetry with golden signals, traces, business metrics, and logging - #160

Merged
Dejmenek merged 21 commits into
mainfrom
feat/159-expand-gathered-telemetry-golden-signals-traces-business-metrics-and-service-logging
Sep 18, 2026
Merged

Dejmenek merged 21 commits into
mainfrom
feat/159-expand-gathered-telemetry-golden-signals-traces-business-metrics-and-service-logging

Conversation

@Dejmenek

Copy link
Copy Markdown
Owner

Summary

This branch expands observability across the API: distributed tracing, business-level metrics, structured logging via Serilog, and GraphQL error handling improvements.

Changes

  • Added TournamentActivitySource and wired distributed tracing into core services, with EF Core instrumentation added to the OpenTelemetry tracing pipeline.
  • Added business metrics for Brackets, Matches, Participants, Users, and GraphQL (BracketMetrics, MatchMetrics, ParticipantMetrics, UserMetrics, GraphQLMetrics), registered as singletons and recorded from the relevant mutations/services.
  • Added UnhandledExceptionErrorFilter to log unhandled GraphQL exceptions and set an UNHANDLED_EXCEPTION error code when one isn't already present.
  • Switched logging from the default provider to Serilog with OpenTelemetry export; reworked ExecutionEventListener and HttpRequestInterceptor logging and removed the unused requestId from the interceptor.
  • Added PushEntityContext to ResolverContextExtensions for entity-aware log enrichment.
  • Refactored Bracket, Match, and User services from static classes to DI-injected instance classes (some moving from DbContext to IDbContextFactory) so they can take metrics/tracing dependencies and be unit tested.
  • Added a custom OnRejected handler for the rate limiter that logs rejected requests.
  • Updated unit tests to use instance methods via DI instead of static calls.
  • Added the OpenTelemetry EF Core instrumentation package and refreshed package lock files.

Refactored BracketCompletionService, BracketService, MatchCascadePositionCalculator, and MatchCorrectionService from static to instance classes to support dependency injection.
Introduced TournamentActivitySource-based tracing spans to key operations (bracket creation, match correction, round updates, auto-closing) in core services and mutation handlers. Enriched spans with tags like tournament ID, bracket ID, match ID, round number, participant count, correlation ID, and auto-close count for improved observability and diagnostics.
Introduced UserMetrics class to monitor login attempts, registrations, and refresh token failures using .NET metrics APIs. Registered UserMetrics as a singleton in DI. Updated UserMutations to record metrics and added logging for failed login and refresh token events. Injected ILoggerFactory to support enhanced observability.
BracketMutations now records success and failure via BracketMetrics when generating brackets, tracking unique constraint violations and successful creations. BracketMetrics is registered as a singleton in the DI container for application-wide access.
Added MatchMetrics class with counters for matches played, replays, and result corrections. Registered as a singleton service and injected into MatchMutations. Updated PlayMatch and CorrectMatchResult to increment metrics accordingly.
Introduce ParticipantMetrics as a singleton service and inject it into ParticipantMutations and TournamentMutations. Mutation methods now record metrics for slot contention, join attempts, and various participant-related events by calling ParticipantMetrics methods in relevant validation and exception handling branches.
Added tournaments_closed_total (by reason) and tournaments_deleted_total metrics. Updated auto-close and manual close logic to increment tournaments_closed_total with appropriate reason. Increment tournaments_deleted_total on tournament deletion.
…ic classes

Refactored BracketMutations.cs and MatchMutations.cs to inject BracketService, BracketMetrics, MatchCorrectionService, BracketCompletionService, and ILoggerFactory as parameters instead of using static service calls. Updated logger usage to utilize the injected logger instance. This enhances testability and aligns with dependency injection best practices.
Introduced GraphQLMetrics class to encapsulate OpenTelemetry metrics for GraphQL requests, including request duration and error counting. Registered GraphQLMetrics as a singleton service. Updated ExecutionEventListener to use GraphQLMetrics for recording request durations and errors during execution. Modified error handling to record errors via GraphQLMetrics.
Introduced PushEntityContext extension for IResolverContext to inject entity type and ID into both Serilog LogContext and GraphQL context data. Applied this in mutation resolvers to ensure entity context is available for logging during and after resolver execution. Enables more informative and request-level logging tied to specific entities.
Replaced direct ApplicationDbContext injection with IDbContextFactory<ApplicationDbContext> in MatchService and UserTournamentsService. Each method now creates its own DbContext instance using CreateDbContextAsync, improving lifetime management in async and multi-threaded scenarios., following official HotChocolate docs
Expanded MetricConstants with new meter names for Brackets, Matches, Participants, Users, and GraphQL. Registered these meters in TelemetryExtensions to enable metric collection and export for these domains.
Logs a warning with the client's IP and request path when a request is rejected due to rate limiting, using the application's logger.
Improved observability by logging trace and span IDs from Activity, distinguishing between anonymous and named operations, and normalizing operation type handling. Added entity type and ID logging from context data. Replaced requestId with traceId and spanId in logs. Simplified error extraction and updated log messages to include new trace, span, entity, and error details for both successful and error-completed GraphQL requests.
Introduced TournamentActivitySource static class for consistent tracing with a defined source name. Updated TelemetryExtensions to register this source in the OpenTelemetry pipeline, enabling improved traceability within the TournamentAPI.
Integrated OpenTelemetry.Instrumentation.EntityFrameworkCore for EF Core tracing. Updated OpenTelemetry.Api and ProviderBuilderExtensions to 1.18.0. Modified TelemetryExtensions.cs to enable EF Core instrumentation. Updated project and lock files for dependency consistency.
Introduce UnhandledExceptionErrorFilter to log unhandled GraphQL exceptions and assign a custom error code. Register the filter, its logger, and additional services for metrics and event listeners in GraphQLExtensions.cs.
Introduced ParticipantMetrics in TournamentAPI.Metrics using System.Diagnostics.Metrics. Tracks tournament join attempts with a "result" tag and slot contention events via counters. Metrics are initialized with IMeterFactory and exposed through public increment methods.
Added ILogger injection and warning log for invalid API key with client IP. Removed requestId global state generation and addition.
Replaced existing logging setup in Program.cs with Serilog and OpenTelemetry integration. Updated appsettings.json to configure Serilog log levels and overrides. Removed obsolete "Logging" sections from appsettings.Development.json and appsettings.json.
Refactored BracketServiceTests and MatchCascadePositionCalculatorTests to use instance methods instead of static methods. Introduced private readonly _sut fields initialized with NullLogger for each service under test. Updated all static method calls to use the instance methods on _sut, improving testability and supporting dependency injection practices.
@Dejmenek
Dejmenek merged commit 36f21ea into main Sep 18, 2026
3 checks passed
@Dejmenek
Dejmenek deleted the feat/159-expand-gathered-telemetry-golden-signals-traces-business-metrics-and-service-logging branch September 18, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expand gathered telemetry: golden signals, traces, business metrics, and service logging

1 participant