diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index 8c9d19c07a3..9197cd1287c 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -61,9 +61,9 @@ public override ExportResult Export(in Batch batch) continue; } - if (this.TagTransformer.TryTransformTag(tag, out var result)) + if (this.TagWriter.TryTransformTag(tag, out var result)) { - this.WriteLine($" {result}"); + this.WriteLine($" {result.Key}: {result.Value}"); } } } @@ -93,9 +93,9 @@ public override ExportResult Export(in Batch batch) this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]"); foreach (ref readonly var attribute in activityEvent.EnumerateTagObjects()) { - if (this.TagTransformer.TryTransformTag(attribute, out var result)) + if (this.TagWriter.TryTransformTag(attribute, out var result)) { - this.WriteLine($" {result}"); + this.WriteLine($" {result.Key}: {result.Value}"); } } } @@ -109,9 +109,9 @@ public override ExportResult Export(in Batch batch) this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}"); foreach (ref readonly var attribute in activityLink.EnumerateTagObjects()) { - if (this.TagTransformer.TryTransformTag(attribute, out var result)) + if (this.TagWriter.TryTransformTag(attribute, out var result)) { - this.WriteLine($" {result}"); + this.WriteLine($" {result.Key}: {result.Value}"); } } } @@ -123,9 +123,9 @@ public override ExportResult Export(in Batch batch) this.WriteLine("Resource associated with Activity:"); foreach (var resourceAttribute in resource.Attributes) { - if (this.TagTransformer.TryTransformTag(resourceAttribute, out var result)) + if (this.TagWriter.TryTransformTag(resourceAttribute, out var result)) { - this.WriteLine($" {result}"); + this.WriteLine($" {result.Key}: {result.Value}"); } } } diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs index 55df14f783f..849d029ffc8 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs @@ -12,10 +12,10 @@ protected ConsoleExporter(ConsoleExporterOptions options) { this.options = options ?? new ConsoleExporterOptions(); - this.TagTransformer = new ConsoleTagTransformer(this.OnUnsupportedTagDropped); + this.TagWriter = new ConsoleTagWriter(this.OnUnsupportedTagDropped); } - internal ConsoleTagTransformer TagTransformer { get; } + internal ConsoleTagWriter TagWriter { get; } protected void WriteLine(string message) { diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs index 3dff6a9bea0..3ad90910954 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs @@ -93,9 +93,9 @@ public override ExportResult Export(in Batch batch) ? new KeyValuePair("OriginalFormat (a.k.a Body)", logRecord.Attributes[i].Value) : logRecord.Attributes[i]; - if (this.TagTransformer.TryTransformTag(valueToTransform, out var result)) + if (this.TagWriter.TryTransformTag(valueToTransform, out var result)) { - this.WriteLine($"{string.Empty,-4}{result}"); + this.WriteLine($"{string.Empty,-4}{result.Key}: {result.Value}"); } } } @@ -127,9 +127,9 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter) foreach (KeyValuePair scopeItem in scope) { - if (this.TagTransformer.TryTransformTag(scopeItem, out var result)) + if (this.TagWriter.TryTransformTag(scopeItem, out var result)) { - exporter.WriteLine($"[Scope.{scopeDepth}]:{result}"); + exporter.WriteLine($"[Scope.{scopeDepth}]:{result.Key}: {result.Value}"); } } } @@ -140,9 +140,9 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter) this.WriteLine("\nResource associated with LogRecord:"); foreach (var resourceAttribute in resource.Attributes) { - if (this.TagTransformer.TryTransformTag(resourceAttribute, out var result)) + if (this.TagWriter.TryTransformTag(resourceAttribute, out var result)) { - this.WriteLine(result); + this.WriteLine($"{result.Key}: {result.Value}"); } } } diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index b8e770c24ff..4bd9772b217 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -27,9 +27,9 @@ public override ExportResult Export(in Batch batch) this.WriteLine("Resource associated with Metric:"); foreach (var resourceAttribute in this.resource.Attributes) { - if (this.TagTransformer.TryTransformTag(resourceAttribute, out var result)) + if (this.TagWriter.TryTransformTag(resourceAttribute, out var result)) { - this.WriteLine($" {result}"); + this.WriteLine($" {result.Key}: {result.Value}"); } } } @@ -67,9 +67,9 @@ public override ExportResult Export(in Batch batch) foreach (var meterTag in metric.MeterTags) { this.WriteLine("\tMeter Tags:"); - if (this.TagTransformer.TryTransformTag(meterTag, out var result)) + if (this.TagWriter.TryTransformTag(meterTag, out var result)) { - this.WriteLine($"\t\t{result}"); + this.WriteLine($"\t\t{result.Key}: {result.Value}"); } } } @@ -80,9 +80,9 @@ public override ExportResult Export(in Batch batch) StringBuilder tagsBuilder = new StringBuilder(); foreach (var tag in metricPoint.Tags) { - if (this.TagTransformer.TryTransformTag(tag, out var result)) + if (this.TagWriter.TryTransformTag(tag, out var result)) { - tagsBuilder.Append(result); + tagsBuilder.Append($"{result.Key}: {result.Value}"); tagsBuilder.Append(' '); } } @@ -216,7 +216,7 @@ public override ExportResult Export(in Batch batch) bool appendedTagString = false; foreach (var tag in exemplar.FilteredTags) { - if (this.TagTransformer.TryTransformTag(tag, out var result)) + if (this.TagWriter.TryTransformTag(tag, out var result)) { if (!appendedTagString) { @@ -224,7 +224,7 @@ public override ExportResult Export(in Batch batch) appendedTagString = true; } - exemplarString.Append(result); + exemplarString.Append($"{result.Key}: {result.Value}"); exemplarString.Append(' '); } } diff --git a/src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagTransformer.cs b/src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagTransformer.cs deleted file mode 100644 index 57ace36b352..00000000000 --- a/src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagTransformer.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#nullable enable - -using System.Diagnostics; -using OpenTelemetry.Internal; - -namespace OpenTelemetry.Exporter; - -internal sealed class ConsoleTagTransformer : TagTransformer -{ - private readonly Action onUnsupportedTagDropped; - - public ConsoleTagTransformer(Action onUnsupportedTagDropped) - { - Debug.Assert(onUnsupportedTagDropped != null, "onUnsupportedTagDropped was null"); - - this.onUnsupportedTagDropped = onUnsupportedTagDropped!; - } - - protected override string TransformIntegralTag(string key, long value) => $"{key}: {value}"; - - protected override string TransformFloatingPointTag(string key, double value) => $"{key}: {value}"; - - protected override string TransformBooleanTag(string key, bool value) => $"{key}: {(value ? "true" : "false")}"; - - protected override string TransformStringTag(string key, string value) => $"{key}: {value}"; - - protected override string TransformArrayTag(string key, Array array) - => this.TransformStringTag(key, TagTransformerJsonHelper.JsonSerializeArrayTag(array)); - - protected override void OnUnsupportedTagDropped( - string tagKey, - string tagValueTypeFullName) - { - this.onUnsupportedTagDropped(tagKey, tagValueTypeFullName); - } -} diff --git a/src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagWriter.cs b/src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagWriter.cs new file mode 100644 index 00000000000..f34d14cb3fb --- /dev/null +++ b/src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagWriter.cs @@ -0,0 +1,79 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#nullable enable + +using System.Diagnostics; +using System.Text; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Exporter; + +internal sealed class ConsoleTagWriter : JsonStringArrayTagWriter +{ + private readonly Action onUnsupportedTagDropped; + + public ConsoleTagWriter(Action onUnsupportedTagDropped) + { + Debug.Assert(onUnsupportedTagDropped != null, "onUnsupportedTagDropped was null"); + + this.onUnsupportedTagDropped = onUnsupportedTagDropped!; + } + + public bool TryTransformTag(KeyValuePair tag, out KeyValuePair result) + { + ConsoleTag consoleTag = default; + if (this.TryWriteTag(ref consoleTag, tag)) + { + result = new KeyValuePair(consoleTag.Key!, consoleTag.Value!); + return true; + } + + result = default; + return false; + } + + protected override void WriteIntegralTag(ref ConsoleTag consoleTag, string key, long value) + { + consoleTag.Key = key; + consoleTag.Value = value.ToString(); + } + + protected override void WriteFloatingPointTag(ref ConsoleTag consoleTag, string key, double value) + { + consoleTag.Key = key; + consoleTag.Value = value.ToString(); + } + + protected override void WriteBooleanTag(ref ConsoleTag consoleTag, string key, bool value) + { + consoleTag.Key = key; + consoleTag.Value = value ? "true" : "false"; + } + + protected override void WriteStringTag(ref ConsoleTag consoleTag, string key, string value) + { + consoleTag.Key = key; + consoleTag.Value = value; + } + + protected override void WriteArrayTag(ref ConsoleTag consoleTag, string key, ArraySegment arrayUtf8JsonBytes) + { + consoleTag.Key = key; + consoleTag.Value = Encoding.UTF8.GetString(arrayUtf8JsonBytes.Array!, 0, arrayUtf8JsonBytes.Count); + } + + protected override void OnUnsupportedTagDropped( + string tagKey, + string tagValueTypeFullName) + { + this.onUnsupportedTagDropped(tagKey, tagValueTypeFullName); + } + + internal struct ConsoleTag + { + public string? Key; + + public string? Value; + } +} diff --git a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj index d089c15e830..1f1aa356b5e 100644 --- a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj +++ b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj @@ -26,8 +26,7 @@ - - +