Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prometheus AspNetCore] Support named options in pipeline extensions #3780

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeResponseCacheDurationMi
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeResponseCacheDurationMilliseconds.set -> void
OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions
static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, OpenTelemetry.Metrics.MeterProvider meterProvider, System.Func<Microsoft.AspNetCore.Http.HttpContext, bool> predicate, string path, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configureBranchedPipeline) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, OpenTelemetry.Metrics.MeterProvider meterProvider, System.Func<Microsoft.AspNetCore.Http.HttpContext, bool> predicate, string path, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configureBranchedPipeline, string optionsName) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, string path) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, System.Func<Microsoft.AspNetCore.Http.HttpContext, bool> predicate) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path = null, OpenTelemetry.Metrics.MeterProvider meterProvider = null, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configureBranchedPipeline = null) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder
static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path, OpenTelemetry.Metrics.MeterProvider meterProvider, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configureBranchedPipeline, string optionsName) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.PrometheusAspNetCoreOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.PrometheusAspNetCoreOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Support named options in `UseOpenTelemetryPrometheusScrapingEndpoint` &
`MapPrometheusScrapingEndpoint` extensions
([#3780](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3780))

## 1.4.0-beta.2

Released 2022-Oct-17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class PrometheusExporterApplicationBuilderExtensions
/// <returns>A reference to the original <see
/// cref="IApplicationBuilder"/> for chaining calls.</returns>
public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(this IApplicationBuilder app)
=> UseOpenTelemetryPrometheusScrapingEndpoint(app, meterProvider: null, predicate: null, path: null, configureBranchedPipeline: null);
=> UseOpenTelemetryPrometheusScrapingEndpoint(app, meterProvider: null, predicate: null, path: null, configureBranchedPipeline: null, optionsName: null);

/// <summary>
/// Adds OpenTelemetry Prometheus scraping endpoint middleware to an
Expand All @@ -58,7 +58,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi
public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(this IApplicationBuilder app, string path)
{
Guard.ThrowIfNull(path);
return UseOpenTelemetryPrometheusScrapingEndpoint(app, meterProvider: null, predicate: null, path: path, configureBranchedPipeline: null);
return UseOpenTelemetryPrometheusScrapingEndpoint(app, meterProvider: null, predicate: null, path: path, configureBranchedPipeline: null, optionsName: null);
}

/// <summary>
Expand All @@ -76,7 +76,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi
public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(this IApplicationBuilder app, Func<HttpContext, bool> predicate)
{
Guard.ThrowIfNull(predicate);
return UseOpenTelemetryPrometheusScrapingEndpoint(app, meterProvider: null, predicate: predicate, path: null, configureBranchedPipeline: null);
return UseOpenTelemetryPrometheusScrapingEndpoint(app, meterProvider: null, predicate: predicate, path: null, configureBranchedPipeline: null, optionsName: null);
}

/// <summary>
Expand All @@ -101,14 +101,17 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi
/// <param name="configureBranchedPipeline">Optional callback to
/// configure the branched pipeline. Called before registration of the
/// Prometheus middleware.</param>
/// <param name="optionsName">Optional name used to retrieve <see
/// cref="PrometheusAspNetCoreOptions"/>.</param>
/// <returns>A reference to the original <see
/// cref="IApplicationBuilder"/> for chaining calls.</returns>
public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
this IApplicationBuilder app,
MeterProvider meterProvider,
Func<HttpContext, bool> predicate,
string path,
Action<IApplicationBuilder> configureBranchedPipeline)
Action<IApplicationBuilder> configureBranchedPipeline,
string optionsName)
{
// Note: Order is important here. MeterProvider is accessed before
// GetOptions<PrometheusAspNetCoreOptions> so that any changes made to
Expand All @@ -120,7 +123,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
{
if (path == null)
{
var options = app.ApplicationServices.GetRequiredService<IOptions<PrometheusAspNetCoreOptions>>().Value;
var options = app.ApplicationServices.GetRequiredService<IOptionsMonitor<PrometheusAspNetCoreOptions>>().Get(optionsName ?? Options.DefaultName);

path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class PrometheusExporterEndpointRouteBuilderExtensions
/// middleware to.</param>
/// <returns>A convention routes for the Prometheus scraping endpoint.</returns>
public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEndpointRouteBuilder endpoints)
=> MapPrometheusScrapingEndpoint(endpoints, path: null, meterProvider: null, configureBranchedPipeline: null);
=> MapPrometheusScrapingEndpoint(endpoints, path: null, meterProvider: null, configureBranchedPipeline: null, optionsName: null);

/// <summary>
/// Adds OpenTelemetry Prometheus scraping endpoint middleware to an
Expand All @@ -55,7 +55,7 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEnd
public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEndpointRouteBuilder endpoints, string path)
{
Guard.ThrowIfNull(path);
return MapPrometheusScrapingEndpoint(endpoints, path, meterProvider: null, configureBranchedPipeline: null);
return MapPrometheusScrapingEndpoint(endpoints, path, meterProvider: null, configureBranchedPipeline: null, optionsName: null);
}

/// <summary>
Expand All @@ -73,12 +73,15 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEnd
/// <param name="configureBranchedPipeline">Optional callback to
/// configure the branched pipeline. Called before registration of the
/// Prometheus middleware.</param>
/// <param name="optionsName">Optional name used to retrieve <see
/// cref="PrometheusAspNetCoreOptions"/>.</param>
/// <returns>A convention routes for the Prometheus scraping endpoint.</returns>
public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(
this IEndpointRouteBuilder endpoints,
string path = null,
MeterProvider meterProvider = null,
Action<IApplicationBuilder> configureBranchedPipeline = null)
string path,
MeterProvider meterProvider,
Action<IApplicationBuilder> configureBranchedPipeline,
string optionsName)
{
var builder = endpoints.CreateApplicationBuilder();

Expand All @@ -90,7 +93,7 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(

if (path == null)
{
var options = endpoints.ServiceProvider.GetRequiredService<IOptions<PrometheusAspNetCoreOptions>>().Value;
var options = endpoints.ServiceProvider.GetRequiredService<IOptionsMonitor<PrometheusAspNetCoreOptions>>().Get(optionsName ?? Options.DefaultName);

path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public Task PrometheusExporterMiddlewareIntegration_PathOverride()
app => app.UseOpenTelemetryPrometheusScrapingEndpoint("/metrics_override"));
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_WithPathNamedOptionsOverride()
{
return RunPrometheusExporterMiddlewareIntegrationTest(
"/metrics_override",
app => app.UseOpenTelemetryPrometheusScrapingEndpoint(
meterProvider: null,
predicate: null,
path: null,
configureBranchedPipeline: null,
optionsName: "myOptions"),
services =>
{
services.Configure<PrometheusAspNetCoreOptions>("myOptions", o => o.ScrapeEndpointPath = "/metrics_override");
});
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_Predicate()
{
Expand All @@ -102,7 +119,8 @@ public Task PrometheusExporterMiddlewareIntegration_MixedPredicateAndPath()
{
context.Response.Headers.Add("X-MiddlewareExecuted", "true");
return next();
})),
}),
optionsName: null),
services => services.Configure<PrometheusAspNetCoreOptions>(o => o.ScrapeEndpointPath = "/metrics_options"),
validateResponse: rsp =>
{
Expand All @@ -128,7 +146,8 @@ public Task PrometheusExporterMiddlewareIntegration_MixedPath()
{
context.Response.Headers.Add("X-MiddlewareExecuted", "true");
return next();
})),
}),
optionsName: null),
services => services.Configure<PrometheusAspNetCoreOptions>(o => o.ScrapeEndpointPath = "/metrics_options"),
validateResponse: rsp =>
{
Expand All @@ -155,7 +174,8 @@ await RunPrometheusExporterMiddlewareIntegrationTest(
meterProvider: meterProvider,
predicate: null,
path: null,
configureBranchedPipeline: null),
configureBranchedPipeline: null,
optionsName: null),
registerMeterProvider: false).ConfigureAwait(false);
}

Expand Down Expand Up @@ -186,6 +206,23 @@ public Task PrometheusExporterMiddlewareIntegration_MapEndpoint_WithPathOverride
services => services.AddRouting());
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_MapEndpoint_WithPathNamedOptionsOverride()
{
return RunPrometheusExporterMiddlewareIntegrationTest(
"/metrics_path",
app => app.UseRouting().UseEndpoints(builder => builder.MapPrometheusScrapingEndpoint(
path: null,
meterProvider: null,
configureBranchedPipeline: null,
optionsName: "myOptions")),
services =>
{
services.AddRouting();
services.Configure<PrometheusAspNetCoreOptions>("myOptions", o => o.ScrapeEndpointPath = "/metrics_path");
});
}

[Fact]
public async Task PrometheusExporterMiddlewareIntegration_MapEndpoint_WithMeterProvider()
{
Expand All @@ -199,7 +236,8 @@ await RunPrometheusExporterMiddlewareIntegrationTest(
app => app.UseRouting().UseEndpoints(builder => builder.MapPrometheusScrapingEndpoint(
path: null,
meterProvider: meterProvider,
configureBranchedPipeline: null)),
configureBranchedPipeline: null,
optionsName: null)),
services => services.AddRouting(),
registerMeterProvider: false).ConfigureAwait(false);
}
Expand Down