Skip to content

Commit

Permalink
Merge branch 'utpilla/Update-OTel-Latest-Stable-Version' of https://g…
Browse files Browse the repository at this point in the history
…ithub.com/utpilla/opentelemetry-dotnet into utpilla/Update-OTel-Latest-Stable-Version
  • Loading branch information
utpilla committed Sep 7, 2023
2 parents 4118c7c + e1f58eb commit e5dcc01
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md
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)
{
// 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

0 comments on commit e5dcc01

Please sign in to comment.