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

[zipkin] Switch to TagWriter for handling of tags/attributes #5590

Merged
merged 4 commits into from
May 7, 2024
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
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