Skip to content

Commit

Permalink
Code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed May 6, 2024
1 parent fb2959b commit 7e455f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
5 changes: 0 additions & 5 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

## Unreleased

* Zipkin will now write numeric, boolean, and floating-point tag values as
primitive types in its emitted JSON instead of converting the values to
strings.
([#5590](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5590))

## 1.8.1

Released 2024-Apr-17
Expand Down
24 changes: 19 additions & 5 deletions src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Globalization;
using System.Text.Json;
using OpenTelemetry.Internal;

Expand Down Expand Up @@ -152,14 +153,27 @@ public void Write(Utf8JsonWriter writer)
writer.WritePropertyName(ZipkinSpanJsonHelper.TagsPropertyName);
writer.WriteStartObject();

foreach (var tag in this.LocalEndpoint.Tags ?? Enumerable.Empty<KeyValuePair<string, object>>())
// Note: The spec says "Primitive types MUST be converted to string using en-US culture settings"
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md#attribute

var originalUICulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

try
{
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
foreach (var tag in this.LocalEndpoint.Tags ?? Enumerable.Empty<KeyValuePair<string, object>>())
{
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
}

foreach (var tag in this.Tags)
{
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
}
}

foreach (var tag in this.Tags)
finally
{
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}

writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,51 @@

#nullable enable

using System.Buffers.Text;
using System.Globalization;
using System.Text.Json;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.Zipkin.Implementation;

internal sealed class ZipkinTagWriter : JsonStringArrayTagWriter<Utf8JsonWriter>
{
public const int StackallocByteThreshold = 256;

private ZipkinTagWriter()
{
}

public static ZipkinTagWriter Instance { get; } = new();

protected override void WriteIntegralTag(ref Utf8JsonWriter writer, string key, long value)
=> writer.WriteNumber(key, value);
{
Span<byte> destination = stackalloc byte[StackallocByteThreshold];
if (Utf8Formatter.TryFormat(value, destination, out int bytesWritten))
{
writer.WriteString(key, destination.Slice(0, bytesWritten));
}
else
{
writer.WriteString(key, value.ToString(CultureInfo.InvariantCulture));
}
}

protected override void WriteFloatingPointTag(ref Utf8JsonWriter writer, string key, double value)
=> writer.WriteNumber(key, value);
{
Span<byte> destination = stackalloc byte[StackallocByteThreshold];
if (Utf8Formatter.TryFormat(value, destination, out int bytesWritten))
{
writer.WriteString(key, destination.Slice(0, bytesWritten));
}
else
{
writer.WriteString(key, value.ToString(CultureInfo.InvariantCulture));
}
}

protected override void WriteBooleanTag(ref Utf8JsonWriter writer, string key, bool value)
=> writer.WriteBoolean(key, value);
=> writer.WriteString(key, value ? "true" : "false");

protected override void WriteStringTag(ref Utf8JsonWriter writer, string key, string value)
=> writer.WriteString(key, value);
Expand Down

0 comments on commit 7e455f0

Please sign in to comment.