Skip to content

Commit fcb5e21

Browse files
codebotenlzchenemdnetoxrmx
authored
bugfix(exporter): set user agent for otlp metrics exporter (#4475)
* bugfix(exporter): set user agent for otlp metrics exporter Fixes #4474 Signed-off-by: Alex Boten <[email protected]> * update changelog Signed-off-by: Alex Boten <[email protected]> * add test Signed-off-by: Alex Boten <[email protected]> * Update CHANGELOG.md for user agent fix --------- Signed-off-by: Alex Boten <[email protected]> Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 7d5ac58 commit fcb5e21

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Fix user agent in OTLP HTTP metrics exporter
11+
([#4475](https://github.com/open-telemetry/opentelemetry-python/pull/4475))
12+
1013
## Version 1.31.0/0.52b0 (2025-03-12)
1114

1215
- semantic-conventions: Bump to 1.31.0

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
from opentelemetry.exporter.otlp.proto.common.metrics_encoder import (
4141
encode_metrics,
4242
)
43-
from opentelemetry.exporter.otlp.proto.http import Compression
43+
from opentelemetry.exporter.otlp.proto.http import (
44+
_OTLP_HTTP_HEADERS,
45+
Compression,
46+
)
4447
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import ( # noqa: F401
4548
ExportMetricsServiceRequest,
4649
)
@@ -152,9 +155,7 @@ def __init__(
152155
self._compression = compression or _compression_from_env()
153156
self._session = session or requests.Session()
154157
self._session.headers.update(self._headers)
155-
self._session.headers.update(
156-
{"Content-Type": "application/x-protobuf"}
157-
)
158+
self._session.headers.update(_OTLP_HTTP_HEADERS)
158159
if self._compression is not Compression.NoCompression:
159160
self._session.headers.update(
160161
{"Content-Encoding": self._compression.value}

exporter/opentelemetry-exporter-otlp-proto-http/tests/metrics/test_otlp_metrics_exporter.py

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
DEFAULT_TIMEOUT,
3333
OTLPMetricExporter,
3434
)
35+
from opentelemetry.exporter.otlp.proto.http.version import __version__
3536
from opentelemetry.sdk.environment_variables import (
3637
OTEL_EXPORTER_OTLP_CERTIFICATE,
3738
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE,
@@ -124,6 +125,15 @@ def test_constructor_default(self):
124125
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
125126
self.assertEqual(exporter._headers, {})
126127
self.assertIsInstance(exporter._session, Session)
128+
self.assertIn("User-Agent", exporter._session.headers)
129+
self.assertEqual(
130+
exporter._session.headers.get("Content-Type"),
131+
"application/x-protobuf",
132+
)
133+
self.assertEqual(
134+
exporter._session.headers.get("User-Agent"),
135+
"OTel-OTLP-Exporter-Python/" + __version__,
136+
)
127137

128138
@patch.dict(
129139
"os.environ",

0 commit comments

Comments
 (0)