Skip to content

Commit

Permalink
[console] Switch to TagWriter for handling of tags/attributes (#5596)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored May 9, 2024
1 parent ee5e1e0 commit 49d70e0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 65 deletions.
16 changes: 8 additions & 8 deletions src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public override ExportResult Export(in Batch<Activity> 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}");
}
}
}
Expand Down Expand Up @@ -93,9 +93,9 @@ public override ExportResult Export(in Batch<Activity> 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}");
}
}
}
Expand All @@ -109,9 +109,9 @@ public override ExportResult Export(in Batch<Activity> 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}");
}
}
}
Expand All @@ -123,9 +123,9 @@ public override ExportResult Export(in Batch<Activity> 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}");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
12 changes: 6 additions & 6 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public override ExportResult Export(in Batch<LogRecord> batch)
? 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}");
}
}
}
Expand Down Expand Up @@ -127,9 +127,9 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)

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}");
}
}
}
Expand All @@ -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}");
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public override ExportResult Export(in Batch<Metric> 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}");
}
}
}
Expand Down Expand Up @@ -67,9 +67,9 @@ public override ExportResult Export(in Batch<Metric> 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}");
}
}
}
Expand All @@ -80,9 +80,9 @@ public override ExportResult Export(in Batch<Metric> 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(' ');
}
}
Expand Down Expand Up @@ -216,15 +216,15 @@ public override ExportResult Export(in Batch<Metric> 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)
{
exemplarString.Append(" Filtered Tags : ");
appendedTagString = true;
}

exemplarString.Append(result);
exemplarString.Append($"{result.Key}: {result.Value}");
exemplarString.Append(' ');
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 49d70e0

Please sign in to comment.