|
| 1 | +// <copyright file="PrometheusExporterEndpointRouteBuilderExtensions.cs" company="OpenTelemetry Authors"> |
| 2 | +// Copyright The OpenTelemetry Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +#if NETCOREAPP3_1_OR_GREATER |
| 18 | + |
| 19 | +using System; |
| 20 | +using Microsoft.AspNetCore.Http; |
| 21 | +using Microsoft.AspNetCore.Routing; |
| 22 | +using Microsoft.Extensions.DependencyInjection; |
| 23 | +using OpenTelemetry.Exporter; |
| 24 | +using OpenTelemetry.Exporter.Prometheus; |
| 25 | +using OpenTelemetry.Metrics; |
| 26 | + |
| 27 | +namespace Microsoft.AspNetCore.Builder |
| 28 | +{ |
| 29 | + /// <summary> |
| 30 | + /// Provides extension methods for <see cref="IEndpointRouteBuilder"/> to add |
| 31 | + /// Prometheus scraping endpoint. |
| 32 | + /// </summary> |
| 33 | + public static class PrometheusExporterEndpointRouteBuilderExtensions |
| 34 | + { |
| 35 | + /// <summary> |
| 36 | + /// Adds OpenTelemetry Prometheus scraping endpoint middleware to an |
| 37 | + /// <see cref="IEndpointRouteBuilder"/> instance. |
| 38 | + /// </summary> |
| 39 | + /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add |
| 40 | + /// middleware to.</param> |
| 41 | + /// <param name="path">Optional path to use for the branched pipeline.</param> |
| 42 | + /// <param name="meterProvider">Optional <see cref="MeterProvider"/> |
| 43 | + /// containing a <see cref="PrometheusExporter"/> otherwise the primary |
| 44 | + /// SDK provider will be resolved using application services.</param> |
| 45 | + /// <param name="configureBranchedPipeline">Optional callback to |
| 46 | + /// configure the branched pipeline. Called before registration of the |
| 47 | + /// Prometheus middleware.</param> |
| 48 | + /// <returns>A convention routes for the Prometheus scraping endpoint.</returns> |
| 49 | + public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( |
| 50 | + this IEndpointRouteBuilder endpoints, |
| 51 | + string path = null, |
| 52 | + MeterProvider meterProvider = null, |
| 53 | + Action<IApplicationBuilder> configureBranchedPipeline = null) |
| 54 | + { |
| 55 | + var builder = endpoints.CreateApplicationBuilder(); |
| 56 | + |
| 57 | + // Note: Order is important here. MeterProvider is accessed before |
| 58 | + // GetOptions<PrometheusExporterOptions> so that any changes made to |
| 59 | + // PrometheusExporterOptions in deferred AddPrometheusExporter |
| 60 | + // configure actions are reflected. |
| 61 | + meterProvider ??= builder.ApplicationServices.GetRequiredService<MeterProvider>(); |
| 62 | + |
| 63 | + if (path == null) |
| 64 | + { |
| 65 | + var options = builder.ApplicationServices.GetOptions<PrometheusExporterOptions>(); |
| 66 | + path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath; |
| 67 | + } |
| 68 | + |
| 69 | + if (!path.StartsWith("/")) |
| 70 | + { |
| 71 | + path = $"/{path}"; |
| 72 | + } |
| 73 | + |
| 74 | + configureBranchedPipeline?.Invoke(builder); |
| 75 | + |
| 76 | + builder.UseMiddleware<PrometheusExporterMiddleware>(meterProvider); |
| 77 | + |
| 78 | + return endpoints.Map(new PathString(path), builder.Build()); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +#endif |
0 commit comments