| | | 1 | | using OpenTelemetry.Metrics; |
| | | 2 | | using OpenTelemetry.Resources; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Heimdall.WebApi.Metrics; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Registers the OpenTelemetry metrics pipeline and mounts its Prometheus scrape endpoint. Both |
| | | 8 | | /// halves live here rather than inline in <c>Startup</c> so the tests can build a host with |
| | | 9 | | /// exactly this wiring and prove which requests reach the exporter — <c>Startup</c> itself |
| | | 10 | | /// cannot be exercised on the metrics port, because the in-memory test server reports a local |
| | | 11 | | /// port of <c>0</c> for every request. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class MetricsConfiguration |
| | | 14 | | { |
| | | 15 | | /// <summary>The <c>service.name</c> every series carries, so dashboards can tell this API apart.</summary> |
| | | 16 | | public const string ServiceName = "heimdall-api"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Meters that ship inside the runtime and the frameworks the API already uses, subscribed to by |
| | | 20 | | /// name. None of them costs a package: each is emitted whether or not anything listens, and |
| | | 21 | | /// naming it here is what makes the exporter collect it. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// <list type="bullet"> |
| | | 25 | | /// <item> |
| | | 26 | | /// <c>System.Runtime</c> — GC, heap, thread pool, exceptions, CPU. Built into .NET 9 |
| | | 27 | | /// and later, which is what replaces the OpenTelemetry.Instrumentation.Runtime package. |
| | | 28 | | /// </item> |
| | | 29 | | /// <item> |
| | | 30 | | /// <c>Microsoft.AspNetCore.Server.Kestrel</c> — connections, queued requests, TLS |
| | | 31 | | /// handshakes. <c>AddAspNetCoreInstrumentation</c> subscribes to it as well; it is named |
| | | 32 | | /// again so removing that call would not quietly take the connection metrics with it. |
| | | 33 | | /// </item> |
| | | 34 | | /// <item> |
| | | 35 | | /// <c>Microsoft.EntityFrameworkCore</c> — active contexts, queries, SaveChanges and |
| | | 36 | | /// optimistic-concurrency failures (EF Core 9 and later). |
| | | 37 | | /// </item> |
| | | 38 | | /// <item> |
| | | 39 | | /// <c>Npgsql</c> — connection pool usage, command duration and failures: the first |
| | | 40 | | /// place a saturated or unreachable database shows. |
| | | 41 | | /// </item> |
| | | 42 | | /// </list> |
| | | 43 | | /// </remarks> |
| | 2 | 44 | | private static readonly string[] BuiltInMeters = |
| | 2 | 45 | | [ |
| | 2 | 46 | | "System.Runtime", |
| | 2 | 47 | | "Microsoft.AspNetCore.Server.Kestrel", |
| | 2 | 48 | | "Microsoft.EntityFrameworkCore", |
| | 2 | 49 | | "Npgsql" |
| | 2 | 50 | | ]; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Registers the metrics pipeline, or nothing at all when <paramref name="options" /> has |
| | | 54 | | /// metrics switched off — no meter listener, no exporter, no collection cost. |
| | | 55 | | /// </summary> |
| | | 56 | | public static IServiceCollection AddHeimdallMetrics(this IServiceCollection services, MetricsOptions options) |
| | | 57 | | { |
| | 902 | 58 | | if (!options.Enabled) |
| | | 59 | | { |
| | 1 | 60 | | return services; |
| | | 61 | | } |
| | | 62 | | |
| | 901 | 63 | | services.AddOpenTelemetry() |
| | 901 | 64 | | .ConfigureResource(resource => resource.AddService(ServiceName)) |
| | 1802 | 65 | | .WithMetrics(metrics => metrics |
| | 1802 | 66 | | .AddAspNetCoreInstrumentation() |
| | 1802 | 67 | | .AddHttpClientInstrumentation() |
| | 1802 | 68 | | .AddMeter(BuiltInMeters) |
| | 1802 | 69 | | .AddPrometheusExporter()); |
| | | 70 | | |
| | 901 | 71 | | return services; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Mounts the scrape endpoint behind <see cref="MetricsOptions.IsScrapeRequest" />. It must be |
| | | 76 | | /// called before anything else in the pipeline: the endpoint is a terminal branch, so a |
| | | 77 | | /// request it takes never reaches CORS, rate limiting, authentication, authorization or MVC — |
| | | 78 | | /// and, not being a controller action, it never appears in the OpenAPI document either. |
| | | 79 | | /// </summary> |
| | | 80 | | public static IApplicationBuilder UseHeimdallMetrics(this IApplicationBuilder app, MetricsOptions options) |
| | | 81 | | { |
| | 902 | 82 | | if (!options.Enabled) |
| | | 83 | | { |
| | 1 | 84 | | return app; |
| | | 85 | | } |
| | | 86 | | |
| | 901 | 87 | | return app.UseOpenTelemetryPrometheusScrapingEndpoint(options.IsScrapeRequest); |
| | | 88 | | } |
| | | 89 | | } |