From 8927ae5085c5cc3d62603e7bdd473f34abf882ff Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Wed, 12 Oct 2022 05:07:08 -0700 Subject: [PATCH] `exporter-otlp-proto-http`: add user agent string (#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 #2958 * update changelog * fix linting, fix link Co-authored-by: Srikanth Chekuri --- CHANGELOG.md | 2 ++ .../opentelemetry/exporter/otlp/proto/http/__init__.py | 8 ++++++++ .../exporter/otlp/proto/http/_log_exporter/__init__.py | 9 +++++---- .../otlp/proto/http/_log_exporter/encoder/__init__.py | 2 -- .../otlp/proto/http/trace_exporter/__init__.py | 9 +++++---- .../otlp/proto/http/trace_exporter/encoder/__init__.py | 2 -- .../tests/test_proto_log_exporter.py | 10 +++++----- .../tests/test_proto_span_exporter.py | 5 +++++ .../tests/test_protobuf_encoder.py | 5 ----- 9 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d3b1d2fedb..879ec025985 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update explicit histogram bucket boundaries ([#2947](https://github.com/open-telemetry/opentelemetry-python/pull/2947)) +- `exporter-otlp-proto-http`: add user agent string + ([#2959](https://github.com/open-telemetry/opentelemetry-python/pull/2959)) ## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0) - 2022-09-26 diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/__init__.py index 08b07258351..a14f0e2992d 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/__init__.py @@ -71,6 +71,14 @@ """ import enum +from .version import __version__ + + +_OTLP_HTTP_HEADERS = { + "Content-Type": "application/x-protobuf", + "User-Agent": "OTel OTLP Exporter Python/" + __version__, +} + class Compression(enum.Enum): NoCompression = "none" diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py index 041f1ab3c07..a74f849f406 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py @@ -35,7 +35,10 @@ LogExportResult, LogData, ) -from opentelemetry.exporter.otlp.proto.http import Compression +from opentelemetry.exporter.otlp.proto.http import ( + _OTLP_HTTP_HEADERS, + Compression, +) from opentelemetry.exporter.otlp.proto.http._log_exporter.encoder import ( _ProtobufEncoder, ) @@ -78,9 +81,7 @@ def __init__( self._compression = compression or _compression_from_env() self._session = session or requests.Session() self._session.headers.update(self._headers) - self._session.headers.update( - {"Content-Type": _ProtobufEncoder._CONTENT_TYPE} - ) + self._session.headers.update(_OTLP_HTTP_HEADERS) if self._compression is not Compression.NoCompression: self._session.headers.update( {"Content-Encoding": self._compression.value} diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/encoder/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/encoder/__init__.py index bf8784aacf8..c8f2dd84564 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/encoder/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/encoder/__init__.py @@ -36,8 +36,6 @@ class _ProtobufEncoder: - _CONTENT_TYPE = "application/x-protobuf" - @classmethod def serialize(cls, batch: Sequence[LogData]) -> str: return cls.encode(batch).SerializeToString() diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py index 3c95a325b8d..a65bc44320f 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py @@ -36,7 +36,10 @@ OTEL_EXPORTER_OTLP_TIMEOUT, ) from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult -from opentelemetry.exporter.otlp.proto.http import Compression +from opentelemetry.exporter.otlp.proto.http import ( + _OTLP_HTTP_HEADERS, + Compression, +) from opentelemetry.exporter.otlp.proto.http.trace_exporter.encoder import ( _ProtobufEncoder, ) @@ -89,9 +92,7 @@ def __init__( self._compression = compression or _compression_from_env() self._session = session or requests.Session() self._session.headers.update(self._headers) - self._session.headers.update( - {"Content-Type": _ProtobufEncoder._CONTENT_TYPE} - ) + self._session.headers.update(_OTLP_HTTP_HEADERS) if self._compression is not Compression.NoCompression: self._session.headers.update( {"Content-Encoding": self._compression.value} diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/encoder/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/encoder/__init__.py index fc0d9608ef2..c1c9fe88643 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/encoder/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/encoder/__init__.py @@ -60,8 +60,6 @@ class _ProtobufEncoder: - _CONTENT_TYPE = "application/x-protobuf" - @classmethod def serialize(cls, sdk_spans: Sequence[SDKSpan]) -> str: return cls.encode(sdk_spans).SerializeToString() diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py index d5e34b7463d..2063820b5d2 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py @@ -84,6 +84,11 @@ def test_constructor_default(self): self.assertIs(exporter._compression, DEFAULT_COMPRESSION) self.assertEqual(exporter._headers, {}) self.assertIsInstance(exporter._session, requests.Session) + self.assertIn("User-Agent", exporter._session.headers) + self.assertEqual( + exporter._session.headers.get("Content-Type"), + "application/x-protobuf", + ) @patch.dict( "os.environ", @@ -154,11 +159,6 @@ def test_serialize(self): expected_encoding.SerializeToString(), ) - def test_content_type(self): - self.assertEqual( - _ProtobufEncoder._CONTENT_TYPE, "application/x-protobuf" - ) - @staticmethod def _get_sdk_log_data() -> List[LogData]: log1 = LogData( diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py index 4eb0db6160c..73e54e86c0b 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py @@ -58,6 +58,11 @@ def test_constructor_default(self): self.assertIs(exporter._compression, DEFAULT_COMPRESSION) self.assertEqual(exporter._headers, {}) self.assertIsInstance(exporter._session, requests.Session) + self.assertIn("User-Agent", exporter._session.headers) + self.assertEqual( + exporter._session.headers.get("Content-Type"), + "application/x-protobuf", + ) @patch.dict( "os.environ", diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_protobuf_encoder.py b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_protobuf_encoder.py index b3718623c18..7145ddbfa97 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_protobuf_encoder.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_protobuf_encoder.py @@ -69,11 +69,6 @@ def test_serialize(self): expected_encoding.SerializeToString(), ) - def test_content_type(self): - self.assertEqual( - _ProtobufEncoder._CONTENT_TYPE, "application/x-protobuf" - ) - @staticmethod def get_exhaustive_otel_span_list() -> List[SDKSpan]: trace_id = 0x3E0C63257DE34C926F9EFCD03927272E