Skip to content

Commit 48a87c6

Browse files
committed
fix(exporter): Correct integer encoding in OTLP JSON exporters
Replace string representation of integers with actual integer values in the OTLP JSON exporters to comply with the OTLP specification. This ensures integer attributes are properly encoded as {intValue: 123} instead of {intValue: 123} for better compatibility with OTLP receivers.
1 parent 1cacc84 commit 48a87c6

File tree

3 files changed

+33
-27
lines changed
  • exporter/opentelemetry-exporter-otlp-json-common/src/opentelemetry/exporter/otlp/json/common/_internal

3 files changed

+33
-27
lines changed

exporter/opentelemetry-exporter-otlp-json-common/src/opentelemetry/exporter/otlp/json/common/_internal/_log_encoder/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,29 @@ def _encode_attributes(attributes: Dict[str, Any]) -> List[Dict[str, Any]]:
184184
def _encode_attribute_value(value: Any) -> Dict[str, Any]:
185185
"""Encodes a single attribute value into OTLP JSON format."""
186186
if isinstance(value, bool):
187-
return {"boolValue": value}
187+
return {"value": {"boolValue": value}}
188188
if isinstance(value, int):
189-
return {"intValue": str(value)}
189+
return {"value": {"intValue": value}}
190190
if isinstance(value, float):
191-
return {"doubleValue": value}
191+
return {"value": {"doubleValue": value}}
192192
if isinstance(value, str):
193-
return {"stringValue": value}
193+
return {"value": {"stringValue": value}}
194194
if isinstance(value, (list, tuple)):
195195
if not value:
196-
return {"arrayValue": {"values": []}}
196+
return {"value": {"arrayValue": {"values": []}}}
197197

198198
array_value = {"values": []}
199199
for element in value:
200-
element_value = _encode_attribute_value(element)
200+
element_value = _encode_attribute_value(element)["value"]
201201
array_value["values"].append(element_value)
202202

203-
return {"arrayValue": array_value}
203+
return {"value": {"arrayValue": array_value}}
204204
if isinstance(value, bytes):
205-
return {"bytesValue": base64.b64encode(value).decode("ascii")}
205+
return {
206+
"value": {"bytesValue": base64.b64encode(value).decode("ascii")}
207+
}
206208
# Convert anything else to string
207-
return {"stringValue": str(value)}
209+
return {"value": {"stringValue": str(value)}}
208210

209211

210212
# pylint: disable=too-many-return-statements

exporter/opentelemetry-exporter-otlp-json-common/src/opentelemetry/exporter/otlp/json/common/_internal/metrics_encoder/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,27 +466,29 @@ def _encode_attributes(attributes: Dict[str, Any]) -> List[Dict[str, Any]]:
466466
def _encode_attribute_value(value: Any) -> Dict[str, Any]:
467467
"""Encodes a single attribute value into OTLP JSON format."""
468468
if isinstance(value, bool):
469-
return {"boolValue": value}
469+
return {"value": {"boolValue": value}}
470470
if isinstance(value, int):
471-
return {"intValue": str(value)}
471+
return {"value": {"intValue": value}}
472472
if isinstance(value, float):
473-
return {"doubleValue": value}
473+
return {"value": {"doubleValue": value}}
474474
if isinstance(value, str):
475-
return {"stringValue": value}
475+
return {"value": {"stringValue": value}}
476476
if isinstance(value, (list, tuple)):
477477
if not value:
478-
return {"arrayValue": {"values": []}}
478+
return {"value": {"arrayValue": {"values": []}}}
479479

480480
array_value = {"values": []}
481481
for element in value:
482-
element_value = _encode_attribute_value(element)
482+
element_value = _encode_attribute_value(element)["value"]
483483
array_value["values"].append(element_value)
484484

485-
return {"arrayValue": array_value}
485+
return {"value": {"arrayValue": array_value}}
486486
if isinstance(value, bytes):
487-
return {"bytesValue": base64.b64encode(value).decode("ascii")}
487+
return {
488+
"value": {"bytesValue": base64.b64encode(value).decode("ascii")}
489+
}
488490
# Convert anything else to string
489-
return {"stringValue": str(value)}
491+
return {"value": {"stringValue": str(value)}}
490492

491493

492494
def _get_aggregation_temporality(temporality) -> str:

exporter/opentelemetry-exporter-otlp-json-common/src/opentelemetry/exporter/otlp/json/common/_internal/trace_encoder/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,29 @@ def _encode_attributes(attributes: Dict[str, Any]) -> List[Dict[str, Any]]:
199199
def _encode_attribute_value(value: Any) -> Dict[str, Any]:
200200
"""Encodes a single attribute value into OTLP JSON format."""
201201
if isinstance(value, bool):
202-
return {"boolValue": value}
202+
return {"value": {"boolValue": value}}
203203
if isinstance(value, int):
204-
return {"intValue": str(value)}
204+
return {"value": {"intValue": value}}
205205
if isinstance(value, float):
206-
return {"doubleValue": value}
206+
return {"value": {"doubleValue": value}}
207207
if isinstance(value, str):
208-
return {"stringValue": value}
208+
return {"value": {"stringValue": value}}
209209
if isinstance(value, (list, tuple)):
210210
if not value:
211-
return {"arrayValue": {"values": []}}
211+
return {"value": {"arrayValue": {"values": []}}}
212212

213213
array_value = {"values": []}
214214
for element in value:
215-
element_value = _encode_attribute_value(element)
215+
element_value = _encode_attribute_value(element)["value"]
216216
array_value["values"].append(element_value)
217217

218-
return {"arrayValue": array_value}
218+
return {"value": {"arrayValue": array_value}}
219219
if isinstance(value, bytes):
220-
return {"bytesValue": base64.b64encode(value).decode("ascii")}
220+
return {
221+
"value": {"bytesValue": base64.b64encode(value).decode("ascii")}
222+
}
221223
# Convert anything else to string
222-
return {"stringValue": str(value)}
224+
return {"value": {"stringValue": str(value)}}
223225

224226

225227
def _encode_events(

0 commit comments

Comments
 (0)