Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporter-otlp-proto-http: add user agent string #2959

Merged
merged 6 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@


class _ProtobufEncoder:
_CONTENT_TYPE = "application/x-protobuf"

@classmethod
def serialize(cls, batch: Sequence[LogData]) -> str:
return cls.encode(batch).SerializeToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down