Skip to content

Commit 7529346

Browse files
committed
Spike
1 parent 55c5dd1 commit 7529346

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterApplicationBuilderExtensions.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using Microsoft.AspNetCore.Http;
21+
using Microsoft.AspNetCore.Routing;
2122
using Microsoft.Extensions.DependencyInjection;
2223
using OpenTelemetry.Exporter;
2324
using OpenTelemetry.Exporter.Prometheus;
@@ -149,6 +150,52 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
149150
builder.UseMiddleware<PrometheusExporterMiddleware>(meterProvider);
150151
});
151152
}
153+
154+
/// <summary>
155+
/// Adds OpenTelemetry Prometheus scraping endpoint middleware to an
156+
/// <see cref="IEndpointRouteBuilder"/> instance.
157+
/// </summary>
158+
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add
159+
/// middleware to.</param>
160+
/// <param name="path">Optional path to use for the branched pipeline.</param>
161+
/// <param name="meterProvider">Optional <see cref="MeterProvider"/>
162+
/// containing a <see cref="PrometheusExporter"/> otherwise the primary
163+
/// SDK provider will be resolved using application services.</param>
164+
/// <param name="configureBranchedPipeline">Optional callback to
165+
/// configure the branched pipeline. Called before registration of the
166+
/// Prometheus middleware.</param>
167+
/// <returns>A convention routes for the Prometheus scraping endpoint.</returns>
168+
public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(
169+
this IEndpointRouteBuilder endpoints,
170+
string path = null,
171+
MeterProvider meterProvider = null,
172+
Action<IApplicationBuilder> configureBranchedPipeline = null)
173+
{
174+
var builder = endpoints.CreateApplicationBuilder();
175+
176+
// Note: Order is important here. MeterProvider is accessed before
177+
// GetOptions<PrometheusExporterOptions> so that any changes made to
178+
// PrometheusExporterOptions in deferred AddPrometheusExporter
179+
// configure actions are reflected.
180+
meterProvider ??= builder.ApplicationServices.GetRequiredService<MeterProvider>();
181+
182+
if (path == null)
183+
{
184+
var options = builder.ApplicationServices.GetOptions<PrometheusExporterOptions>();
185+
path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
186+
}
187+
188+
if (!path.StartsWith("/"))
189+
{
190+
path = $"/{path}";
191+
}
192+
193+
configureBranchedPipeline?.Invoke(builder);
194+
195+
builder.UseMiddleware<PrometheusExporterMiddleware>(meterProvider);
196+
197+
return endpoints.Map(new PathString(path), builder.Build());
198+
}
152199
}
153200
}
154201
#endif

0 commit comments

Comments
 (0)