Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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)
{
// 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)
{
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