|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from logging import ERROR |
| 17 | + |
| 18 | +from opentelemetry.exporter.otlp.proto.common._internal import ( |
| 19 | + _encode_attributes, |
| 20 | +) |
| 21 | +from opentelemetry.proto.common.v1.common_pb2 import AnyValue as PB2AnyValue |
| 22 | +from opentelemetry.proto.common.v1.common_pb2 import ( |
| 23 | + ArrayValue as PB2ArrayValue, |
| 24 | +) |
| 25 | +from opentelemetry.proto.common.v1.common_pb2 import KeyValue as PB2KeyValue |
| 26 | + |
| 27 | + |
| 28 | +class TestOTLPAttributeEncoder(unittest.TestCase): |
| 29 | + def test_encode_attributes_all_kinds(self): |
| 30 | + result = _encode_attributes( |
| 31 | + { |
| 32 | + "a": 1, # int |
| 33 | + "b": 3.14, # float |
| 34 | + "c": False, # bool |
| 35 | + "hello": "world", # str |
| 36 | + "greet": ["hola", "bonjour"], # Sequence[str] |
| 37 | + "data": [1, 2], # Sequence[int] |
| 38 | + "data_granular": [1.4, 2.4], # Sequence[float] |
| 39 | + } |
| 40 | + ) |
| 41 | + self.assertEqual( |
| 42 | + result, |
| 43 | + [ |
| 44 | + PB2KeyValue(key="a", value=PB2AnyValue(int_value=1)), |
| 45 | + PB2KeyValue(key="b", value=PB2AnyValue(double_value=3.14)), |
| 46 | + PB2KeyValue(key="c", value=PB2AnyValue(bool_value=False)), |
| 47 | + PB2KeyValue( |
| 48 | + key="hello", value=PB2AnyValue(string_value="world") |
| 49 | + ), |
| 50 | + PB2KeyValue( |
| 51 | + key="greet", |
| 52 | + value=PB2AnyValue( |
| 53 | + array_value=PB2ArrayValue( |
| 54 | + values=[ |
| 55 | + PB2AnyValue(string_value="hola"), |
| 56 | + PB2AnyValue(string_value="bonjour"), |
| 57 | + ] |
| 58 | + ) |
| 59 | + ), |
| 60 | + ), |
| 61 | + PB2KeyValue( |
| 62 | + key="data", |
| 63 | + value=PB2AnyValue( |
| 64 | + array_value=PB2ArrayValue( |
| 65 | + values=[ |
| 66 | + PB2AnyValue(int_value=1), |
| 67 | + PB2AnyValue(int_value=2), |
| 68 | + ] |
| 69 | + ) |
| 70 | + ), |
| 71 | + ), |
| 72 | + PB2KeyValue( |
| 73 | + key="data_granular", |
| 74 | + value=PB2AnyValue( |
| 75 | + array_value=PB2ArrayValue( |
| 76 | + values=[ |
| 77 | + PB2AnyValue(double_value=1.4), |
| 78 | + PB2AnyValue(double_value=2.4), |
| 79 | + ] |
| 80 | + ) |
| 81 | + ), |
| 82 | + ), |
| 83 | + ], |
| 84 | + ) |
| 85 | + |
| 86 | + def test_encode_attributes_error_logs_key(self): |
| 87 | + with self.assertLogs(level=ERROR) as error: |
| 88 | + result = _encode_attributes({"a": 1, "bad_key": None, "b": 2}) |
| 89 | + |
| 90 | + self.assertEqual(len(error.records), 1) |
| 91 | + self.assertEqual(error.records[0].msg, "Failed to encode key %s: %s") |
| 92 | + self.assertEqual(error.records[0].args[0], "bad_key") |
| 93 | + self.assertIsInstance(error.records[0].args[1], Exception) |
| 94 | + self.assertEqual( |
| 95 | + result, |
| 96 | + [ |
| 97 | + PB2KeyValue(key="a", value=PB2AnyValue(int_value=1)), |
| 98 | + PB2KeyValue(key="b", value=PB2AnyValue(int_value=2)), |
| 99 | + ], |
| 100 | + ) |
0 commit comments