Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -10,6 +10,8 @@ OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.get -> str
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 @@ -27,6 +27,10 @@ Notes](../../RELEASENOTES.md).
schemes when negotiated via the `Accept` header.
([#7439](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7439))

* Add `PrometheusAspNetCoreOptions.TargetInfoEnabled` property to enable or
disable the `target_info` metric in Prometheus metrics. Defaults to `true`.
([#7438](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7438))

## 1.16.0-beta.1

Released 2026-Jun-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,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();
}
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.Prometheus.AspNetCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,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 @@ -7,6 +7,8 @@ OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCo
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.PrometheusHttpListenerOptions() -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.get -> string?
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.set -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Notes](../../RELEASENOTES.md).
schemes when negotiated via the `Accept` header.
([#7439](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7439))

* Add `PrometheusHttpListenerOptions.TargetInfoEnabled` property to enable or
disable the `target_info` metric in Prometheus metrics. Defaults to `true`.
([#7438](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7438))

## 1.16.0-beta.1

Released 2026-Jun-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal sealed class PrometheusCollectionManager

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 @@ -31,6 +32,7 @@ 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 @@ -353,7 +355,7 @@ private bool TryWriteResponse(in PrometheusProtocol protocol, PrometheusProtocol
{
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public PrometheusExporter(PrometheusExporterOptions options)

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

this.CollectionManager = new PrometheusCollectionManager(this);
Expand All @@ -45,6 +46,8 @@ public PrometheusExporter(PrometheusExporterOptions options)

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 @@ -14,6 +14,7 @@ public PrometheusExporterOptions()
{
this.ScopeInfoEnabled = true;
this.ScrapeResponseCacheDurationMilliseconds = 300;
this.TargetInfoEnabled = true;
}

/// <summary>
Expand All @@ -38,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 @@ -69,6 +69,7 @@ private static BaseExportingMetricReader BuildPrometheusHttpListenerMetricReader
{
ScopeInfoEnabled = options.ScopeInfoEnabled,
ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds,
TargetInfoEnabled = options.TargetInfoEnabled,
DisableTotalNameSuffixForCounters = options.DisableTotalNameSuffixForCounters,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,11 @@ 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; } = true;

internal Action<PrometheusHttpListenerOptions, System.Net.HttpListener>? ConfigureHttpListener { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ variables.
* `ScopeInfoEnabled`: Whether to include scope labels in metrics (default `true`).
* `ScrapeEndpointPath`: Defines the Prometheus scrape endpoint path.
(default `"/metrics"`).
* `TargetInfoEnabled`: Whether to produce a `target_info` metric (default `true`).
* `DisableTotalNameSuffixForCounters`: Whether to disable the `_total` suffix for
counter metrics (default `false`).
* `DisableTimestamp`: Whether to disable the timestamp for metrics (default `false`).
Expand Down Expand Up @@ -108,6 +109,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 @@ -48,6 +48,20 @@ public async Task RunWithScopeInfoEnabledConfigured(bool scopeInfoEnabled)
await Verify(output, "text", PrometheusSerializerTests.VerifySettings).UseParameters(scopeInfoEnabled);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task RunWithTargetInfoEnabledConfigured(bool targetInfoEnabled)
{
var output = await RunPrometheusExporterMiddlewareIntegrationTest(
"/metrics",
app => app.UseOpenTelemetryPrometheusScrapingEndpoint(),
services => services.Configure<PrometheusAspNetCoreOptions>(o => o.TargetInfoEnabled = targetInfoEnabled),
assertResponseContent: false);

await Verify(output, "text", PrometheusSerializerTests.VerifySettings).UseParameters(targetInfoEnabled);
}

[Fact]
public async Task RunWithCustomScrapeEndpointPath()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public async Task RunHttpServerWithScopeInfoEnabledConfigured(bool scopeInfoEnab
await Verify(output, "text", PrometheusSerializerTests.VerifySettings).UseParameters(scopeInfoEnabled);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task RunHttpServerWithTargetInfoEnabledConfigured(bool targetInfoEnabled)
{
var output = await RunPrometheusExporterHttpServerIntegrationTest(
configureListener: (options) =>
{
options.TargetInfoEnabled = targetInfoEnabled;
return options.Port;
},
assertResponseContent: false);

await Verify(output, "text", PrometheusSerializerTests.VerifySettings).UseParameters(targetInfoEnabled);
}

[Fact]
public async Task RunHttpServerWithNoMetrics()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TYPE counter_double_bytes counter
# UNIT counter_double_bytes bytes
counter_double_bytes_total{otel_scope_name="PrometheusHttpListenerTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} 101.17
counter_double_bytes_created{otel_scope_name="PrometheusHttpListenerTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} <TIMESTAMP>
# EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TYPE target info
# HELP target Target metadata
target_info{service_name="my_service",service_instance_id="id1"} 1
# TYPE counter_double_bytes counter
# UNIT counter_double_bytes bytes
counter_double_bytes_total{otel_scope_name="PrometheusHttpListenerTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} 101.17
counter_double_bytes_created{otel_scope_name="PrometheusHttpListenerTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} <TIMESTAMP>
# EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TYPE counter_double_bytes counter
# UNIT counter_double_bytes bytes
counter_double_bytes_total{otel_scope_name="PrometheusExporterMiddlewareTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} 101.17
counter_double_bytes_created{otel_scope_name="PrometheusExporterMiddlewareTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} <TIMESTAMP>
# EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TYPE target info
# HELP target Target metadata
target_info{service_name="my_service",service_instance_id="id1"} 1
# TYPE counter_double_bytes counter
# UNIT counter_double_bytes bytes
counter_double_bytes_total{otel_scope_name="PrometheusExporterMiddlewareTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} 101.17
counter_double_bytes_created{otel_scope_name="PrometheusExporterMiddlewareTests",otel_scope_version="1.0.1",key1="value1",key2="value2"} <TIMESTAMP>
# EOF