Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8927ae5

Browse files
Alex Botensrikanthccv
andauthored
exporter-otlp-proto-http: add user agent string (open-telemetry#2959)
* `exporter-otlp-proto-http`: add user agent string Adding user agent string to OTLP HTTP exporter. As part of the change, I refactored the content-type header as well. Part of open-telemetry#2958 * update changelog * fix linting, fix link Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 6f6f8d1 commit 8927ae5

File tree

9 files changed

+30
-22
lines changed

9 files changed

+30
-22
lines changed

CHANGELOG.md

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

1010
- Update explicit histogram bucket boundaries
1111
([#2947](https://github.com/open-telemetry/opentelemetry-python/pull/2947))
12+
- `exporter-otlp-proto-http`: add user agent string
13+
([#2959](https://github.com/open-telemetry/opentelemetry-python/pull/2959))
1214

1315
## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0) - 2022-09-26
1416

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@
7171
"""
7272
import enum
7373

74+
from .version import __version__
75+
76+
77+
_OTLP_HTTP_HEADERS = {
78+
"Content-Type": "application/x-protobuf",
79+
"User-Agent": "OTel OTLP Exporter Python/" + __version__,
80+
}
81+
7482

7583
class Compression(enum.Enum):
7684
NoCompression = "none"

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
LogExportResult,
3636
LogData,
3737
)
38-
from opentelemetry.exporter.otlp.proto.http import Compression
38+
from opentelemetry.exporter.otlp.proto.http import (
39+
_OTLP_HTTP_HEADERS,
40+
Compression,
41+
)
3942
from opentelemetry.exporter.otlp.proto.http._log_exporter.encoder import (
4043
_ProtobufEncoder,
4144
)
@@ -78,9 +81,7 @@ def __init__(
7881
self._compression = compression or _compression_from_env()
7982
self._session = session or requests.Session()
8083
self._session.headers.update(self._headers)
81-
self._session.headers.update(
82-
{"Content-Type": _ProtobufEncoder._CONTENT_TYPE}
83-
)
84+
self._session.headers.update(_OTLP_HTTP_HEADERS)
8485
if self._compression is not Compression.NoCompression:
8586
self._session.headers.update(
8687
{"Content-Encoding": self._compression.value}

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/encoder/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636

3737

3838
class _ProtobufEncoder:
39-
_CONTENT_TYPE = "application/x-protobuf"
40-
4139
@classmethod
4240
def serialize(cls, batch: Sequence[LogData]) -> str:
4341
return cls.encode(batch).SerializeToString()

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
OTEL_EXPORTER_OTLP_TIMEOUT,
3737
)
3838
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
39-
from opentelemetry.exporter.otlp.proto.http import Compression
39+
from opentelemetry.exporter.otlp.proto.http import (
40+
_OTLP_HTTP_HEADERS,
41+
Compression,
42+
)
4043
from opentelemetry.exporter.otlp.proto.http.trace_exporter.encoder import (
4144
_ProtobufEncoder,
4245
)
@@ -89,9 +92,7 @@ def __init__(
8992
self._compression = compression or _compression_from_env()
9093
self._session = session or requests.Session()
9194
self._session.headers.update(self._headers)
92-
self._session.headers.update(
93-
{"Content-Type": _ProtobufEncoder._CONTENT_TYPE}
94-
)
95+
self._session.headers.update(_OTLP_HTTP_HEADERS)
9596
if self._compression is not Compression.NoCompression:
9697
self._session.headers.update(
9798
{"Content-Encoding": self._compression.value}

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/encoder/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060

6161

6262
class _ProtobufEncoder:
63-
_CONTENT_TYPE = "application/x-protobuf"
64-
6563
@classmethod
6664
def serialize(cls, sdk_spans: Sequence[SDKSpan]) -> str:
6765
return cls.encode(sdk_spans).SerializeToString()

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ def test_constructor_default(self):
8484
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
8585
self.assertEqual(exporter._headers, {})
8686
self.assertIsInstance(exporter._session, requests.Session)
87+
self.assertIn("User-Agent", exporter._session.headers)
88+
self.assertEqual(
89+
exporter._session.headers.get("Content-Type"),
90+
"application/x-protobuf",
91+
)
8792

8893
@patch.dict(
8994
"os.environ",
@@ -154,11 +159,6 @@ def test_serialize(self):
154159
expected_encoding.SerializeToString(),
155160
)
156161

157-
def test_content_type(self):
158-
self.assertEqual(
159-
_ProtobufEncoder._CONTENT_TYPE, "application/x-protobuf"
160-
)
161-
162162
@staticmethod
163163
def _get_sdk_log_data() -> List[LogData]:
164164
log1 = LogData(

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def test_constructor_default(self):
5858
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
5959
self.assertEqual(exporter._headers, {})
6060
self.assertIsInstance(exporter._session, requests.Session)
61+
self.assertIn("User-Agent", exporter._session.headers)
62+
self.assertEqual(
63+
exporter._session.headers.get("Content-Type"),
64+
"application/x-protobuf",
65+
)
6166

6267
@patch.dict(
6368
"os.environ",

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_protobuf_encoder.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ def test_serialize(self):
6969
expected_encoding.SerializeToString(),
7070
)
7171

72-
def test_content_type(self):
73-
self.assertEqual(
74-
_ProtobufEncoder._CONTENT_TYPE, "application/x-protobuf"
75-
)
76-
7772
@staticmethod
7873
def get_exhaustive_otel_span_list() -> List[SDKSpan]:
7974
trace_id = 0x3E0C63257DE34C926F9EFCD03927272E

0 commit comments

Comments
 (0)