Skip to content

Commit e826f9d

Browse files
authored
[Exporter.PrometheusAspNetCore] update Public API (#3717)
* Refactor prometheus exporter options.
1 parent 6b38ed7 commit e826f9d

14 files changed

+98
-54
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions
22
Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions
3-
OpenTelemetry.Exporter.PrometheusExporterOptions
4-
OpenTelemetry.Exporter.PrometheusExporterOptions.PrometheusExporterOptions() -> void
5-
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeEndpointPath.get -> string
6-
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeEndpointPath.set -> void
7-
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
8-
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.set -> void
3+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions
4+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.PrometheusAspNetCoreOptions() -> void
5+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.get -> string
6+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.set -> void
7+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
8+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeResponseCacheDurationMilliseconds.set -> void
99
OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions
1010
static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
1111
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
@@ -15,5 +15,5 @@ static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtens
1515
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
1616
static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder
1717
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
18-
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.PrometheusExporterOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
19-
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.PrometheusExporterOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
18+
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.PrometheusAspNetCoreOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
19+
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.PrometheusAspNetCoreOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
## Unreleased
44

5+
* Changed configuration class name from `PrometheusExporterOptions`
6+
to `PrometheusAspNetCoreOptions`
7+
([#3717](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3717))
8+
59
## 1.4.0-beta.1
610

711
Released 2022-Sep-29
812

913
* Bug fix for Prometheus Exporter reporting StatusCode 204
10-
instead of 200, when no metrics are collected
11-
([#3643](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3643))
14+
instead of 200, when no metrics are collected
15+
([#3643](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3643))
1216
* Added overloads which accept a name to the `MeterProviderBuilder`
1317
`AddPrometheusExporter` extension to allow for more fine-grained options
1418
management

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusCollectionManager.cs" Link="Includes/PrometheusCollectionManager.cs" />
2222
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporter.cs" Link="Includes/PrometheusExporter.cs" />
2323
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporterEventSource.cs" Link="Includes/PrometheusExporterEventSource.cs" />
24+
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporterOptions.cs" Link="Includes/PrometheusExporterOptions.cs" />
2425
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializer.cs" Link="Includes/PrometheusSerializer.cs" />
2526
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializerExt.cs" Link="Includes/PrometheusSerializerExt.cs" />
2627
</ItemGroup>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <copyright file="PrometheusAspNetCoreOptions.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+
using OpenTelemetry.Exporter.Prometheus;
18+
19+
namespace OpenTelemetry.Exporter
20+
{
21+
/// <summary>
22+
/// Prometheus exporter options.
23+
/// </summary>
24+
public class PrometheusAspNetCoreOptions
25+
{
26+
internal const string DefaultScrapeEndpointPath = "/metrics";
27+
28+
/// <summary>
29+
/// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics".
30+
/// </summary>
31+
public string ScrapeEndpointPath { get; set; } = DefaultScrapeEndpointPath;
32+
33+
/// <summary>
34+
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
35+
/// </summary>
36+
/// <remarks>
37+
/// Note: Specify 0 to disable response caching.
38+
/// </remarks>
39+
public int ScrapeResponseCacheDurationMilliseconds
40+
{
41+
get => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds;
42+
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
43+
}
44+
45+
internal PrometheusExporterOptions ExporterOptions { get; } = new();
46+
}
47+
}

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static class PrometheusExporterApplicationBuilderExtensions
3636
/// </summary>
3737
/// <remarks>Note: A branched pipeline is created for the route
3838
/// specified by <see
39-
/// cref="PrometheusExporterOptions.ScrapeEndpointPath"/>.</remarks>
39+
/// cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/>.</remarks>
4040
/// <param name="app">The <see cref="IApplicationBuilder"/> to add
4141
/// middleware to.</param>
4242
/// <returns>A reference to the original <see
@@ -86,7 +86,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi
8686
/// <remarks>Note: A branched pipeline is created based on the <paramref
8787
/// name="predicate"/> or <paramref name="path"/>. If neither <paramref
8888
/// name="predicate"/> nor <paramref name="path"/> are provided then
89-
/// <see cref="PrometheusExporterOptions.ScrapeEndpointPath"/> is
89+
/// <see cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/> is
9090
/// used.</remarks>
9191
/// <param name="app">The <see cref="IApplicationBuilder"/> to add
9292
/// middleware to.</param>
@@ -111,18 +111,18 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
111111
Action<IApplicationBuilder> configureBranchedPipeline)
112112
{
113113
// Note: Order is important here. MeterProvider is accessed before
114-
// GetOptions<PrometheusExporterOptions> so that any changes made to
115-
// PrometheusExporterOptions in deferred AddPrometheusExporter
114+
// GetOptions<PrometheusAspNetCoreOptions> so that any changes made to
115+
// PrometheusAspNetCoreOptions in deferred AddPrometheusExporter
116116
// configure actions are reflected.
117117
meterProvider ??= app.ApplicationServices.GetRequiredService<MeterProvider>();
118118

119119
if (predicate == null)
120120
{
121121
if (path == null)
122122
{
123-
var options = app.ApplicationServices.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;
123+
var options = app.ApplicationServices.GetRequiredService<IOptions<PrometheusAspNetCoreOptions>>().Value;
124124

125-
path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
125+
path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath;
126126
}
127127

128128
if (!path.StartsWith("/"))

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static class PrometheusExporterEndpointRouteBuilderExtensions
3737
/// </summary>
3838
/// <remarks>Note: A branched pipeline is created for the route
3939
/// specified by <see
40-
/// cref="PrometheusExporterOptions.ScrapeEndpointPath"/>.</remarks>
40+
/// cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/>.</remarks>
4141
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add
4242
/// middleware to.</param>
4343
/// <returns>A convention routes for the Prometheus scraping endpoint.</returns>
@@ -65,7 +65,7 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEnd
6565
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add
6666
/// middleware to.</param>
6767
/// <param name="path">Optional path to use for the branched pipeline.
68-
/// If not provided then <see cref="PrometheusExporterOptions.ScrapeEndpointPath"/>
68+
/// If not provided then <see cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/>
6969
/// is used.</param>
7070
/// <param name="meterProvider">Optional <see cref="MeterProvider"/>
7171
/// containing a Prometheus exporter otherwise the primary SDK provider
@@ -83,16 +83,16 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(
8383
var builder = endpoints.CreateApplicationBuilder();
8484

8585
// Note: Order is important here. MeterProvider is accessed before
86-
// GetOptions<PrometheusExporterOptions> so that any changes made to
87-
// PrometheusExporterOptions in deferred AddPrometheusExporter
86+
// GetOptions<PrometheusAspNetCoreOptions> so that any changes made to
87+
// PrometheusAspNetCoreOptions in deferred AddPrometheusExporter
8888
// configure actions are reflected.
8989
meterProvider ??= endpoints.ServiceProvider.GetRequiredService<MeterProvider>();
9090

9191
if (path == null)
9292
{
93-
var options = endpoints.ServiceProvider.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;
93+
var options = endpoints.ServiceProvider.GetRequiredService<IOptions<PrometheusAspNetCoreOptions>>().Value;
9494

95-
path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
95+
path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath;
9696
}
9797

9898
if (!path.StartsWith("/"))

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@ public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuild
4040
/// Adds <see cref="PrometheusExporter"/> to the <see cref="MeterProviderBuilder"/>.
4141
/// </summary>
4242
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
43-
/// <param name="configure">Callback action for configuring <see cref="PrometheusExporterOptions"/>.</param>
43+
/// <param name="configure">Callback action for configuring <see cref="PrometheusAspNetCoreOptions"/>.</param>
4444
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
4545
public static MeterProviderBuilder AddPrometheusExporter(
4646
this MeterProviderBuilder builder,
47-
Action<PrometheusExporterOptions> configure)
47+
Action<PrometheusAspNetCoreOptions> configure)
4848
=> AddPrometheusExporter(builder, name: null, configure);
4949

5050
/// <summary>
5151
/// Adds <see cref="PrometheusExporter"/> to the <see cref="MeterProviderBuilder"/>.
5252
/// </summary>
5353
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
5454
/// <param name="name">Name which is used when retrieving options.</param>
55-
/// <param name="configure">Callback action for configuring <see cref="PrometheusExporterOptions"/>.</param>
55+
/// <param name="configure">Callback action for configuring <see cref="PrometheusAspNetCoreOptions"/>.</param>
5656
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
5757
public static MeterProviderBuilder AddPrometheusExporter(
5858
this MeterProviderBuilder builder,
5959
string name,
60-
Action<PrometheusExporterOptions> configure)
60+
Action<PrometheusAspNetCoreOptions> configure)
6161
{
6262
Guard.ThrowIfNull(builder);
6363

@@ -70,15 +70,15 @@ public static MeterProviderBuilder AddPrometheusExporter(
7070

7171
return builder.ConfigureBuilder((sp, builder) =>
7272
{
73-
var options = sp.GetRequiredService<IOptionsMonitor<PrometheusExporterOptions>>().Get(name);
73+
var options = sp.GetRequiredService<IOptionsMonitor<PrometheusAspNetCoreOptions>>().Get(name);
7474

7575
AddPrometheusExporter(builder, options);
7676
});
7777
}
7878

79-
private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusExporterOptions options)
79+
private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusAspNetCoreOptions options)
8080
{
81-
var exporter = new PrometheusExporter(scrapeResponseCacheDurationMilliseconds: options.ScrapeResponseCacheDurationMilliseconds);
81+
var exporter = new PrometheusExporter(options.ExporterOptions);
8282

8383
var reader = new BaseExportingMetricReader(exporter)
8484
{

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ dotnet add package --prerelease OpenTelemetry.Exporter.Prometheus.AspNetCore
6868

6969
## Configuration
7070

71-
The `PrometheusExporter` can be configured using the `PrometheusExporterOptions`
71+
The `PrometheusExporter` can be configured using the `PrometheusAspNetCoreOptions`
7272
properties.
7373

7474
### ScrapeEndpointPath

src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ internal sealed class PrometheusExporter : BaseExporter<Metric>, IPullMetricExpo
3333
/// <summary>
3434
/// Initializes a new instance of the <see cref="PrometheusExporter"/> class.
3535
/// </summary>
36-
/// <param name="scrapeResponseCacheDurationMilliseconds">
37-
/// The cache duration in milliseconds for scrape responses. Default value: 0.
38-
/// </param>
39-
public PrometheusExporter(int scrapeResponseCacheDurationMilliseconds = 0)
36+
/// <param name="options"><see cref="PrometheusExporterOptions"/>.</param>
37+
public PrometheusExporter(PrometheusExporterOptions options)
4038
{
41-
Guard.ThrowIfOutOfRange(scrapeResponseCacheDurationMilliseconds, min: 0);
39+
Guard.ThrowIfNull(options);
40+
41+
this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;
4242

43-
this.ScrapeResponseCacheDurationMilliseconds = scrapeResponseCacheDurationMilliseconds;
4443
this.CollectionManager = new PrometheusCollectionManager(this);
4544
}
4645

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterOptions.cs renamed to src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporterOptions.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,15 @@
1616

1717
using OpenTelemetry.Internal;
1818

19-
namespace OpenTelemetry.Exporter
19+
namespace OpenTelemetry.Exporter.Prometheus
2020
{
2121
/// <summary>
2222
/// Prometheus exporter options.
2323
/// </summary>
24-
public class PrometheusExporterOptions
24+
internal sealed class PrometheusExporterOptions
2525
{
26-
internal const string DefaultScrapeEndpointPath = "/metrics";
27-
2826
private int scrapeResponseCacheDurationMilliseconds = 300;
2927

30-
/// <summary>
31-
/// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics".
32-
/// </summary>
33-
public string ScrapeEndpointPath { get; set; } = DefaultScrapeEndpointPath;
34-
3528
/// <summary>
3629
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
3730
/// </summary>

0 commit comments

Comments
 (0)