Skip to content

Commit

Permalink
Use ToOtlpAttribute method for traces (#3274)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored May 16, 2022
1 parent d983cb1 commit a383463
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ option
* Fix handling of array-valued attributes for the OTLP trace exporter.
([#3238](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3238))

* Improve the conversion and formatting of attribute values to the OTLP format
for resources, metrics, and logs. The list of data types that must be
supported per the
* Improve the conversion and formatting of attribute values to the OTLP format.
The list of data types that must be supported per the
[OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/common#attribute)
is more narrow than what the .NET OpenTelemetry SDK supports. Numeric
[built-in value types](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/built-in-types)
Expand All @@ -30,6 +29,7 @@ option
`char`, `bool` are supported. All other types are converted to a `string`.
Array values are also supported.
([#3262](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3262))
([#3274](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3274))

## 1.3.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,6 @@ private static OtlpTrace.Span.Types.Event ToOtlpEvent(ActivityEvent activityEven
return (Action<RepeatedField<OtlpTrace.Span>, int>)dynamicMethod.CreateDelegate(typeof(Action<RepeatedField<OtlpTrace.Span>, int>));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static OtlpCommon.KeyValue CreateOtlpKeyValue(string key, OtlpCommon.AnyValue value)
{
return new OtlpCommon.KeyValue { Key = key, Value = value };
}

private struct TagEnumerationState : IActivityEnumerator<KeyValuePair<string, object>>, PeerServiceResolver.IPeerServiceState
{
public bool Created;
Expand Down Expand Up @@ -361,75 +355,19 @@ public bool ForEach(KeyValuePair<string, object> activityTag)
this.Created = true;
}

OtlpCommon.ArrayValue arrayValue;

switch (activityTag.Value)
var attribute = activityTag.ToOtlpAttribute();
if (attribute != null)
{
case string s:
PeerServiceResolver.InspectTag(ref this, key, s);
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { StringValue = s }));
break;
case bool b:
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { BoolValue = b }));
break;
case int i:
PeerServiceResolver.InspectTag(ref this, key, i);
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { IntValue = i }));
break;
case long l:
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { IntValue = l }));
break;
case double d:
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { DoubleValue = d }));
break;
case int[] intArray:
arrayValue = new OtlpCommon.ArrayValue();
foreach (var item in intArray)
{
arrayValue.Values.Add(new OtlpCommon.AnyValue { IntValue = item });
}

PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { ArrayValue = arrayValue }));
break;
case double[] doubleArray:
arrayValue = new OtlpCommon.ArrayValue();
foreach (var item in doubleArray)
{
arrayValue.Values.Add(new OtlpCommon.AnyValue { DoubleValue = item });
}

PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { ArrayValue = arrayValue }));
break;
case bool[] boolArray:
arrayValue = new OtlpCommon.ArrayValue();
foreach (var item in boolArray)
{
arrayValue.Values.Add(new OtlpCommon.AnyValue { BoolValue = item });
}

PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { ArrayValue = arrayValue }));
break;
case string[] stringArray:
arrayValue = new OtlpCommon.ArrayValue();
foreach (var item in stringArray)
{
arrayValue.Values.Add(item == null ? new OtlpCommon.AnyValue() : new OtlpCommon.AnyValue { StringValue = item });
}

PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { ArrayValue = arrayValue }));
break;
case long[] longArray:
arrayValue = new OtlpCommon.ArrayValue();
foreach (var item in longArray)
{
arrayValue.Values.Add(new OtlpCommon.AnyValue { IntValue = item });
}

PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { ArrayValue = arrayValue }));
break;
default:
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { StringValue = activityTag.Value.ToString() }));
break;
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, attribute);

if (attribute.Value.ValueCase == OtlpCommon.AnyValue.ValueOneofCase.StringValue)
{
PeerServiceResolver.InspectTag(ref this, key, attribute.Value.StringValue);
}
else if (attribute.Value.ValueCase == OtlpCommon.AnyValue.ValueOneofCase.IntValue)
{
PeerServiceResolver.InspectTag(ref this, key, attribute.Value.IntValue);
}
}

return true;
Expand Down

0 comments on commit a383463

Please sign in to comment.