|
| 1 | +// <copyright file="OtlpCommonExtensions.cs" company="OpenTelemetry Authors"> |
| 2 | +// Copyright The OpenTelemetry Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Runtime.CompilerServices; |
| 20 | +using OtlpCommon = Opentelemetry.Proto.Common.V1; |
| 21 | + |
| 22 | +namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation |
| 23 | +{ |
| 24 | + internal static class OtlpCommonExtensions |
| 25 | + { |
| 26 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 27 | + public static OtlpCommon.KeyValue ToOtlpAttribute(this KeyValuePair<string, object> kvp) |
| 28 | + { |
| 29 | + if (kvp.Value == null) |
| 30 | + { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + var value = ToOtlpValue(kvp.Value); |
| 35 | + |
| 36 | + if (value == null) |
| 37 | + { |
| 38 | + OpenTelemetryProtocolExporterEventSource.Log.UnsupportedAttributeType(kvp.Value.GetType().ToString(), kvp.Key); |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + return new OtlpCommon.KeyValue { Key = kvp.Key, Value = value }; |
| 43 | + } |
| 44 | + |
| 45 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 46 | + private static OtlpCommon.AnyValue ToOtlpValue(object value) |
| 47 | + { |
| 48 | + switch (value) |
| 49 | + { |
| 50 | + case char: |
| 51 | + case string: |
| 52 | + return new OtlpCommon.AnyValue { StringValue = Convert.ToString(value) }; |
| 53 | + case bool b: |
| 54 | + return new OtlpCommon.AnyValue { BoolValue = b }; |
| 55 | + case byte: |
| 56 | + case sbyte: |
| 57 | + case short: |
| 58 | + case ushort: |
| 59 | + case int: |
| 60 | + case uint: |
| 61 | + case long: |
| 62 | + return new OtlpCommon.AnyValue { IntValue = Convert.ToInt64(value) }; |
| 63 | + case float: |
| 64 | + case double: |
| 65 | + return new OtlpCommon.AnyValue { DoubleValue = Convert.ToDouble(value) }; |
| 66 | + case Array array: |
| 67 | + return ToOtlpArrayValue(array); |
| 68 | + |
| 69 | + // All other types are converted to strings including the following |
| 70 | + // built-in value types: |
| 71 | + // case nint: Pointer type. |
| 72 | + // case nuint: Pointer type. |
| 73 | + // case ulong: May throw an exception on overflow. |
| 74 | + // case decimal: Converting to double produces rounding errors. |
| 75 | + default: |
| 76 | + try |
| 77 | + { |
| 78 | + return new OtlpCommon.AnyValue { StringValue = value.ToString() }; |
| 79 | + } |
| 80 | + catch |
| 81 | + { |
| 82 | + return null; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 88 | + private static OtlpCommon.AnyValue ToOtlpArrayValue(Array array) |
| 89 | + { |
| 90 | +#pragma warning disable SA1011 // Closing square brackets should be spaced correctly |
| 91 | + var arrayValue = new OtlpCommon.ArrayValue(); |
| 92 | + switch (array) |
| 93 | + { |
| 94 | + case char[]: |
| 95 | + case string[]: |
| 96 | + case bool[]: |
| 97 | + case byte[]: |
| 98 | + case sbyte[]: |
| 99 | + case short[]: |
| 100 | + case ushort[]: |
| 101 | + case int[]: |
| 102 | + case uint[]: |
| 103 | + case long[]: |
| 104 | + case float[]: |
| 105 | + case double[]: |
| 106 | + foreach (var item in array) |
| 107 | + { |
| 108 | + arrayValue.Values.Add(ToOtlpValue(item)); |
| 109 | + } |
| 110 | + |
| 111 | + return new OtlpCommon.AnyValue { ArrayValue = arrayValue }; |
| 112 | + default: |
| 113 | + foreach (var item in array) |
| 114 | + { |
| 115 | + try |
| 116 | + { |
| 117 | + arrayValue.Values.Add(ToOtlpValue(item.ToString())); |
| 118 | + } |
| 119 | + catch |
| 120 | + { |
| 121 | + return null; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return new OtlpCommon.AnyValue { ArrayValue = arrayValue }; |
| 126 | + } |
| 127 | +#pragma warning restore SA1011 // Closing square brackets should be spaced correctly |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments