Skip to content

Commit 7b01c50

Browse files
[Exporter.Prometheus*] Optionally disable timestamps (#6600)
1 parent 84c0f42 commit 7b01c50

File tree

15 files changed

+178
-97
lines changed

15 files changed

+178
-97
lines changed

src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions
22
Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions
33
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions
4+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTimestamp.get -> bool
5+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTimestamp.set -> void
46
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTotalNameSuffixForCounters.get -> bool
57
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTotalNameSuffixForCounters.set -> void
68
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.PrometheusAspNetCoreOptions() -> void

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Notes](../../RELEASENOTES.md).
1616
* Add support for .NET 10.0.
1717
([#6307](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6307))
1818

19+
* Added the possibility to disable timestamps via the `PrometheusAspNetCoreOptions`.
20+
([#6600](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6600))
21+
1922
## 1.13.1-beta.1
2023

2124
Released 2025-Oct-10

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,14 @@ public int ScrapeResponseCacheDurationMilliseconds
3838
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
3939
}
4040

41+
/// <summary>
42+
/// Gets or sets a value indicating whether timestamps should be disabled. Default value: <see langword="false"/>.
43+
/// </summary>
44+
public bool DisableTimestamp
45+
{
46+
get => this.ExporterOptions.DisableTimestamp;
47+
set => this.ExporterOptions.DisableTimestamp = value;
48+
}
49+
4150
internal PrometheusExporterOptions ExporterOptions { get; } = new();
4251
}

src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
OpenTelemetry.Exporter.PrometheusHttpListenerOptions
2+
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTimestamp.get -> bool
3+
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTimestamp.set -> void
24
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCounters.get -> bool
35
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCounters.set -> void
46
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.get -> System.Collections.Generic.IReadOnlyCollection<string!>!

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Notes](../../RELEASENOTES.md).
1616
* Add support for .NET 10.0.
1717
([#6307](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6307))
1818

19+
* Added the possibility to disable timestamps via the `PrometheusHttpListenerOptions`.
20+
([#6600](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6600))
21+
1922
## 1.13.1-beta.1
2023

2124
Released 2025-Oct-10

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ private ExportResult OnCollect(in Batch<Metric> metrics)
275275
cursor,
276276
metric,
277277
this.GetPrometheusMetric(metric),
278-
this.exporter.OpenMetricsRequested);
278+
this.exporter.OpenMetricsRequested,
279+
this.exporter.DisableTimestamp);
279280

280281
break;
281282
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public PrometheusExporter(PrometheusExporterOptions options)
2727

2828
this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;
2929
this.DisableTotalNameSuffixForCounters = options.DisableTotalNameSuffixForCounters;
30+
this.DisableTimestamp = options.DisableTimestamp;
3031

3132
this.CollectionManager = new PrometheusCollectionManager(this);
3233
}
@@ -50,6 +51,8 @@ public PrometheusExporter(PrometheusExporterOptions options)
5051

5152
internal bool OpenMetricsRequested { get; set; }
5253

54+
internal bool DisableTimestamp { get; set; }
55+
5356
internal Resource Resource => this.resource ??= this.ParentProvider.GetResource();
5457

5558
/// <inheritdoc/>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ public int ScrapeResponseCacheDurationMilliseconds
3333
/// Gets or sets a value indicating whether addition of _total suffix for counter metric names is disabled. Default value: <see langword="false"/>.
3434
/// </summary>
3535
public bool DisableTotalNameSuffixForCounters { get; set; }
36+
37+
/// <summary>
38+
/// Gets or sets a value indicating whether timestamps should be disabled. Default value: <see langword="false"/>.
39+
/// </summary>
40+
public bool DisableTimestamp { get; set; }
3641
}

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static bool CanWriteMetric(Metric metric)
2222
return true;
2323
}
2424

25-
public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, bool openMetricsRequested = false)
25+
public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, bool openMetricsRequested, bool disableTimestamp)
2626
{
2727
cursor = WriteTypeMetadata(buffer, cursor, prometheusMetric, openMetricsRequested);
2828
cursor = WriteUnitMetadata(buffer, cursor, prometheusMetric, openMetricsRequested);
@@ -66,9 +66,11 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
6666
}
6767
}
6868

69-
buffer[cursor++] = unchecked((byte)' ');
70-
71-
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
69+
if (!disableTimestamp)
70+
{
71+
buffer[cursor++] = unchecked((byte)' ');
72+
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
73+
}
7274

7375
buffer[cursor++] = ASCII_LINEFEED;
7476
}
@@ -103,9 +105,12 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
103105
cursor = WriteAsciiStringNoEscape(buffer, cursor, "\"} ");
104106

105107
cursor = WriteLong(buffer, cursor, totalCount);
106-
buffer[cursor++] = unchecked((byte)' ');
107108

108-
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
109+
if (!disableTimestamp)
110+
{
111+
buffer[cursor++] = unchecked((byte)' ');
112+
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
113+
}
109114

110115
buffer[cursor++] = ASCII_LINEFEED;
111116
}
@@ -118,9 +123,12 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
118123
buffer[cursor++] = unchecked((byte)' ');
119124

120125
cursor = WriteDouble(buffer, cursor, metricPoint.GetHistogramSum());
121-
buffer[cursor++] = unchecked((byte)' ');
122126

123-
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
127+
if (!disableTimestamp)
128+
{
129+
buffer[cursor++] = unchecked((byte)' ');
130+
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
131+
}
124132

125133
buffer[cursor++] = ASCII_LINEFEED;
126134

@@ -132,9 +140,12 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
132140
buffer[cursor++] = unchecked((byte)' ');
133141

134142
cursor = WriteLong(buffer, cursor, metricPoint.GetHistogramCount());
135-
buffer[cursor++] = unchecked((byte)' ');
136143

137-
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
144+
if (!disableTimestamp)
145+
{
146+
buffer[cursor++] = unchecked((byte)' ');
147+
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);
148+
}
138149

139150
buffer[cursor++] = ASCII_LINEFEED;
140151
}

src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private static BaseExportingMetricReader BuildPrometheusHttpListenerMetricReader
6969
{
7070
ScrapeResponseCacheDurationMilliseconds = 0,
7171
DisableTotalNameSuffixForCounters = options.DisableTotalNameSuffixForCounters,
72+
DisableTimestamp = options.DisableTimestamp,
7273
});
7374

7475
var reader = new BaseExportingMetricReader(exporter)

0 commit comments

Comments
 (0)