|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using System.Buffers.Text; |
| 7 | +using System.Globalization; |
| 8 | +using System.Text.Json; |
| 9 | +using OpenTelemetry.Internal; |
| 10 | + |
| 11 | +namespace OpenTelemetry.Exporter.Zipkin.Implementation; |
| 12 | + |
| 13 | +internal sealed class ZipkinTagWriter : JsonStringArrayTagWriter<Utf8JsonWriter> |
| 14 | +{ |
| 15 | + public const int StackallocByteThreshold = 256; |
| 16 | + |
| 17 | + private ZipkinTagWriter() |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + public static ZipkinTagWriter Instance { get; } = new(); |
| 22 | + |
| 23 | + protected override void WriteIntegralTag(ref Utf8JsonWriter writer, string key, long value) |
| 24 | + { |
| 25 | + Span<byte> destination = stackalloc byte[StackallocByteThreshold]; |
| 26 | + if (Utf8Formatter.TryFormat(value, destination, out int bytesWritten)) |
| 27 | + { |
| 28 | + writer.WriteString(key, destination.Slice(0, bytesWritten)); |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + writer.WriteString(key, value.ToString(CultureInfo.InvariantCulture)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + protected override void WriteFloatingPointTag(ref Utf8JsonWriter writer, string key, double value) |
| 37 | + { |
| 38 | + Span<byte> destination = stackalloc byte[StackallocByteThreshold]; |
| 39 | + if (Utf8Formatter.TryFormat(value, destination, out int bytesWritten)) |
| 40 | + { |
| 41 | + writer.WriteString(key, destination.Slice(0, bytesWritten)); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + writer.WriteString(key, value.ToString(CultureInfo.InvariantCulture)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + protected override void WriteBooleanTag(ref Utf8JsonWriter writer, string key, bool value) |
| 50 | + => writer.WriteString(key, value ? "true" : "false"); |
| 51 | + |
| 52 | + protected override void WriteStringTag(ref Utf8JsonWriter writer, string key, string value) |
| 53 | + => writer.WriteString(key, value); |
| 54 | + |
| 55 | + protected override void WriteArrayTag(ref Utf8JsonWriter writer, string key, ArraySegment<byte> arrayUtf8JsonBytes) |
| 56 | + { |
| 57 | + writer.WritePropertyName(key); |
| 58 | + writer.WriteStringValue(arrayUtf8JsonBytes); |
| 59 | + } |
| 60 | + |
| 61 | + protected override void OnUnsupportedTagDropped( |
| 62 | + string tagKey, |
| 63 | + string tagValueTypeFullName) |
| 64 | + { |
| 65 | + ZipkinExporterEventSource.Log.UnsupportedAttributeType( |
| 66 | + tagValueTypeFullName, |
| 67 | + tagKey); |
| 68 | + } |
| 69 | +} |
0 commit comments