Skip to content

Commit 0da377a

Browse files
authored
[Prometheus.Tests] Add tests for mapping metric types (#6645)
1 parent dc4bd4d commit 0da377a

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public PrometheusMetric(string name, string unit, PrometheusType type, bool disa
8888

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

9494
internal static string SanitizeMetricName(string metricName)
@@ -177,6 +177,12 @@ internal static string RemoveAnnotations(string unit)
177177
return sb.ToString();
178178
}
179179

180+
internal static PrometheusType GetPrometheusType(MetricType openTelemetryMetricType)
181+
{
182+
int metricType = (int)openTelemetryMetricType >> 4;
183+
return MetricTypes[metricType];
184+
}
185+
180186
private static string SanitizeOpenMetricsName(string metricName)
181187
{
182188
if (metricName.EndsWith("_total", StringComparison.Ordinal))
@@ -231,12 +237,6 @@ private static bool TryProcessRateUnits(string updatedUnit, [NotNullWhen(true)]
231237
return false;
232238
}
233239

234-
private static PrometheusType GetPrometheusType(Metric metric)
235-
{
236-
int metricType = (int)metric.MetricType >> 4;
237-
return MetricTypes[metricType];
238-
}
239-
240240
// The map to translate OTLP units to Prometheus units
241241
// OTLP metrics use the c/s notation as specified at https://ucum.org/ucum.html
242242
// (See also https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/metrics.md#instrument-units)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using OpenTelemetry.Metrics;
5+
6+
namespace OpenTelemetry.Exporter.Prometheus.Tests;
7+
8+
#pragma warning disable CA1515 // Consider making public types internal
9+
public class MetricsMappingTestData
10+
#pragma warning restore CA1515 // Consider making public types internal
11+
{
12+
internal MetricsMappingTestData(MetricType openTelemetryMetricType, PrometheusType expectedPrometheusType)
13+
{
14+
this.OpenTelemetryMetricType = openTelemetryMetricType;
15+
this.ExpectedPrometheusType = expectedPrometheusType;
16+
}
17+
18+
internal MetricType OpenTelemetryMetricType { get; }
19+
20+
internal PrometheusType ExpectedPrometheusType { get; }
21+
}

test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<DefineConstants>$(DefineConstants);PROMETHEUS_HTTP_LISTENER</DefineConstants>
77
<!-- Avoid conflicting port registrations if TFMs tested in parallel -->
88
<TestTfmsInParallel>false</TestTfmsInParallel>
9+
<!-- CA1062 Validate arguments of public methods - needed for Theory methods -->
10+
<NoWarn>$(NoWarn);CA1062</NoWarn>
911
</PropertyGroup>
1012

1113
<ItemGroup>

test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusMetricTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using OpenTelemetry.Metrics;
45
using Xunit;
56

67
namespace OpenTelemetry.Exporter.Prometheus.Tests;
78

89
public sealed class PrometheusMetricTests
910
{
11+
public static TheoryData<MetricsMappingTestData> GetPrometheusType_Data()
12+
{
13+
var data = new TheoryData<MetricsMappingTestData>
14+
{
15+
new(MetricType.LongSum, PrometheusType.Counter),
16+
new(MetricType.DoubleSum, PrometheusType.Counter),
17+
new(MetricType.LongGauge, PrometheusType.Gauge),
18+
new(MetricType.DoubleGauge, PrometheusType.Gauge),
19+
new(MetricType.Histogram, PrometheusType.Histogram),
20+
new(MetricType.ExponentialHistogram, PrometheusType.Histogram),
21+
new(MetricType.LongSumNonMonotonic, PrometheusType.Gauge),
22+
new(MetricType.DoubleSumNonMonotonic, PrometheusType.Gauge),
23+
};
24+
return data;
25+
}
26+
1027
[Fact]
1128
public void SanitizeMetricName_Valid()
1229
{
@@ -247,6 +264,14 @@ public void OpenMetricsMetadataName_Counter_DisableSuffixTotal_NotAppendTotal()
247264
AssertOpenMetricsMetadataName("db_bytes_written", "By", PrometheusType.Counter, true, "db_bytes_written_bytes");
248265
}
249266

267+
[Theory]
268+
[MemberData(nameof(GetPrometheusType_Data))]
269+
public void GetPrometheusType_MapsOpenTelemetryMetricsTypeToPrometheus(MetricsMappingTestData mappingTestData)
270+
{
271+
var result = PrometheusMetric.GetPrometheusType(mappingTestData.OpenTelemetryMetricType);
272+
Assert.Equal(mappingTestData.ExpectedPrometheusType, result);
273+
}
274+
250275
private static void AssertName(
251276
string name, string unit, PrometheusType type, bool disableTotalNameSuffixForCounters, string expected)
252277
{

0 commit comments

Comments
 (0)