Skip to content

Commit

Permalink
[shared] Tag transformer improvements (#5455)
Browse files Browse the repository at this point in the history
Co-authored-by: Utkarsh Umesan Pillai <[email protected]>
  • Loading branch information
CodeBlanch and utpilla authored Mar 22, 2024
1 parent 2f0af65 commit 49f16e4
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 213 deletions.
1 change: 0 additions & 1 deletion OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A49299
src\Shared\SpanAttributeConstants.cs = src\Shared\SpanAttributeConstants.cs
src\Shared\SpanHelper.cs = src\Shared\SpanHelper.cs
src\Shared\StatusHelper.cs = src\Shared\StatusHelper.cs
src\Shared\TagAndValueTransformer.cs = src\Shared\TagAndValueTransformer.cs
src\Shared\TagTransformer.cs = src\Shared\TagTransformer.cs
src\Shared\TagTransformerJsonHelper.cs = src\Shared\TagTransformerJsonHelper.cs
EndProjectSection
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override ExportResult Export(in Batch<Activity> batch)
continue;
}

if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result))
if (this.TagTransformer.TryTransformTag(tag, out var result))
{
this.WriteLine($" {result}");
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public override ExportResult Export(in Batch<Activity> batch)
this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]");
foreach (ref readonly var attribute in activityEvent.EnumerateTagObjects())
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
if (this.TagTransformer.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
Expand All @@ -104,7 +104,7 @@ 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 (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
if (this.TagTransformer.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
Expand All @@ -118,7 +118,7 @@ public override ExportResult Export(in Batch<Activity> batch)
this.WriteLine("Resource associated with Activity:");
foreach (var resourceAttribute in resource.Attributes)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result))
if (this.TagTransformer.TryTransformTag(resourceAttribute, out var result))
{
this.WriteLine($" {result}");
}
Expand Down
15 changes: 11 additions & 4 deletions src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public abstract class ConsoleExporter<T> : BaseExporter<T>
protected ConsoleExporter(ConsoleExporterOptions options)
{
this.options = options ?? new ConsoleExporterOptions();
ConsoleTagTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
this.WriteLine($"Unsupported attribute type {tagValueType} for {tagKey}.");
};

this.TagTransformer = new ConsoleTagTransformer(this.OnUnsupportedTagDropped);
}

internal ConsoleTagTransformer TagTransformer { get; }

protected void WriteLine(string message)
{
if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Console))
Expand All @@ -29,4 +29,11 @@ protected void WriteLine(string message)
System.Diagnostics.Trace.WriteLine(message);
}
}

private void OnUnsupportedTagDropped(
string tagKey,
string tagValueTypeFullName)
{
this.WriteLine($"Unsupported attribute value type '{tagValueTypeFullName}' for '{tagKey}'.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ 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 (ConsoleTagTransformer.Instance.TryTransformTag(valueToTransform, out var result))
if (this.TagTransformer.TryTransformTag(valueToTransform, out var result))
{
this.WriteLine($"{string.Empty,-4}{result}");
}
Expand Down Expand Up @@ -127,7 +127,7 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)

foreach (KeyValuePair<string, object> scopeItem in scope)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(scopeItem, out var result))
if (this.TagTransformer.TryTransformTag(scopeItem, out var result))
{
exporter.WriteLine($"[Scope.{scopeDepth}]:{result}");
}
Expand All @@ -140,7 +140,7 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)
this.WriteLine("\nResource associated with LogRecord:");
foreach (var resourceAttribute in resource.Attributes)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result))
if (this.TagTransformer.TryTransformTag(resourceAttribute, out var result))
{
this.WriteLine(result);
}
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override ExportResult Export(in Batch<Metric> batch)
this.WriteLine("Resource associated with Metric:");
foreach (var resourceAttribute in this.resource.Attributes)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result))
if (this.TagTransformer.TryTransformTag(resourceAttribute, out var result))
{
this.WriteLine($" {result}");
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public override ExportResult Export(in Batch<Metric> batch)
foreach (var meterTag in metric.MeterTags)
{
this.WriteLine("\tMeter Tags:");
if (ConsoleTagTransformer.Instance.TryTransformTag(meterTag, out var result))
if (this.TagTransformer.TryTransformTag(meterTag, out var result))
{
this.WriteLine($"\t\t{result}");
}
Expand All @@ -80,7 +80,7 @@ public override ExportResult Export(in Batch<Metric> batch)
StringBuilder tagsBuilder = new StringBuilder();
foreach (var tag in metricPoint.Tags)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result))
if (this.TagTransformer.TryTransformTag(tag, out var result))
{
tagsBuilder.Append(result);
tagsBuilder.Append(' ');
Expand Down Expand Up @@ -216,7 +216,7 @@ public override ExportResult Export(in Batch<Metric> batch)
bool appendedTagString = false;
foreach (var tag in exemplar.FilteredTags)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result))
if (this.TagTransformer.TryTransformTag(tag, out var result))
{
if (!appendedTagString)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// 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 ConsoleTagTransformer()
private readonly Action<string, string> onUnsupportedTagDropped;

public ConsoleTagTransformer(Action<string, string> onUnsupportedTagDropped)
{
}
Debug.Assert(onUnsupportedTagDropped != null, "onUnsupportedTagDropped was null");

public static ConsoleTagTransformer Instance { get; } = new();
this.onUnsupportedTagDropped = onUnsupportedTagDropped!;
}

protected override string TransformIntegralTag(string key, long value) => $"{key}: {value}";

Expand All @@ -23,4 +29,11 @@ private ConsoleTagTransformer()

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" RequiresExposedExperimentalFeatures="true" />
<Compile Include="$(RepoRoot)\src\Shared\DiagnosticDefinitions.cs" Link="Includes\DiagnosticDefinitions.cs" RequiresExposedExperimentalFeatures="true" />
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" RequiresExposedExperimentalFeatures="true" />
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" RequiresExposedExperimentalFeatures="true" />
<Compile Include="$(RepoRoot)\src\Shared\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" RequiresExposedExperimentalFeatures="true" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink, SdkLimit
int maxTags = sdkLimitOptions.SpanLinkAttributeCountLimit ?? int.MaxValue;
foreach (ref readonly var tag in activityLink.EnumerateTagObjects())
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
{
if (otlpLink.Attributes.Count < maxTags)
{
Expand Down Expand Up @@ -265,7 +265,7 @@ private static Span.Types.Event ToOtlpEvent(in ActivityEvent activityEvent, SdkL
int maxTags = sdkLimitOptions.SpanEventAttributeCountLimit ?? int.MaxValue;
foreach (ref readonly var tag in activityEvent.EnumerateTagObjects())
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
{
if (otlpEvent.Attributes.Count < maxTags)
{
Expand Down Expand Up @@ -322,7 +322,7 @@ public void EnumerateTags(Activity activity, int maxTags)
continue;
}

if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, this.SdkLimitOptions.AttributeValueLengthLimit))
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, this.SdkLimitOptions.AttributeValueLengthLimit))
{
if (this.Span.Attributes.Count < maxTags)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ internal static OtlpMetrics.Exemplar ToOtlpExemplar<T>(T value, in Metrics.Exemp

foreach (var tag in exemplar.FilteredTags)
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
{
otlpExemplar.FilteredAttributes.Add(result);
}
Expand All @@ -429,7 +429,7 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
{
foreach (var tag in tags)
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
{
attributes.Add(result);
}
Expand All @@ -440,7 +440,7 @@ private static void AddScopeAttributes(IEnumerable<KeyValuePair<string, object>>
{
foreach (var tag in meterTags)
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
{
attributes.Add(result);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)
{
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = attribute.Value as string };
}
else if (OtlpKeyValueTransformer.Instance.TryTransformTag(attribute, out var result, attributeValueLengthLimit))
else if (OtlpTagTransformer.Instance.TryTransformTag(attribute, out var result, attributeValueLengthLimit))
{
AddAttribute(otlpLogRecord, result, attributeCountLimit);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
}
else
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit))
if (OtlpTagTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit))
{
AddAttribute(otlpLog, result, attributeCountLimit);
}
Expand Down Expand Up @@ -257,7 +257,7 @@ private static void AddAttribute(OtlpLogs.LogRecord logRecord, OtlpCommon.KeyVal
private static void AddStringAttribute(OtlpLogs.LogRecord logRecord, string key, string value, int? maxValueLength, int maxAttributeCount)
{
var attributeItem = new KeyValuePair<string, object>(key, value);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength))
if (OtlpTagTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength))
{
AddAttribute(logRecord, result, maxAttributeCount);
}
Expand All @@ -267,7 +267,7 @@ private static void AddStringAttribute(OtlpLogs.LogRecord logRecord, string key,
private static void AddIntAttribute(OtlpLogs.LogRecord logRecord, string key, int value, int maxAttributeCount)
{
var attributeItem = new KeyValuePair<string, object>(key, value);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result))
if (OtlpTagTransformer.Instance.TryTransformTag(attributeItem, out var result))
{
AddAttribute(logRecord, result, maxAttributeCount);
}
Expand Down
Loading

0 comments on commit 49f16e4

Please sign in to comment.