Skip to content

Commit 623d32c

Browse files
Gpower2arslanoglou-test
authored andcommitted
Add new option DisableAppendingUnitName in order to avoid appending the unit name in the metric name
1 parent aa73ce3 commit 623d32c

File tree

12 files changed

+195
-36
lines changed

12 files changed

+195
-36
lines changed

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

+2
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.DisableAppendingUnitName.get -> bool
5+
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableAppendingUnitName.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/PrometheusAspNetCoreOptions.cs

+9
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 addition of the unit name for metric names is disabled. Default value: <see langword="false"/>.
43+
/// </summary>
44+
public bool DisableAppendingUnitName
45+
{
46+
get => this.ExporterOptions.DisableAppendingUnitName;
47+
set => this.ExporterOptions.DisableAppendingUnitName = value;
48+
}
49+
4150
internal PrometheusExporterOptions ExporterOptions { get; } = new();
4251
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
OpenTelemetry.Exporter.PrometheusHttpListenerOptions
2+
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableAppendingUnitName.get -> bool
3+
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableAppendingUnitName.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/Internal/PrometheusCollectionManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private PrometheusMetric GetPrometheusMetric(Metric metric)
361361
// Optimize writing metrics with bounded cache that has pre-calculated Prometheus names.
362362
if (!this.metricsCache.TryGetValue(metric, out var prometheusMetric))
363363
{
364-
prometheusMetric = PrometheusMetric.Create(metric, this.exporter.DisableTotalNameSuffixForCounters);
364+
prometheusMetric = PrometheusMetric.Create(metric, this.exporter.DisableTotalNameSuffixForCounters, this.exporter.DisableAppendingUnitName);
365365

366366
// Add to the cache if there is space.
367367
if (this.metricsCacheCount < MaxCachedMetrics)

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

+3
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.DisableAppendingUnitName = options.DisableAppendingUnitName;
3031

3132
this.CollectionManager = new PrometheusCollectionManager(this);
3233
}
@@ -48,6 +49,8 @@ public PrometheusExporter(PrometheusExporterOptions options)
4849

4950
internal bool DisableTotalNameSuffixForCounters { get; }
5051

52+
internal bool DisableAppendingUnitName { get; }
53+
5154
internal bool OpenMetricsRequested { get; set; }
5255

5356
internal Resource Resource => this.resource ??= this.ParentProvider.GetResource();

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

+5
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 addition of the unit name for metric names is disabled. Default value: <see langword="false"/>.
39+
/// </summary>
40+
public bool DisableAppendingUnitName { get; set; }
3641
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ UpDownCounter becomes gauge
2121
PrometheusType.Untyped, PrometheusType.Counter, PrometheusType.Gauge, PrometheusType.Summary, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Gauge,
2222
];
2323

24-
public PrometheusMetric(string name, string unit, PrometheusType type, bool disableTotalNameSuffixForCounters)
24+
public PrometheusMetric(string name, string unit, PrometheusType type, bool disableTotalNameSuffixForCounters, bool disableAppendingUnitName)
2525
{
2626
// The metric name is
2727
// required to match the regex: `[a-zA-Z_:]([a-zA-Z0-9_:])*`. Invalid characters
@@ -32,7 +32,7 @@ public PrometheusMetric(string name, string unit, PrometheusType type, bool disa
3232
var openMetricsName = SanitizeOpenMetricsName(sanitizedName);
3333

3434
string? sanitizedUnit = null;
35-
if (!string.IsNullOrEmpty(unit))
35+
if (!disableAppendingUnitName && !string.IsNullOrEmpty(unit))
3636
{
3737
sanitizedUnit = GetUnit(unit);
3838

@@ -86,9 +86,9 @@ public PrometheusMetric(string name, string unit, PrometheusType type, bool disa
8686

8787
public PrometheusType Type { get; }
8888

89-
public static PrometheusMetric Create(Metric metric, bool disableTotalNameSuffixForCounters)
89+
public static PrometheusMetric Create(Metric metric, bool disableTotalNameSuffixForCounters, bool disableAppendingUnitName)
9090
{
91-
return new PrometheusMetric(metric.Name, metric.Unit, GetPrometheusType(metric), disableTotalNameSuffixForCounters);
91+
return new PrometheusMetric(metric.Name, metric.Unit, GetPrometheusType(metric), disableTotalNameSuffixForCounters, disableAppendingUnitName);
9292
}
9393

9494
internal static string SanitizeMetricName(string metricName)

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

+1
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+
DisableAppendingUnitName = options.DisableAppendingUnitName,
7273
});
7374

7475
var reader = new BaseExportingMetricReader(exporter)

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

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public class PrometheusHttpListenerOptions
2424
/// </summary>
2525
public bool DisableTotalNameSuffixForCounters { get; set; }
2626

27+
/// <summary>
28+
/// Gets or sets a value indicating whether addition of the unit name for metric names is disabled. Default value: <see langword="false"/>.
29+
/// </summary>
30+
public bool DisableAppendingUnitName { get; set; }
31+
2732
/// <summary>
2833
/// Gets or sets the URI (Uniform Resource Identifier) prefixes to use for the http listener.
2934
/// Default value: <c>["http://localhost:9464/"]</c>.

test/Benchmarks/Exporter/PrometheusSerializerBenchmarks.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private PrometheusMetric GetPrometheusMetric(Metric metric)
6666
{
6767
if (!this.cache.TryGetValue(metric, out var prometheusMetric))
6868
{
69-
prometheusMetric = PrometheusMetric.Create(metric, false);
69+
prometheusMetric = PrometheusMetric.Create(metric, false, false);
7070
this.cache[metric] = prometheusMetric;
7171
}
7272

0 commit comments

Comments
 (0)