From 79989fab21d31659652d763e5fc94f8079458476 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Wed, 11 May 2022 12:57:42 -0700 Subject: [PATCH 1/4] Use ToOtlpAttribute method for traces --- .../Implementation/ActivityExtensions.cs | 71 ++----------------- 1 file changed, 5 insertions(+), 66 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs index e6fe5027e7b..7b10d464969 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,74 +355,19 @@ public bool ForEach(KeyValuePair activityTag) this.Created = true; } - OtlpCommon.ArrayValue arrayValue; + var attribute = activityTag.ToOtlpAttribute(); + if (attribute != null) + { + PooledList.Add(ref this.Tags, attribute); + } switch (activityTag.Value) { 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; } From 150f058eae0eb91b2202eaf12134d5eed682960e Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Wed, 11 May 2022 13:02:35 -0700 Subject: [PATCH 2/4] Update changelog --- .../CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From 62fecebc3697d181408870b83c118d08f307a86a Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 13 May 2022 13:11:33 -0700 Subject: [PATCH 3/4] Refactor PeerServiceResolver check --- .../Implementation/ActivityExtensions.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs index 7b10d464969..62110466bd4 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs @@ -359,16 +359,16 @@ public bool ForEach(KeyValuePair activityTag) if (attribute != null) { PooledList.Add(ref this.Tags, attribute); - } - switch (activityTag.Value) - { - case string s: - PeerServiceResolver.InspectTag(ref this, key, s); - break; - case int i: - PeerServiceResolver.InspectTag(ref this, key, i); - break; + switch (attribute.Value.ValueCase) + { + case OtlpCommon.AnyValue.ValueOneofCase.StringValue: + PeerServiceResolver.InspectTag(ref this, key, attribute.Value.StringValue); + break; + case OtlpCommon.AnyValue.ValueOneofCase.IntValue: + PeerServiceResolver.InspectTag(ref this, key, attribute.Value.IntValue); + break; + } } return true; From eacc315ffee0b92832b466a62e5222c12ea49345 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 13 May 2022 13:23:24 -0700 Subject: [PATCH 4/4] Change switch to if/else --- .../Implementation/ActivityExtensions.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs index 62110466bd4..6ec0710b17d 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs @@ -360,14 +360,13 @@ public bool ForEach(KeyValuePair activityTag) { PooledList.Add(ref this.Tags, attribute); - switch (attribute.Value.ValueCase) + if (attribute.Value.ValueCase == OtlpCommon.AnyValue.ValueOneofCase.StringValue) { - case OtlpCommon.AnyValue.ValueOneofCase.StringValue: - PeerServiceResolver.InspectTag(ref this, key, attribute.Value.StringValue); - break; - case OtlpCommon.AnyValue.ValueOneofCase.IntValue: - PeerServiceResolver.InspectTag(ref this, key, attribute.Value.IntValue); - break; + 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); } }