Skip to content

Commit bb29f9c

Browse files
authored
Add custom ADOT UserAgent for OTLP Spans Exporter (#554)
*Description of changes:* - Added custom User-Agent header to AWS OTLP span exporter that adds "GenAI" when agent observability is enabled. <img width="1159" height="195" alt="image" src="https://github.com/user-attachments/assets/65a65bfc-4ba3-4555-8c07-4c05335f73cc" /> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 5b76598 commit bb29f9c

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ For any change that affects end users of this package, please add an entry under
1111
If your change does not need a CHANGELOG entry, add the "skip changelog" label to your PR.
1212

1313
## Unreleased
14+
- Add custom ADOT UserAgent for OTLP Spans Exporter
15+
([#554](https://github.com/aws-observability/aws-otel-python-instrumentation/pull/554))
1416
- Disable django instrumentation if DJANGO_SETTINGS_MODULE is not set
1517
([#549](https://github.com/aws-observability/aws-otel-python-instrumentation/pull/549))
1618
- [PATCH] Add safety check for bedrock ConverseStream responses
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from amazon.opentelemetry.distro._utils import is_agent_observability_enabled
5+
from amazon.opentelemetry.distro.version import __version__
6+
7+
8+
def build_user_agent() -> str:
9+
user_agent = f"ADOT-OTLP-Exporter-Python/{__version__}"
10+
11+
if is_agent_observability_enabled():
12+
user_agent = f"ADOT-GenAI-OTLP-Exporter-Python/{__version__}"
13+
return user_agent
14+
15+
16+
_OTLP_AWS_HTTP_HEADERS = {
17+
"User-Agent": build_user_agent(),
18+
}

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/exporter/otlp/aws/traces/otlp_aws_span_exporter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from botocore.session import Session
88

99
from amazon.opentelemetry.distro._utils import is_agent_observability_enabled
10+
from amazon.opentelemetry.distro.exporter.otlp.aws.common._aws_http_headers import _OTLP_AWS_HTTP_HEADERS
1011
from amazon.opentelemetry.distro.exporter.otlp.aws.common.aws_auth_session import AwsAuthSession
1112
from amazon.opentelemetry.distro.llo_handler import LLOHandler
1213
from opentelemetry._logs import get_logger_provider
@@ -56,6 +57,7 @@ def __init__(
5657
compression,
5758
session=AwsAuthSession(session=session, aws_region=self._aws_region, service="xray"),
5859
)
60+
self._session.headers.update(_OTLP_AWS_HTTP_HEADERS)
5961

6062
def _ensure_llo_handler(self):
6163
"""Lazily initialize LLO handler when needed to avoid initialization order issues"""

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/exporter/otlp/aws/traces/test_otlp_aws_span_exporter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest.mock import MagicMock, patch
66

77
from amazon.opentelemetry.distro._utils import get_aws_session
8+
from amazon.opentelemetry.distro.exporter.otlp.aws.common._aws_http_headers import _OTLP_AWS_HTTP_HEADERS
89
from amazon.opentelemetry.distro.exporter.otlp.aws.traces.otlp_aws_span_exporter import OTLPAwsSpanExporter
910
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
1011
from opentelemetry.sdk._logs import LoggerProvider
@@ -35,6 +36,20 @@ def test_init_without_logger_provider(self):
3536
self.assertEqual(exporter._aws_region, "us-west-2")
3637
self.assertIsNone(exporter._llo_handler)
3738

39+
def test_aws_headers_applied(self):
40+
endpoint = "https://xray.us-east-1.amazonaws.com/v1/traces"
41+
custom_headers = {"X-Custom-Header": "custom-value"}
42+
43+
exporter = OTLPAwsSpanExporter(
44+
session=get_aws_session(), aws_region="us-east-1", endpoint=endpoint, headers=custom_headers
45+
)
46+
47+
for key in _OTLP_AWS_HTTP_HEADERS.keys():
48+
self.assertIn(key, exporter._session.headers)
49+
50+
self.assertEqual(exporter._session.headers["X-Custom-Header"], "custom-value")
51+
self.assertIn("User-Agent", exporter._session.headers)
52+
3853
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.traces.otlp_aws_span_exporter.is_agent_observability_enabled")
3954
def test_ensure_llo_handler_when_disabled(self, mock_is_enabled):
4055
# Test _ensure_llo_handler when agent observability is disabled

0 commit comments

Comments
 (0)