Skip to content

Commit

Permalink
[zipkin] Switch to TagWriter for handling of tags/attributes (#5590)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored May 7, 2024
1 parent ac0d1d1 commit 2fe54ee
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 48 deletions.
14 changes: 5 additions & 9 deletions src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,22 @@ public void Write(Utf8JsonWriter writer)
writer.WritePropertyName(ZipkinSpanJsonHelper.TagsPropertyName);
writer.WriteStartObject();

// this will be used when we convert int, double, int[], double[] to string
// 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
{
foreach (var tag in this.LocalEndpoint.Tags ?? Enumerable.Empty<KeyValuePair<string, object>>())
{
if (ZipkinTagTransformer.Instance.TryTransformTag(tag, out var result))
{
writer.WriteString(tag.Key, result);
}
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
}

foreach (var tag in this.Tags)
{
if (ZipkinTagTransformer.Instance.TryTransformTag(tag, out var result))
{
writer.WriteString(tag.Key, result);
}
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
}
}
finally
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#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)
{
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)
{
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.WriteString(key, value ? "true" : "false");

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

protected override void WriteArrayTag(ref Utf8JsonWriter writer, string key, ArraySegment<byte> arrayUtf8JsonBytes)
{
writer.WritePropertyName(key);
writer.WriteStringValue(arrayUtf8JsonBytes);
}

protected override void OnUnsupportedTagDropped(
string tagKey,
string tagValueTypeFullName)
{
ZipkinExporterEventSource.Log.UnsupportedAttributeType(
tagValueTypeFullName,
tagKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
<Compile Include="$(RepoRoot)\src\Shared\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\TagTransformer.cs" Link="Includes\TagTransformer.cs" />
<Compile Include="$(RepoRoot)\src\Shared\TagTransformerJsonHelper.cs" Link="Includes\TagTransformerJsonHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\TagWriter\*.cs" Link="Includes\TagWriter\%(Filename).cs" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 2fe54ee

Please sign in to comment.