feat(telemetry): MapZbMetrics Prometheus scrape endpoint

This commit is contained in:
Joseph Doherty
2026-06-01 07:34:26 -04:00
parent 4126e1df54
commit 3e4d4369bf
2 changed files with 60 additions and 0 deletions
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
namespace ZB.MOM.WW.Telemetry;
/// <summary>
/// Endpoint extension for mounting the Prometheus <c>/metrics</c> scrape endpoint.
/// </summary>
public static class ZbMetricsEndpointExtensions
{
/// <summary>
/// Mounts the Prometheus <c>/metrics</c> endpoint. Only valid when
/// <see cref="ZbTelemetryOptions.Exporter"/> = <see cref="ZbExporter.Prometheus"/>.
/// Call after <c>app.UseRouting()</c>.
/// </summary>
/// <param name="endpoints">The endpoint route builder.</param>
public static IEndpointConventionBuilder MapZbMetrics(this IEndpointRouteBuilder endpoints)
{
ArgumentNullException.ThrowIfNull(endpoints);
return endpoints.MapPrometheusScrapingEndpoint();
}
}
@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using ZB.MOM.WW.Telemetry;
namespace ZB.MOM.WW.Telemetry.Tests;
public sealed class MapZbMetricsTests
{
[Fact]
public async Task MapZbMetrics_ServesPrometheusEndpoint()
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.AddZbTelemetry(o =>
{
o.ServiceName = "t";
o.Exporter = ZbExporter.Prometheus;
o.Meters = ["Test.Meter"];
});
await using var app = builder.Build();
app.MapZbMetrics();
await app.StartAsync();
var address = app.Urls.First();
using var client = new HttpClient { BaseAddress = new Uri(address) };
var response = await client.GetAsync("/metrics");
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("text/plain", response.Content.Headers.ContentType!.MediaType);
await app.StopAsync();
}
}