From 04cf0b2a6dba9ac54a6355c5732ec422ed5ec10e Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 18 Oct 2022 10:44:22 -0700 Subject: [PATCH 1/2] Support named options in Prometheus AspNetCore pipeline extensions. --- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 4 +- .../CHANGELOG.md | 4 ++ ...eusExporterApplicationBuilderExtensions.cs | 13 ++++-- ...sExporterEndpointRouteBuilderExtensions.cs | 15 +++--- .../PrometheusExporterMiddlewareTests.cs | 46 +++++++++++++++++-- 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt index 99bf429263..0dd120b12d 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -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 predicate, string path, System.Action configureBranchedPipeline) -> Microsoft.AspNetCore.Builder.IApplicationBuilder +static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, OpenTelemetry.Metrics.MeterProvider meterProvider, System.Func predicate, string path, System.Action 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 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 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 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 configure) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md index 487621c251..4c54ccc50d 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Support named options in `UseOpenTelemetryPrometheusScrapingEndpoint` & + `MapPrometheusScrapingEndpoint` extensions + ([#XXXX](https://github.com/open-telemetry/opentelemetry-dotnet/pull/XXXX)) + ## 1.4.0-beta.2 Released 2022-Oct-17 diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs index 91678fa0ef..d3bb6666a6 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs @@ -42,7 +42,7 @@ public static class PrometheusExporterApplicationBuilderExtensions /// A reference to the original for chaining calls. 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); /// /// Adds OpenTelemetry Prometheus scraping endpoint middleware to an @@ -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); } /// @@ -76,7 +76,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(this IApplicationBuilder app, Func 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); } /// @@ -101,6 +101,8 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi /// Optional callback to /// configure the branched pipeline. Called before registration of the /// Prometheus middleware. + /// Optional name used to retrieve . /// A reference to the original for chaining calls. public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint( @@ -108,7 +110,8 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint( MeterProvider meterProvider, Func predicate, string path, - Action configureBranchedPipeline) + Action configureBranchedPipeline, + string optionsName) { // Note: Order is important here. MeterProvider is accessed before // GetOptions so that any changes made to @@ -120,7 +123,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint( { if (path == null) { - var options = app.ApplicationServices.GetRequiredService>().Value; + var options = app.ApplicationServices.GetRequiredService>().Get(optionsName ?? Options.DefaultName); path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath; } diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs index 95546da47b..39e043afdf 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs @@ -42,7 +42,7 @@ public static class PrometheusExporterEndpointRouteBuilderExtensions /// middleware to. /// A convention routes for the Prometheus scraping endpoint. 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); /// /// Adds OpenTelemetry Prometheus scraping endpoint middleware to an @@ -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); } /// @@ -73,12 +73,15 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEnd /// Optional callback to /// configure the branched pipeline. Called before registration of the /// Prometheus middleware. + /// Optional name used to retrieve . /// A convention routes for the Prometheus scraping endpoint. public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( this IEndpointRouteBuilder endpoints, - string path = null, - MeterProvider meterProvider = null, - Action configureBranchedPipeline = null) + string path, + MeterProvider meterProvider, + Action configureBranchedPipeline, + string optionsName) { var builder = endpoints.CreateApplicationBuilder(); @@ -90,7 +93,7 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( if (path == null) { - var options = endpoints.ServiceProvider.GetRequiredService>().Value; + var options = endpoints.ServiceProvider.GetRequiredService>().Get(optionsName ?? Options.DefaultName); path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath; } diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index 88d18e3209..8a6fb6eca9 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -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("myOptions", o => o.ScrapeEndpointPath = "/metrics_override"); + }); + } + [Fact] public Task PrometheusExporterMiddlewareIntegration_Predicate() { @@ -102,7 +119,8 @@ public Task PrometheusExporterMiddlewareIntegration_MixedPredicateAndPath() { context.Response.Headers.Add("X-MiddlewareExecuted", "true"); return next(); - })), + }), + optionsName: null), services => services.Configure(o => o.ScrapeEndpointPath = "/metrics_options"), validateResponse: rsp => { @@ -128,7 +146,8 @@ public Task PrometheusExporterMiddlewareIntegration_MixedPath() { context.Response.Headers.Add("X-MiddlewareExecuted", "true"); return next(); - })), + }), + optionsName: null), services => services.Configure(o => o.ScrapeEndpointPath = "/metrics_options"), validateResponse: rsp => { @@ -155,7 +174,8 @@ await RunPrometheusExporterMiddlewareIntegrationTest( meterProvider: meterProvider, predicate: null, path: null, - configureBranchedPipeline: null), + configureBranchedPipeline: null, + optionsName: null), registerMeterProvider: false).ConfigureAwait(false); } @@ -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("myOptions", o => o.ScrapeEndpointPath = "/metrics_path"); + }); + } + [Fact] public async Task PrometheusExporterMiddlewareIntegration_MapEndpoint_WithMeterProvider() { @@ -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); } From c74fe9753f718038d350229f3629ecf36f585799 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 18 Oct 2022 10:48:51 -0700 Subject: [PATCH 2/2] Patch CHANGELOG. --- src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md index 4c54ccc50d..89f34d1892 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md @@ -4,7 +4,7 @@ * Support named options in `UseOpenTelemetryPrometheusScrapingEndpoint` & `MapPrometheusScrapingEndpoint` extensions - ([#XXXX](https://github.com/open-telemetry/opentelemetry-dotnet/pull/XXXX)) + ([#3780](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3780)) ## 1.4.0-beta.2