[zipkin] Switch to TagWriter for handling of tags/attributes (#5590)
This commit is contained in:
parent
ac0d1d1fab
commit
2fe54ee958
|
|
@ -153,7 +153,9 @@ internal readonly struct ZipkinSpan
|
|||
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;
|
||||
|
||||
|
|
@ -161,18 +163,12 @@ internal readonly struct ZipkinSpan
|
|||
{
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#nullable enable
|
||||
|
||||
using OpenTelemetry.Internal;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Zipkin.Implementation;
|
||||
|
||||
internal sealed class ZipkinTagTransformer : TagTransformer<string>
|
||||
{
|
||||
private ZipkinTagTransformer()
|
||||
{
|
||||
}
|
||||
|
||||
public static ZipkinTagTransformer Instance { get; } = new();
|
||||
|
||||
protected override string TransformIntegralTag(string key, long value) => value.ToString();
|
||||
|
||||
protected override string TransformFloatingPointTag(string key, double value) => value.ToString();
|
||||
|
||||
protected override string TransformBooleanTag(string key, bool value) => value ? "true" : "false";
|
||||
|
||||
protected override string TransformStringTag(string key, string value) => value;
|
||||
|
||||
protected override string TransformArrayTag(string key, Array array)
|
||||
=> this.TransformStringTag(key, TagTransformerJsonHelper.JsonSerializeArrayTag(array));
|
||||
|
||||
protected override void OnUnsupportedTagDropped(
|
||||
string tagKey,
|
||||
string tagValueTypeFullName)
|
||||
{
|
||||
ZipkinExporterEventSource.Log.UnsupportedAttributeType(
|
||||
tagValueTypeFullName,
|
||||
tagKey);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue