Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meter-level tags to Prometheus exporter #5837

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -7,6 +7,9 @@ Notes](../../RELEASENOTES.md).

## Unreleased

* Added meter-level tags to Prometheus exporter
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
([#5837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5837))

## 1.9.0-beta.2

Released 2024-Jun-24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Notes](../../RELEASENOTES.md).

## Unreleased

* Added meter-level tags to Prometheus exporter
([#5837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5837))

## 1.9.0-beta.2

Released 2024-Jun-24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@ public static int WriteTags(byte[] buffer, int cursor, Metric metric, ReadOnlyTa
buffer[cursor++] = unchecked((byte)',');
}

if (metric.MeterTags != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the current tests have been updated to validate MeterTags. Is there a test that validates null MeterTags?"

{
foreach (var tag in metric.MeterTags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
buffer[cursor++] = unchecked((byte)',');
}
}

foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,25 @@ public async Task PrometheusExporterMiddlewareIntegration_CanServeOpenMetricsAnd
using var host = await StartTestHostAsync(
app => app.UseOpenTelemetryPrometheusScrapingEndpoint());

var tags = new KeyValuePair<string, object?>[]
var meterTags = new KeyValuePair<string, object?>[]
{
new("meterKey1", "value1"),
new("meterKey2", "value2"),
};

var counterTags = new KeyValuePair<string, object?>[]
{
new("key1", "value1"),
new("key2", "value2"),
};

using var meter = new Meter(MeterName, MeterVersion);
using var meter = new Meter(MeterName, MeterVersion, meterTags);

var beginTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

var counter = meter.CreateCounter<double>("counter_double", unit: "By");
counter.Add(100.18D, tags);
counter.Add(0.99D, tags);
counter.Add(100.18D, counterTags);
counter.Add(0.99D, counterTags);

var testCases = new bool[] { true, false, true, true, false };

Expand Down Expand Up @@ -326,21 +332,27 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest(

using var host = await StartTestHostAsync(configure, configureServices, registerMeterProvider, configureOptions);

var tags = new KeyValuePair<string, object?>[]
var meterTags = new KeyValuePair<string, object?>[]
{
new("meterKey1", "value1"),
new("meterKey2", "value2"),
};

var counterTags = new KeyValuePair<string, object?>[]
{
new("key1", "value1"),
new("key2", "value2"),
};

using var meter = new Meter(MeterName, MeterVersion);
using var meter = new Meter(MeterName, MeterVersion, meterTags);

var beginTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

var counter = meter.CreateCounter<double>("counter_double", unit: "By");
if (!skipMetrics)
{
counter.Add(100.18D, tags);
counter.Add(0.99D, tags);
counter.Add(100.18D, counterTags);
counter.Add(0.99D, counterTags);
}

using var client = host.GetTestClient();
Expand Down Expand Up @@ -394,14 +406,14 @@ private static async Task VerifyAsync(long beginTimestamp, long endTimestamp, Ht
otel_scope_info{otel_scope_name="{{MeterName}}"} 1
# TYPE counter_double_bytes counter
# UNIT counter_double_bytes bytes
counter_double_bytes_total{otel_scope_name="{{MeterName}}",otel_scope_version="{{MeterVersion}}",key1="value1",key2="value2"} 101.17 (\d+\.\d{3})
counter_double_bytes_total{otel_scope_name="{{MeterName}}",otel_scope_version="{{MeterVersion}}",meterKey1="value1",meterKey2="value2",key1="value1",key2="value2"} 101.17 (\d+\.\d{3})
# EOF

""".ReplaceLineEndings()
: $$"""
# TYPE counter_double_bytes_total counter
# UNIT counter_double_bytes_total bytes
counter_double_bytes_total{otel_scope_name="{{MeterName}}",otel_scope_version="{{MeterVersion}}",key1="value1",key2="value2"} 101.17 (\d+)
counter_double_bytes_total{otel_scope_name="{{MeterName}}",otel_scope_version="{{MeterVersion}}",meterKey1="value1",meterKey2="value2",key1="value1",key2="value2"} 101.17 (\d+)
# EOF

""".ReplaceLineEndings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,16 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri
{
var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text");

using var meter = new Meter(MeterName, MeterVersion);
var meterTags = new KeyValuePair<string, object?>[]
{
new("meterKey1", "value1"),
new("meterKey2", "value2"),
};
using var meter = new Meter(MeterName, MeterVersion, meterTags);

var provider = BuildMeterProvider(meter, [], out var address);

var tags = new KeyValuePair<string, object?>[]
var counterTags = new KeyValuePair<string, object?>[]
{
new("key1", "value1"),
new("key2", "value2"),
Expand All @@ -253,8 +258,8 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri
var counter = meter.CreateCounter<double>("counter_double", unit: "By");
if (!skipMetrics)
{
counter.Add(100.18D, tags);
counter.Add(0.99D, tags);
counter.Add(100.18D, counterTags);
counter.Add(0.99D, counterTags);
}

using HttpClient client = new HttpClient();
Expand Down Expand Up @@ -291,11 +296,11 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri
+ $"otel_scope_info{{otel_scope_name='{MeterName}'}} 1\n"
+ "# TYPE counter_double_bytes counter\n"
+ "# UNIT counter_double_bytes bytes\n"
+ $"counter_double_bytes_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\n"
+ $"counter_double_bytes_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',meterKey1='value1',meterKey2='value2',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\n"
+ "# EOF\n"
: "# TYPE counter_double_bytes_total counter\n"
+ "# UNIT counter_double_bytes_total bytes\n"
+ $"counter_double_bytes_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\n"
+ $"counter_double_bytes_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',meterKey1='value1',meterKey2='value2',key1='value1',key2='value2'}} 101.17 (\\d+)\n"
+ "# EOF\n";

Assert.Matches(("^" + expected + "$").Replace('\'', '"'), content);
Expand Down
Loading