Skip to content

Commit

Permalink
Adds user agent string to grpc headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pridhi-arora committed Nov 14, 2022
1 parent 787e499 commit c7dd38b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2997](https://github.com/open-telemetry/opentelemetry-python/pull/2997))
- Fix a bug with exporter retries for with newer versions of the backoff library
([#2980](https://github.com/open-telemetry/opentelemetry-python/pull/2980))
- [exporter-otlp-proto-grpc] add user agent string
([#3009](https://github.com/open-telemetry/opentelemetry-python/pull/3009))

## Version 1.13.0/0.34b0 (2022-09-26)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@
API
---
"""
from .version import __version__

otel_string = "OTel OTLP Exporter Python/" + __version__
_OTLP_GRPC_HEADERS = [("user-agent", otel_string)]
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.util.re import parse_headers
from opentelemetry.sdk.metrics.export import MetricsData
from opentelemetry.exporter.otlp.proto.grpc import (
_OTLP_GRPC_HEADERS,
)

logger = getLogger(__name__)
SDKDataT = TypeVar("SDKDataT")
Expand Down Expand Up @@ -251,6 +254,10 @@ def __init__(
self._headers = tuple(temp_headers.items())
elif isinstance(self._headers, dict):
self._headers = tuple(self._headers.items())
if self._headers is None:
self._headers = tuple(_OTLP_GRPC_HEADERS)
else:
self._headers = self._headers + tuple(_OTLP_GRPC_HEADERS)

self._timeout = timeout or int(
environ.get(OTEL_EXPORTER_OTLP_TIMEOUT, 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.version import __version__

from opentelemetry.exporter.otlp.proto.grpc.exporter import _translate_value
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import (
ExportLogsServiceRequest,
Expand All @@ -48,6 +50,9 @@
from opentelemetry.sdk._logs.severity import (
SeverityNumber as SDKSeverityNumber,
)
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_LOG_HEADERS,
)
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.trace import TraceFlags
Expand Down Expand Up @@ -173,11 +178,8 @@ def test_exporting(self):
"opentelemetry.exporter.otlp.proto.grpc._log_exporter.OTLPLogExporter._stub"
)
# pylint: disable=unused-argument
def test_no_credentials_error(
self, mock_ssl_channel, mock_secure, mock_stub
):
OTLPLogExporter(insecure=False)
self.assertTrue(mock_ssl_channel.called)
def test_header_agent(self):
self.exporter

# pylint: disable=no-self-use
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
Expand Down Expand Up @@ -251,6 +253,15 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
)
mock_method.reset_mock()

@patch.dict(
"os.environ",
{OTEL_EXPORTER_OTLP_LOG_HEADERS: " key1=value1,KEY2 = VALUE=2 "},
)
def test_otlp_headers_from_env(self):
self.assertEqual(
self.exporter._headers, (("key1", "value1"),("key2", "VALUE=2"), ("user-agent", "OTel OTLP Exporter Python/" + __version__))
)

@patch("opentelemetry.exporter.otlp.proto.grpc.exporter._expo")
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep")
def test_unavailable(self, mock_sleep, mock_expo):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
)
from opentelemetry.test.metrictestutil import _generate_gauge, _generate_sum

from opentelemetry.exporter.otlp.proto.grpc.version import __version__


THIS_DIR = dirname(__file__)


Expand Down Expand Up @@ -413,14 +416,14 @@ def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure):
exporter = OTLPMetricExporter()
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key1", "value1"), ("key2", "VALUE=2"))
exporter._headers, (("key1", "value1"), ("key2", "VALUE=2"),("user-agent", "OTel OTLP Exporter Python/" + __version__))
)
exporter = OTLPMetricExporter(
headers=(("key3", "value3"), ("key4", "value4"))
)
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key3", "value3"), ("key4", "value4"))
exporter._headers, (("key3", "value3"), ("key4", "value4"),("user-agent", "OTel OTLP Exporter Python/" + __version__))
)

@patch.dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
from opentelemetry.test.spantestutil import (
get_span_with_dropped_attributes_events_links,
)
from opentelemetry.exporter.otlp.proto.grpc.version import __version__


THIS_DIR = os.path.dirname(__file__)

Expand Down Expand Up @@ -275,21 +277,21 @@ def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure):
exporter = OTLPSpanExporter()
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key1", "value1"), ("key2", "VALUE=2"))
exporter._headers, (("key1", "value1"),("key2", "VALUE=2"), ("user-agent", "OTel OTLP Exporter Python/" + __version__))
)
exporter = OTLPSpanExporter(
headers=(("key3", "value3"), ("key4", "value4"))
)
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key3", "value3"), ("key4", "value4"))
exporter._headers, (("key3", "value3"), ("key4", "value4"), ("user-agent", "OTel OTLP Exporter Python/" + __version__))
)
exporter = OTLPSpanExporter(
headers={"key5": "value5", "key6": "value6"}
)
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key5", "value5"), ("key6", "value6"))
exporter._headers, (("key5", "value5"), ("key6", "value6"), ("user-agent", "OTel OTLP Exporter Python/" + __version__))
)

@patch.dict(
Expand Down Expand Up @@ -434,7 +436,8 @@ def test_otlp_exporter_otlp_compression_precendence(
def test_otlp_headers(self, mock_ssl_channel, mock_secure):
exporter = OTLPSpanExporter()
# pylint: disable=protected-access
self.assertIsNone(exporter._headers, None)
#This ensures that there is no other header than standard user-agent.
self.assertEqual(exporter._headers, (("user-agent", "OTel OTLP Exporter Python/" + __version__),))

@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.backoff")
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@
associated with gRPC or HTTP requests.
"""

OTEL_EXPORTER_OTLP_LOG_HEADERS = "OTEL_EXPORTER_OTLP_LOG_HEADERS"
"""
.. envvar:: OTEL_EXPORTER_OTLP_LOG_HEADERS
The :envvar:`OTEL_EXPORTER_OTLP_LOG_HEADERS` contains the key-value pairs to be used as headers for metrics
associated with gRPC or HTTP requests.
"""

OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION"
"""
.. envvar:: OTEL_EXPORTER_OTLP_TRACES_COMPRESSION
Expand Down

0 comments on commit c7dd38b

Please sign in to comment.