Skip to content

Commit b34294c

Browse files
log the attribute's key when encoding an attribute fails (#3838)
1 parent c2b9a67 commit b34294c

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- When encountering an error encoding metric attributes in the OTLP exporter, log the key that had an error.
11+
([#3838](https://github.com/open-telemetry/opentelemetry-python/pull/3838))
1012
- Log a warning when a `LogRecord` in `sdk/log` has dropped attributes
1113
due to reaching limits
1214
([#3946](https://github.com/open-telemetry/opentelemetry-python/pull/3946))

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _encode_attributes(
110110
try:
111111
pb2_attributes.append(_encode_key_value(key, value))
112112
except Exception as error:
113-
_logger.exception(error)
113+
_logger.exception("Failed to encode key %s: %s", key, error)
114114
else:
115115
pb2_attributes = None
116116
return pb2_attributes
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)