Skip to content

Commit b6af4dd

Browse files
committed
Revision
1 parent 7529346 commit b6af4dd

File tree

2 files changed

+82
-46
lines changed

2 files changed

+82
-46
lines changed

src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterApplicationBuilderExtensions.cs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -150,52 +150,6 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
150150
builder.UseMiddleware<PrometheusExporterMiddleware>(meterProvider);
151151
});
152152
}
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-
}
199153
}
200154
}
201155
#endif
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)