Skip to content

Commit

Permalink
exporter-otlp-proto-http: add user agent string (#2959)
Browse files Browse the repository at this point in the history
* `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 <srikanth.chekuri92@gmail.com>
Alex Boten and srikanthccv authored Oct 12, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6f6f8d1 commit 8927ae5
Showing 9 changed files with 30 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

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


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

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

0 comments on commit 8927ae5

Please sign in to comment.