|
| 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.Internal; |
| 26 | +using OpenTelemetry.Metrics; |
| 27 | + |
| 28 | +namespace Microsoft.AspNetCore.Builder |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// Provides extension methods for <see cref="IEndpointRouteBuilder"/> to add |
| 32 | + /// Prometheus scraping endpoint. |
| 33 | + /// </summary> |
| 34 | + public static class PrometheusExporterEndpointRouteBuilderExtensions |
| 35 | + { |
| 36 | + /// <summary> |
| 37 | + /// Adds OpenTelemetry Prometheus scraping endpoint middleware to an |
| 38 | + /// <see cref="IEndpointRouteBuilder"/> instance. |
| 39 | + /// </summary> |
| 40 | + /// <remarks>Note: A branched pipeline is created for the route |
| 41 | + /// specified by <see |
| 42 | + /// cref="PrometheusExporterOptions.ScrapeEndpointPath"/>.</remarks> |
| 43 | + /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add |
| 44 | + /// middleware to.</param> |
| 45 | + /// <returns>A convention routes for the Prometheus scraping endpoint.</returns> |
| 46 | + public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEndpointRouteBuilder endpoints) |
| 47 | + => MapPrometheusScrapingEndpoint(endpoints, path: null, meterProvider: null, configureBranchedPipeline: null); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Adds OpenTelemetry Prometheus scraping endpoint middleware to an |
| 51 | + /// <see cref="IEndpointRouteBuilder"/> instance. |
| 52 | + /// </summary> |
| 53 | + /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add |
| 54 | + /// middleware to.</param> |
| 55 | + /// <param name="path">The path to use for the branched pipeline.</param> |
| 56 | + /// <returns>A convention routes for the Prometheus scraping endpoint.</returns> |
| 57 | + public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEndpointRouteBuilder endpoints, string path) |
| 58 | + { |
| 59 | + Guard.ThrowIfNull(path); |
| 60 | + return MapPrometheusScrapingEndpoint(endpoints, path, meterProvider: null, configureBranchedPipeline: null); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Adds OpenTelemetry Prometheus scraping endpoint middleware to an |
| 65 | + /// <see cref="IEndpointRouteBuilder"/> instance. |
| 66 | + /// </summary> |
| 67 | + /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add |
| 68 | + /// middleware to.</param> |
| 69 | + /// <param name="path">Optional path to use for the branched pipeline. |
| 70 | + /// If not provided then <see cref="PrometheusExporterOptions.ScrapeEndpointPath"/> |
| 71 | + /// is used.</param> |
| 72 | + /// <param name="meterProvider">Optional <see cref="MeterProvider"/> |
| 73 | + /// containing a <see cref="PrometheusExporter"/> otherwise the primary |
| 74 | + /// SDK provider will be resolved using application services.</param> |
| 75 | + /// <param name="configureBranchedPipeline">Optional callback to |
| 76 | + /// configure the branched pipeline. Called before registration of the |
| 77 | + /// Prometheus middleware.</param> |
| 78 | + /// <returns>A convention routes for the Prometheus scraping endpoint.</returns> |
| 79 | + public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( |
| 80 | + this IEndpointRouteBuilder endpoints, |
| 81 | + string path = null, |
| 82 | + MeterProvider meterProvider = null, |
| 83 | + Action<IApplicationBuilder> configureBranchedPipeline = null) |
| 84 | + { |
| 85 | + var builder = endpoints.CreateApplicationBuilder(); |
| 86 | + |
| 87 | + // Note: Order is important here. MeterProvider is accessed before |
| 88 | + // GetOptions<PrometheusExporterOptions> so that any changes made to |
| 89 | + // PrometheusExporterOptions in deferred AddPrometheusExporter |
| 90 | + // configure actions are reflected. |
| 91 | + meterProvider ??= endpoints.ServiceProvider.GetRequiredService<MeterProvider>(); |
| 92 | + |
| 93 | + if (path == null) |
| 94 | + { |
| 95 | + var options = endpoints.ServiceProvider.GetOptions<PrometheusExporterOptions>(); |
| 96 | + path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath; |
| 97 | + } |
| 98 | + |
| 99 | + if (!path.StartsWith("/")) |
| 100 | + { |
| 101 | + path = $"/{path}"; |
| 102 | + } |
| 103 | + |
| 104 | + configureBranchedPipeline?.Invoke(builder); |
| 105 | + |
| 106 | + builder.UseMiddleware<PrometheusExporterMiddleware>(meterProvider); |
| 107 | + |
| 108 | + return endpoints.Map(new PathString(path), builder.Build()); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +#endif |
0 commit comments