diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md index 6f2a230afb3..323a77463fd 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md @@ -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) @@ -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 diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs index e6fe5027e7b..6ec0710b17d 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs @@ -310,12 +310,6 @@ private static OtlpTrace.Span.Types.Event ToOtlpEvent(ActivityEvent activityEven return (Action, int>)dynamicMethod.CreateDelegate(typeof(Action, 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>, PeerServiceResolver.IPeerServiceState { public bool Created; @@ -361,75 +355,19 @@ public bool ForEach(KeyValuePair 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.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { StringValue = s })); - break; - case bool b: - PooledList.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { BoolValue = b })); - break; - case int i: - PeerServiceResolver.InspectTag(ref this, key, i); - PooledList.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { IntValue = i })); - break; - case long l: - PooledList.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { IntValue = l })); - break; - case double d: - PooledList.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.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.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.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.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.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { ArrayValue = arrayValue })); - break; - default: - PooledList.Add(ref this.Tags, CreateOtlpKeyValue(key, new OtlpCommon.AnyValue { StringValue = activityTag.Value.ToString() })); - break; + PooledList.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;