[console] Switch to TagWriter for handling of tags/attributes (#5596)
This commit is contained in:
parent
ee5e1e0628
commit
49d70e0702
|
|
@ -61,9 +61,9 @@ public class ConsoleActivityExporter : ConsoleExporter<Activity>
|
|||
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 class ConsoleActivityExporter : ConsoleExporter<Activity>
|
|||
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 class ConsoleActivityExporter : ConsoleExporter<Activity>
|
|||
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 class ConsoleActivityExporter : ConsoleExporter<Activity>
|
|||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@ public abstract class ConsoleExporter<T> : BaseExporter<T>
|
|||
{
|
||||
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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -93,9 +93,9 @@ public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
|
|||
? new KeyValuePair<string, object>("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 @@ public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
|
|||
|
||||
foreach (KeyValuePair<string, object> 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 @@ public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
|
|||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
|
|||
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 class ConsoleMetricExporter : ConsoleExporter<Metric>
|
|||
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 class ConsoleMetricExporter : ConsoleExporter<Metric>
|
|||
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 class ConsoleMetricExporter : ConsoleExporter<Metric>
|
|||
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 class ConsoleMetricExporter : ConsoleExporter<Metric>
|
|||
appendedTagString = true;
|
||||
}
|
||||
|
||||
exemplarString.Append(result);
|
||||
exemplarString.Append($"{result.Key}: {result.Value}");
|
||||
exemplarString.Append(' ');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<string>
|
||||
{
|
||||
private readonly Action<string, string> onUnsupportedTagDropped;
|
||||
|
||||
public ConsoleTagTransformer(Action<string, string> 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<ConsoleTagWriter.ConsoleTag>
|
||||
{
|
||||
private readonly Action<string, string> onUnsupportedTagDropped;
|
||||
|
||||
public ConsoleTagWriter(Action<string, string> onUnsupportedTagDropped)
|
||||
{
|
||||
Debug.Assert(onUnsupportedTagDropped != null, "onUnsupportedTagDropped was null");
|
||||
|
||||
this.onUnsupportedTagDropped = onUnsupportedTagDropped!;
|
||||
}
|
||||
|
||||
public bool TryTransformTag(KeyValuePair<string, object?> tag, out KeyValuePair<string, string> result)
|
||||
{
|
||||
ConsoleTag consoleTag = default;
|
||||
if (this.TryWriteTag(ref consoleTag, tag))
|
||||
{
|
||||
result = new KeyValuePair<string, string>(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<byte> 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -26,8 +26,7 @@
|
|||
<ItemGroup>
|
||||
<Compile Include="$(RepoRoot)\src\Shared\Metrics\Base2ExponentialBucketHistogramHelper.cs" Link="Includes\Metrics\Base2ExponentialBucketHistogramHelper.cs" />
|
||||
<Compile Include="$(RepoRoot)\src\Shared\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.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>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Reference in New Issue