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

Fix Prometheus exporter writing bool label values #4823

Merged
merged 7 commits into from
Sep 7, 2023
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 @@ -2,6 +2,9 @@

## Unreleased

* Fixed writing boolean values to use the JSON representation
([#4823](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4823))

## 1.6.0-rc.1

Released 2023-Aug-21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Fixed writing boolean values to use the JSON representation
([#4823](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4823))

## 1.6.0-rc.1

Released 2023-Aug-21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,22 @@ public static int WriteLabel(byte[] buffer, int cursor, string labelKey, object
buffer[cursor++] = unchecked((byte)'"');

// In Prometheus, a label with an empty label value is considered equivalent to a label that does not exist.
cursor = WriteLabelValue(buffer, cursor, labelValue?.ToString() ?? string.Empty);
cursor = WriteLabelValue(buffer, cursor, GetLabelValueString(labelValue));
buffer[cursor++] = unchecked((byte)'"');

return cursor;

static string GetLabelValueString(object labelValue)
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
{
// TODO: Attribute values should be written as their JSON representation. Extra logic may need to be added here to correctly convert other .NET types.
// More detail: https://github.com/open-telemetry/opentelemetry-dotnet/issues/4822#issuecomment-1707328495
if (labelValue is bool b)
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
{
return b ? "true" : "false";
}

return labelValue?.ToString() ?? string.Empty;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ public void GaugeOneDimension()
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void GaugeBoolDimension()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.Build();

meter.CreateObservableGauge(
"test_gauge",
() => new Measurement<long>(123, new KeyValuePair<string, object>("tagKey", true)));

provider.ForceFlush();

var cursor = WriteMetric(buffer, 0, metrics[0]);
Assert.Matches(
("^"
+ "# TYPE test_gauge gauge\n"
+ "test_gauge{tagKey='true'} 123 \\d+\n"
+ "$").Replace('\'', '"'),
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void GaugeDoubleSubnormal()
{
Expand Down