Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exporter.PrometheusAspNetCore] update Public API #3717

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions
Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions
OpenTelemetry.Exporter.PrometheusExporterOptions
OpenTelemetry.Exporter.PrometheusExporterOptions.PrometheusExporterOptions() -> void
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeEndpointPath.get -> string
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeEndpointPath.set -> void
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
OpenTelemetry.Exporter.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.PrometheusAspNetCoreOptions() -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.get -> string
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
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<Microsoft.AspNetCore.Http.HttpContext, bool> predicate, string path, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configureBranchedPipeline) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
Expand All @@ -15,5 +15,5 @@ static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtens
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
static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path) -> 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<OpenTelemetry.Exporter.PrometheusExporterOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.PrometheusExporterOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.PrometheusAspNetCoreOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.PrometheusAspNetCoreOptions> configure) -> OpenTelemetry.Metrics.MeterProviderBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

## Unreleased

* Changed configuration class name from `PrometheusExporterOptions`
to `PrometheusAspNetCoreOptions`
([#3717](https:/open-telemetry/opentelemetry-dotnet/pull/3717))

## 1.4.0-beta.1

Released 2022-Sep-29

* Bug fix for Prometheus Exporter reporting StatusCode 204
instead of 200, when no metrics are collected
([#3643](https:/open-telemetry/opentelemetry-dotnet/pull/3643))
instead of 200, when no metrics are collected
([#3643](https:/open-telemetry/opentelemetry-dotnet/pull/3643))
* Added overloads which accept a name to the `MeterProviderBuilder`
`AddPrometheusExporter` extension to allow for more fine-grained options
management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusCollectionManager.cs" Link="Includes/PrometheusCollectionManager.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporter.cs" Link="Includes/PrometheusExporter.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporterEventSource.cs" Link="Includes/PrometheusExporterEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporterOptions.cs" Link="Includes/PrometheusExporterOptions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializer.cs" Link="Includes/PrometheusSerializer.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializerExt.cs" Link="Includes/PrometheusSerializerExt.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <copyright file="PrometheusAspNetCoreOptions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using OpenTelemetry.Exporter.Prometheus;

namespace OpenTelemetry.Exporter
{
/// <summary>
/// Prometheus exporter options.
/// </summary>
public class PrometheusAspNetCoreOptions
{
internal const string DefaultScrapeEndpointPath = "/metrics";

/// <summary>
/// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics".
/// </summary>
public string ScrapeEndpointPath { get; set; } = DefaultScrapeEndpointPath;

/// <summary>
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
/// </summary>
/// <remarks>
/// Note: Specify 0 to disable response caching.
/// </remarks>
public int ScrapeResponseCacheDurationMilliseconds
{
get => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds;
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
}

internal PrometheusExporterOptions ExporterOptions { get; } = new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class PrometheusExporterApplicationBuilderExtensions
/// </summary>
/// <remarks>Note: A branched pipeline is created for the route
/// specified by <see
/// cref="PrometheusExporterOptions.ScrapeEndpointPath"/>.</remarks>
/// cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/>.</remarks>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add
/// middleware to.</param>
/// <returns>A reference to the original <see
Expand Down Expand Up @@ -86,7 +86,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(thi
/// <remarks>Note: A branched pipeline is created based on the <paramref
/// name="predicate"/> or <paramref name="path"/>. If neither <paramref
/// name="predicate"/> nor <paramref name="path"/> are provided then
/// <see cref="PrometheusExporterOptions.ScrapeEndpointPath"/> is
/// <see cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/> is
/// used.</remarks>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add
/// middleware to.</param>
Expand All @@ -111,18 +111,18 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
Action<IApplicationBuilder> configureBranchedPipeline)
{
// Note: Order is important here. MeterProvider is accessed before
// GetOptions<PrometheusExporterOptions> so that any changes made to
// PrometheusExporterOptions in deferred AddPrometheusExporter
// GetOptions<PrometheusAspNetCoreOptions> so that any changes made to
// PrometheusAspNetCoreOptions in deferred AddPrometheusExporter
// configure actions are reflected.
meterProvider ??= app.ApplicationServices.GetRequiredService<MeterProvider>();

if (predicate == null)
{
if (path == null)
{
var options = app.ApplicationServices.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;
var options = app.ApplicationServices.GetRequiredService<IOptions<PrometheusAspNetCoreOptions>>().Value;

path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath;
}

if (!path.StartsWith("/"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class PrometheusExporterEndpointRouteBuilderExtensions
/// </summary>
/// <remarks>Note: A branched pipeline is created for the route
/// specified by <see
/// cref="PrometheusExporterOptions.ScrapeEndpointPath"/>.</remarks>
/// cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/>.</remarks>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add
/// middleware to.</param>
/// <returns>A convention routes for the Prometheus scraping endpoint.</returns>
Expand Down Expand Up @@ -65,7 +65,7 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(this IEnd
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add
/// middleware to.</param>
/// <param name="path">Optional path to use for the branched pipeline.
/// If not provided then <see cref="PrometheusExporterOptions.ScrapeEndpointPath"/>
/// If not provided then <see cref="PrometheusAspNetCoreOptions.ScrapeEndpointPath"/>
/// is used.</param>
/// <param name="meterProvider">Optional <see cref="MeterProvider"/>
/// containing a Prometheus exporter otherwise the primary SDK provider
Expand All @@ -83,16 +83,16 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(
var builder = endpoints.CreateApplicationBuilder();

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

if (path == null)
{
var options = endpoints.ServiceProvider.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;
var options = endpoints.ServiceProvider.GetRequiredService<IOptions<PrometheusAspNetCoreOptions>>().Value;

path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath;
}

if (!path.StartsWith("/"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuild
/// Adds <see cref="PrometheusExporter"/> to the <see cref="MeterProviderBuilder"/>.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
/// <param name="configure">Callback action for configuring <see cref="PrometheusExporterOptions"/>.</param>
/// <param name="configure">Callback action for configuring <see cref="PrometheusAspNetCoreOptions"/>.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddPrometheusExporter(
this MeterProviderBuilder builder,
Action<PrometheusExporterOptions> configure)
Action<PrometheusAspNetCoreOptions> configure)
=> AddPrometheusExporter(builder, name: null, configure);

/// <summary>
/// Adds <see cref="PrometheusExporter"/> to the <see cref="MeterProviderBuilder"/>.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configure">Callback action for configuring <see cref="PrometheusExporterOptions"/>.</param>
/// <param name="configure">Callback action for configuring <see cref="PrometheusAspNetCoreOptions"/>.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddPrometheusExporter(
this MeterProviderBuilder builder,
string name,
Action<PrometheusExporterOptions> configure)
Action<PrometheusAspNetCoreOptions> configure)
{
Guard.ThrowIfNull(builder);

Expand All @@ -70,15 +70,15 @@ public static MeterProviderBuilder AddPrometheusExporter(

return builder.ConfigureBuilder((sp, builder) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<PrometheusExporterOptions>>().Get(name);
var options = sp.GetRequiredService<IOptionsMonitor<PrometheusAspNetCoreOptions>>().Get(name);

AddPrometheusExporter(builder, options);
});
}

private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusExporterOptions options)
private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusAspNetCoreOptions options)
{
var exporter = new PrometheusExporter(scrapeResponseCacheDurationMilliseconds: options.ScrapeResponseCacheDurationMilliseconds);
var exporter = new PrometheusExporter(options.ExporterOptions);

var reader = new BaseExportingMetricReader(exporter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dotnet add package --prerelease OpenTelemetry.Exporter.Prometheus.AspNetCore

## Configuration

The `PrometheusExporter` can be configured using the `PrometheusExporterOptions`
The `PrometheusExporter` can be configured using the `PrometheusAspNetCoreOptions`
properties.

### ScrapeEndpointPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ internal sealed class PrometheusExporter : BaseExporter<Metric>, IPullMetricExpo
/// <summary>
/// Initializes a new instance of the <see cref="PrometheusExporter"/> class.
/// </summary>
/// <param name="scrapeResponseCacheDurationMilliseconds">
/// The cache duration in milliseconds for scrape responses. Default value: 0.
/// </param>
public PrometheusExporter(int scrapeResponseCacheDurationMilliseconds = 0)
/// <param name="options"><see cref="PrometheusExporterOptions"/>.</param>
public PrometheusExporter(PrometheusExporterOptions options)
{
Guard.ThrowIfOutOfRange(scrapeResponseCacheDurationMilliseconds, min: 0);
Guard.ThrowIfNull(options);

this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;

this.ScrapeResponseCacheDurationMilliseconds = scrapeResponseCacheDurationMilliseconds;
this.CollectionManager = new PrometheusCollectionManager(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@

using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter
namespace OpenTelemetry.Exporter.Prometheus
{
/// <summary>
/// Prometheus exporter options.
/// </summary>
public class PrometheusExporterOptions
internal sealed class PrometheusExporterOptions
{
internal const string DefaultScrapeEndpointPath = "/metrics";

private int scrapeResponseCacheDurationMilliseconds = 300;

/// <summary>
/// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics".
/// </summary>
public string ScrapeEndpointPath { get; set; } = DefaultScrapeEndpointPath;

/// <summary>
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static MeterProviderBuilder AddPrometheusHttpListener(
MeterProviderBuilder builder,
PrometheusHttpListenerOptions options)
{
var exporter = new PrometheusExporter();
var exporter = new PrometheusExporter(new PrometheusExporterOptions { ScrapeResponseCacheDurationMilliseconds = 0 });

var reader = new BaseExportingMetricReader(exporter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using OpenTelemetry.Exporter.Prometheus;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter
Expand All @@ -25,7 +26,7 @@ namespace OpenTelemetry.Exporter
/// </summary>
public class PrometheusHttpListenerOptions
{
private IReadOnlyCollection<string> uriPrefixes = new string[] { "http://localhost:9464/" };
private IReadOnlyCollection<string> uriPrefixes = new[] { "http://localhost:9464/" };

/// <summary>
/// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics".
Expand All @@ -51,5 +52,7 @@ public IReadOnlyCollection<string> UriPrefixes
this.uriPrefixes = value;
}
}

internal PrometheusExporterOptions ExporterOptions { get; } = new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public void TestAddPrometheusExporter_NamedOptions()
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.Configure<PrometheusExporterOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
services.Configure<PrometheusAspNetCoreOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);

services.Configure<PrometheusExporterOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
services.Configure<PrometheusAspNetCoreOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
})
.AddPrometheusExporter()
.AddPrometheusExporter("Exporter2", o => { })
Expand Down
Loading