Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Expand Up @@ -4,10 +4,14 @@ OpenTelemetry.Exporter.PrometheusAspNetCoreOptions
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTotalNameSuffixForCounters.get -> bool
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTotalNameSuffixForCounters.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.PrometheusAspNetCoreOptions() -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScopeInfoEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScopeInfoEnabled.set -> 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.Exporter.PrometheusAspNetCoreOptions.TargetInfoEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.TargetInfoEnabled.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, string? optionsName) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Notes](../../RELEASENOTES.md).
* Added a verbose-level diagnostic event for ignored metrics.
([#7429](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7429))

* Add `PrometheusAspNetCoreOptions.ScopeInfoEnabled` property to enable or
disable scope labels in Prometheus metrics. Defaults to `true`.
([#7436](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7436))

* Add `PrometheusAspNetCoreOptions.TargetInfoEnabled` property to enable or
disable the `target_info` metric in Prometheus metrics. Defaults to `true`.
([#TODO](https://github.com/open-telemetry/opentelemetry-dotnet/pull/TODO))
Comment thread
martincostello marked this conversation as resolved.
Outdated

## 1.16.0-beta.1

Released 2026-Jun-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public bool DisableTotalNameSuffixForCounters
set => this.ExporterOptions.DisableTotalNameSuffixForCounters = value;
}

/// <summary>
/// Gets or sets a value indicating whether the scope information (name, version, schema URL) is added to the scrape response.
/// Default value: <see langword="true"/>.
/// </summary>
public bool ScopeInfoEnabled
{
get => this.ExporterOptions.ScopeInfoEnabled;
set => this.ExporterOptions.ScopeInfoEnabled = value;
}

/// <summary>
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
/// </summary>
Expand All @@ -38,5 +48,15 @@ public int ScrapeResponseCacheDurationMilliseconds
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
}

/// <summary>
/// Gets or sets a value indicating whether to include a <c>target_info</c> metric in the scrape response.
/// Default value: <see langword="true"/>.
/// </summary>
public bool TargetInfoEnabled
{
get => this.ExporterOptions.TargetInfoEnabled;
set => this.ExporterOptions.TargetInfoEnabled = value;
}

internal PrometheusExporterOptions ExporterOptions { get; } = new();
}
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Exporter.Prometheus.AspNetCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ to expose the scraping endpoint on a different port.
The `PrometheusExporter` can be configured using the `PrometheusAspNetCoreOptions`
properties.

### ScopeInfoEnabled

Specifies whether metrics include
[scope labels](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md#instrumentation-scope-1).
Default value: `true`. Set to `false` to disable scope labels.

### ScrapeEndpointPath

Defines the path for the Prometheus scrape endpoint for the middleware
Expand All @@ -136,6 +142,12 @@ Configures scrape endpoint response caching. Multiple scrape requests within the
cache duration time period will receive the same previously generated response.
The default value is `300`. Set to `0` to disable response caching.

### TargetInfoEnabled

Specifies whether to produce a
[`target_info`](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md#resource-attributes-1)
metric. Default value: `true`. Set to `false` to disable the `target_info` metric.

## Troubleshooting

This component uses an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ OpenTelemetry.Exporter.PrometheusHttpListenerOptions.Port.get -> int
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.Port.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCounters.get -> bool
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCounters.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScopeInfoEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScopeInfoEnabled.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.TargetInfoEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.TargetInfoEnabled.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.get -> System.Collections.Generic.IReadOnlyCollection<string!>!
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Notes](../../RELEASENOTES.md).
* Added a verbose-level diagnostic event for ignored metrics.
([#7429](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7429))

* Add `PrometheusHttpListenerOptions.ScopeInfoEnabled` property to enable or
disable scope labels in Prometheus metrics. Defaults to `true`.
([#7436](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7436))

* Add `PrometheusHttpListenerOptions.TargetInfoEnabled` property to enable or
disable the `target_info` metric in Prometheus metrics. Defaults to `true`.
([#TODO](https://github.com/open-telemetry/opentelemetry-dotnet/pull/TODO))
Comment thread
martincostello marked this conversation as resolved.
Outdated

## 1.16.0-beta.1

Released 2026-Jun-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ internal sealed class PrometheusCollectionManager
private readonly ConcurrentDictionary<PrometheusProtocol, PrometheusProtocolState> protocolStates = new();

private readonly PrometheusExporter exporter;
private readonly bool scopeInfoEnabled;
private readonly bool targetInfoEnabled;
private readonly TimeSpan scrapeResponseCacheDuration;
private readonly long baseTimestamp = Stopwatch.GetTimestamp();
private readonly PrometheusExporter.ExportFunc onCollectRef;
Expand All @@ -29,6 +31,8 @@ internal sealed class PrometheusCollectionManager
public PrometheusCollectionManager(PrometheusExporter exporter)
{
this.exporter = exporter;
this.scopeInfoEnabled = this.exporter.ScopeInfoEnabled;
this.targetInfoEnabled = this.exporter.TargetInfoEnabled;
this.scrapeResponseCacheDuration = TimeSpan.FromMilliseconds(this.exporter.ScrapeResponseCacheDurationMilliseconds);
this.onCollectRef = this.OnCollect;
this.metricsCache = [];
Expand Down Expand Up @@ -351,8 +355,9 @@ private bool TryWriteResponse(PrometheusProtocol protocol, PrometheusProtocolSta
{
var serializer = TextFormatSerializer.GetSerializer(protocol);

var cursor = this.WriteTargetInfo(serializer, state);
var cursor = this.targetInfoEnabled ? this.WriteTargetInfo(serializer, state) : 0;
var metricStates = this.GetMetricStates(serializer, metrics);
var options = new TextFormatSerializerOptions(suppressScopeInfo: !this.scopeInfoEnabled);

foreach (var metricState in metricStates)
{
Expand All @@ -369,7 +374,8 @@ private bool TryWriteResponse(PrometheusProtocol protocol, PrometheusProtocolSta
metricState.WriteUnit,
metricState.WriteHelp,
metricState.Unit,
metricState.Help);
metricState.Help,
options);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public PrometheusExporter(PrometheusExporterOptions options)
{
Guard.ThrowIfNull(options);

this.ScopeInfoEnabled = options.ScopeInfoEnabled;
this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;
this.TargetInfoEnabled = options.TargetInfoEnabled;
this.DisableTotalNameSuffixForCounters = options.DisableTotalNameSuffixForCounters;

this.CollectionManager = new PrometheusCollectionManager(this);
Expand All @@ -42,6 +44,10 @@ public PrometheusExporter(PrometheusExporterOptions options)

internal PrometheusCollectionManager CollectionManager { get; }

internal bool ScopeInfoEnabled { get; }

internal bool TargetInfoEnabled { get; }

internal int ScrapeResponseCacheDurationMilliseconds { get; }

internal bool DisableTotalNameSuffixForCounters { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ internal sealed class PrometheusExporterOptions
{
public PrometheusExporterOptions()
{
this.ScopeInfoEnabled = true;
this.ScrapeResponseCacheDurationMilliseconds = 300;
this.TargetInfoEnabled = true;
}

/// <summary>
/// Gets or sets a value indicating whether the scope information (name, version, schema URL) is added to the scrape response.
/// Default value: <see langword="true"/>.
/// </summary>
public bool ScopeInfoEnabled { get; set; }

/// <summary>
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
/// </summary>
Expand All @@ -31,6 +39,12 @@ public int ScrapeResponseCacheDurationMilliseconds
}
}

/// <summary>
/// Gets or sets a value indicating whether to include a <c>target_info</c> metric in the scrape response.
/// Default value: <see langword="true"/>.
/// </summary>
public bool TargetInfoEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether addition of _total suffix for counter metric names is disabled. Default value: <see langword="false"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ protected override int WriteExplicitBound(byte[] buffer, int cursor, double expl
protected override bool ShouldWriteSumAndCount(bool hasNegativeBucketBounds)
=> !hasNegativeBucketBounds;

protected override int WriteCounterExemplar(byte[] buffer, int cursor, in MetricPoint metricPoint, PrometheusMetric prometheusMetric, bool isLongValue)
protected override int WriteCounterExemplar(
byte[] buffer,
int cursor,
in MetricPoint metricPoint,
PrometheusMetric prometheusMetric,
bool isLongValue)
{
if (prometheusMetric.Type == PrometheusType.Counter &&
TryGetLatestExemplar(metricPoint, out var exemplar))
Expand All @@ -63,11 +68,17 @@ protected override int WriteCounterExemplar(byte[] buffer, int cursor, in Metric
return cursor;
}

protected override int WriteCounterCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint)
protected override int WriteCounterCreated(
byte[] buffer,
int cursor,
Metric metric,
PrometheusMetric prometheusMetric,
in MetricPoint metricPoint,
in TextFormatSerializerOptions options)
{
if (prometheusMetric.Type == PrometheusType.Counter)
{
cursor = this.WriteCreatedMetric(buffer, cursor, metric, prometheusMetric, metricPoint);
cursor = this.WriteCreatedMetric(buffer, cursor, metric, prometheusMetric, metricPoint, options);
}

return cursor;
Expand All @@ -83,8 +94,8 @@ protected override int WriteHistogramBucketExemplar(byte[] buffer, int cursor, i
return cursor;
}

protected override int WriteHistogramCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint)
=> this.WriteCreatedMetric(buffer, cursor, metric, prometheusMetric, metricPoint, ReservedHistogramLabelNames);
protected override int WriteHistogramCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint, in TextFormatSerializerOptions options)
=> this.WriteCreatedMetric(buffer, cursor, metric, prometheusMetric, metricPoint, options, ReservedHistogramLabelNames);

private static bool TryGetLatestExemplar(in MetricPoint metricPoint, out Exemplar exemplar)
{
Expand Down Expand Up @@ -150,12 +161,13 @@ private int WriteCreatedMetric(
Metric metric,
PrometheusMetric prometheusMetric,
in MetricPoint metricPoint,
in TextFormatSerializerOptions options,
IReadOnlyCollection<string>? reservedOutputKeys = null)
{
cursor = this.WriteMetricMetadataName(buffer, cursor, prometheusMetric);

cursor = WriteAsciiStringNoEscape(buffer, cursor, "_created");
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, reservedOutputKeys: reservedOutputKeys);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, options, reservedOutputKeys: reservedOutputKeys);

buffer[cursor++] = unchecked((byte)' ');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ protected override bool ShouldWriteSumAndCount(bool hasNegativeBucketBounds)
protected override int WriteCounterExemplar(byte[] buffer, int cursor, in MetricPoint metricPoint, PrometheusMetric prometheusMetric, bool isLongValue)
=> cursor;

protected override int WriteCounterCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint)
protected override int WriteCounterCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint, in TextFormatSerializerOptions options)
=> cursor;

protected override int WriteHistogramBucketExemplar(byte[] buffer, int cursor, in MetricPoint metricPoint, double lowerBoundExclusive, double upperBoundInclusive)
=> cursor;

protected override int WriteHistogramCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint)
protected override int WriteHistogramCreated(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, in MetricPoint metricPoint, in TextFormatSerializerOptions options)
=> cursor;
}
Loading