Skip to content

Commit 667ac52

Browse files
committed
removed is xray otlp endpoint validation in the span exporter
1 parent 01e74f8 commit 667ac52

File tree

4 files changed

+27
-102
lines changed

4 files changed

+27
-102
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_utils.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@
99

1010
_logger: Logger = getLogger(__name__)
1111

12-
XRAY_OTLP_ENDPOINT_PATTERN = r"https://xray\.([a-z0-9-]+)\.amazonaws\.com/v1/traces$"
13-
14-
15-
def is_xray_otlp_endpoint(otlp_endpoint: str = None) -> bool:
16-
"""Is the given endpoint the XRay OTLP endpoint?"""
17-
18-
if not otlp_endpoint:
19-
return False
20-
21-
return bool(re.match(XRAY_OTLP_ENDPOINT_PATTERN, otlp_endpoint.lower()))
22-
2312

2413
def is_installed(req: str) -> bool:
2514
"""Is the given required package installed?"""

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
44
import os
5+
import re
56
from logging import Logger, getLogger
67
from typing import ClassVar, Dict, List, Type, Union
78

@@ -10,7 +11,6 @@
1011

1112
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_LOCAL_SERVICE
1213
from amazon.opentelemetry.distro._aws_resource_attribute_configurator import get_service_attribute
13-
from amazon.opentelemetry.distro._utils import is_xray_otlp_endpoint
1414
from amazon.opentelemetry.distro.always_record_sampler import AlwaysRecordSampler
1515
from amazon.opentelemetry.distro.attribute_propagating_span_processor_builder import (
1616
AttributePropagatingSpanProcessorBuilder,
@@ -83,6 +83,7 @@
8383
OTEL_AWS_PYTHON_DEFER_TO_WORKERS_ENABLED_CONFIG = "OTEL_AWS_PYTHON_DEFER_TO_WORKERS_ENABLED"
8484
SYSTEM_METRICS_INSTRUMENTATION_SCOPE_NAME = "opentelemetry.instrumentation.system_metrics"
8585
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
86+
XRAY_OTLP_ENDPOINT_PATTERN = r"https://xray\.([a-z0-9-]+)\.amazonaws\.com/v1/traces$"
8687
# UDP package size is not larger than 64KB
8788
LAMBDA_SPAN_EXPORT_BATCH_SIZE = 10
8889

@@ -448,6 +449,15 @@ def _is_lambda_environment():
448449
return AWS_LAMBDA_FUNCTION_NAME_CONFIG in os.environ
449450

450451

452+
def is_xray_otlp_endpoint(otlp_endpoint: str = None) -> bool:
453+
"""Is the given endpoint the XRay OTLP endpoint?"""
454+
455+
if not otlp_endpoint:
456+
return False
457+
458+
return bool(re.match(XRAY_OTLP_ENDPOINT_PATTERN, otlp_endpoint.lower()))
459+
460+
451461
def _get_metric_export_interval():
452462
export_interval_millis = float(os.environ.get(METRIC_EXPORT_INTERVAL_CONFIG, DEFAULT_METRIC_EXPORT_INTERVAL))
453463
_logger.debug("Span Metrics export interval: %s", export_interval_millis)

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/otlp_aws_span_exporter.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import requests
77

8-
from amazon.opentelemetry.distro._utils import is_installed, is_xray_otlp_endpoint
8+
from amazon.opentelemetry.distro._utils import is_installed
99
from opentelemetry.exporter.otlp.proto.http import Compression
1010
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
1111

@@ -40,22 +40,24 @@ def __init__(
4040
# some users might not need to use this exporter. In order not conflict
4141
# with existing behavior, we check for botocore before initializing this exporter.
4242

43-
if endpoint and is_xray_otlp_endpoint(endpoint):
43+
if endpoint and is_installed("botocore"):
44+
# pylint: disable=import-outside-toplevel
45+
from botocore import auth, awsrequest, session
4446

45-
if is_installed("botocore"):
46-
# pylint: disable=import-outside-toplevel
47-
from botocore import auth, awsrequest, session
47+
self.boto_auth = auth
48+
self.boto_aws_request = awsrequest
49+
self.boto_session = session.Session()
4850

49-
self.boto_auth = auth
50-
self.boto_aws_request = awsrequest
51-
self.boto_session = session.Session()
52-
self._aws_region = endpoint.split(".")[1]
51+
# Assumes only valid endpoints passed are of XRay OTLP format.
52+
# The only usecase for this class would be for ADOT Python Auto Instrumentation and that already validates
53+
# the endpoint to be an XRay OTLP endpoint.
54+
self._aws_region = endpoint.split(".")[1]
5355

54-
else:
55-
_logger.error(
56-
"botocore is required to export traces to %s. Please install it using `pip install botocore`",
57-
endpoint,
58-
)
56+
else:
57+
_logger.error(
58+
"botocore is required to export traces to %s. Please install it using `pip install botocore`",
59+
endpoint,
60+
)
5961

6062
super().__init__(
6163
endpoint=endpoint,

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_otlp_aws_span_exporter.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ def setUp(self):
3838
self.create_span("test_span5", SpanKind.CONSUMER),
3939
]
4040

41-
self.invalid_otlp_tracing_endpoints = [
42-
"https://xray.us-east-1.amaz.com/v1/traces",
43-
"https://logs.us-east-1.amazonaws.com/v1/logs",
44-
"https://test-endpoint123.com/test",
45-
"xray.us-east-1.amazonaws.com/v1/traces",
46-
"https://test-endpoint123.com/test https://xray.us-east-1.amazonaws.com/v1/traces",
47-
"https://xray.us-east-1.amazonaws.com/v1/tracesssda",
48-
]
49-
5041
self.expected_auth_header = "AWS4-HMAC-SHA256 Credential=test_key/some_date/us-east-1/xray/aws4_request"
5142
self.expected_auth_x_amz_date = "some_date"
5243
self.expected_auth_security_token = "test_token"
@@ -88,73 +79,6 @@ def test_sigv4_exporter_init_valid_cw_otlp_endpoint(self, session_mock):
8879
self.assertEqual(exporter._aws_region, "us-east-1")
8980
self.validate_exporter_extends_http_span_exporter(exporter, OTLP_XRAY_ENDPOINT)
9081

91-
def test_sigv4_exporter_init_invalid_cw_otlp_endpoint(self):
92-
"""Tests that the exporter constructor behavior is set by OTLP protobuf/http Span Exporter
93-
if an invalid OTLP CloudWatch endpoint is set"""
94-
for bad_endpoint in self.invalid_otlp_tracing_endpoints:
95-
with self.subTest(endpoint=bad_endpoint):
96-
with patch.dict(os.environ, {OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: bad_endpoint}):
97-
exporter = OTLPAwsSpanExporter(endpoint=bad_endpoint)
98-
self.validate_exporter_extends_http_span_exporter(exporter, bad_endpoint)
99-
100-
self.assertIsNone(exporter._aws_region)
101-
102-
@patch("requests.Session.post")
103-
@patch("botocore.auth.SigV4Auth.add_auth")
104-
@patch("botocore.session.Session")
105-
def test_sigv4_exporter_export_does_not_add_sigv4_if_not_valid_cw_endpoint(
106-
self, botocore_mock, mock_sigv4_auth, requests_mock
107-
):
108-
"""Tests that if the OTLP endpoint is not a valid XRay endpoint but the credentials are valid,
109-
SigV4 authentication method is called but fails so NO headers are injected into the existing Session headers."""
110-
111-
# Setting the exporter response
112-
mock_response = MagicMock()
113-
mock_response.status_code = 200
114-
type(mock_response).ok = PropertyMock(return_value=True)
115-
116-
# Setting the request session headers to make the call to endpoint
117-
mock_session = MagicMock()
118-
mock_session.headers = {"User-Agent": USER_AGENT, "Content-Type": CONTENT_TYPE}
119-
requests_mock.return_value = mock_session
120-
mock_session.post.return_value = mock_response
121-
122-
# SigV4 mock authentication injection
123-
mock_sigv4_auth.side_effect = self.mock_add_auth
124-
125-
mock_botocore_session = MagicMock()
126-
botocore_mock.return_value = mock_botocore_session
127-
128-
mock_botocore_session.get_credentials.return_value = Credentials(
129-
access_key="test_key", secret_key="test_secret", token="test_token"
130-
)
131-
132-
# For each invalid CW OTLP endpoint, validate that SigV4 is not injected
133-
self.invalid_otlp_tracing_endpoints.append("https://xray.bad-region-1.amazonaws.com/v1/traces")
134-
for bad_endpoint in self.invalid_otlp_tracing_endpoints:
135-
with self.subTest(endpoint=bad_endpoint):
136-
with patch.dict(os.environ, {OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: bad_endpoint}):
137-
138-
exporter = OTLPAwsSpanExporter(endpoint=bad_endpoint)
139-
140-
self.validate_exporter_extends_http_span_exporter(exporter, bad_endpoint)
141-
142-
exporter.export(self.testing_spans)
143-
144-
# Verify that SigV4 request headers were not injected
145-
actual_headers = mock_session.headers
146-
self.assertNotIn(AUTHORIZATION_HEADER, actual_headers)
147-
self.assertNotIn(X_AMZ_DATE_HEADER, actual_headers)
148-
self.assertNotIn(X_AMZ_SECURITY_TOKEN_HEADER, actual_headers)
149-
150-
requests_mock.assert_called_with(
151-
url=bad_endpoint,
152-
data=ANY,
153-
verify=ANY,
154-
timeout=ANY,
155-
cert=ANY,
156-
)
157-
15882
@patch("botocore.session.Session")
15983
@patch("requests.Session")
16084
@patch("botocore.auth.SigV4Auth.add_auth")

0 commit comments

Comments
 (0)