13
13
DEFAULT_ENDPOINT ,
14
14
DEFAULT_TIMEOUT ,
15
15
DEFAULT_TRACES_EXPORT_PATH ,
16
+ OTLPSpanExporter ,
16
17
)
17
18
from opentelemetry .exporter .otlp .proto .http .version import __version__
18
19
from opentelemetry .sdk .environment_variables import OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
@@ -42,24 +43,29 @@ def setUp(self):
42
43
"https://xray.us-east-1.amaz.com/v1/traces" ,
43
44
"https://logs.us-east-1.amazonaws.com/v1/logs" ,
44
45
"https://test-endpoint123.com/test" ,
46
+ "xray.us-east-1.amazonaws.com/v1/traces" ,
47
+ "https://test-endpoint123.com/test https://xray.us-east-1.amazonaws.com/v1/traces" ,
48
+ "https://xray.us-east-1.amazonaws.com/v1/tracesssda" ,
45
49
]
46
50
47
51
self .expected_auth_header = "AWS4-HMAC-SHA256 Credential=test_key/some_date/us-east-1/xray/aws4_request"
48
52
self .expected_auth_x_amz_date = "some_date"
49
53
self .expected_auth_security_token = "test_token"
50
54
51
- # Tests that the default exporter is OTLP protobuf/http Span Exporter if no endpoint is set
52
55
@patch .dict (os .environ , {}, clear = True )
53
56
def test_sigv4_exporter_init_default (self ):
57
+ """Tests that the default exporter is OTLP protobuf/http Span Exporter if no endpoint is set"""
58
+
54
59
exporter = OTLPAwsSigV4Exporter ()
55
60
self .validate_exporter_extends_http_span_exporter (exporter , DEFAULT_ENDPOINT + DEFAULT_TRACES_EXPORT_PATH )
56
61
self .assertIsInstance (exporter ._session , requests .Session )
57
62
58
- # Tests that the endpoint is validated and sets the aws_region but still uses the OTLP protobuf/http
59
- # Span Exporter exporter constructor behavior if a valid OTLP CloudWatch endpoint is set
60
63
@patch .dict (os .environ , {OTEL_EXPORTER_OTLP_TRACES_ENDPOINT : OTLP_CW_ENDPOINT }, clear = True )
61
64
@patch ("botocore.session.Session" )
62
65
def test_sigv4_exporter_init_valid_cw_otlp_endpoint (self , session_mock ):
66
+ """Tests that the endpoint is validated and sets the aws_region but still uses the OTLP protobuf/http
67
+ Span Exporter exporter constructor behavior if a valid OTLP CloudWatch endpoint is set."""
68
+
63
69
mock_session = MagicMock ()
64
70
session_mock .return_value = mock_session
65
71
@@ -71,12 +77,18 @@ def test_sigv4_exporter_init_valid_cw_otlp_endpoint(self, session_mock):
71
77
72
78
mock_session .get_available_regions .assert_called_once_with ("xray" )
73
79
74
- # Tests that the exporter constructor behavior
75
- # is set by OTLP protobuf/http Span Exporter
76
- # if an invalid OTLP CloudWatch endpoint is set
80
+ @patch .dict ("sys.modules" , {"botocore" : None })
81
+ def test_no_botocore_valid_xray_endpoint (self ):
82
+ """Test that exporter defaults when using OTLP CW endpoint without botocore"""
83
+
84
+ exporter = OTLPAwsSigV4Exporter (endpoint = OTLP_CW_ENDPOINT )
85
+
86
+ self .assertIsNone (exporter ._aws_region )
87
+
77
88
@patch ("botocore.session.Session" )
78
89
def test_sigv4_exporter_init_invalid_cw_otlp_endpoint (self , botocore_mock ):
79
-
90
+ """Tests that the exporter constructor behavior is set by OTLP protobuf/http Span Exporter
91
+ if an invalid OTLP CloudWatch endpoint is set"""
80
92
for bad_endpoint in self .invalid_cw_otlp_tracing_endpoints :
81
93
with self .subTest (endpoint = bad_endpoint ):
82
94
with patch .dict (os .environ , {OTEL_EXPORTER_OTLP_TRACES_ENDPOINT : bad_endpoint }):
@@ -90,15 +102,15 @@ def test_sigv4_exporter_init_invalid_cw_otlp_endpoint(self, botocore_mock):
90
102
91
103
self .assertIsNone (exporter ._aws_region )
92
104
93
- # Tests that if the OTLP endpoint is not a valid CW endpoint but the credentials are valid,
94
- # SigV4 authentication method is NOT called and is
95
- # NOT injected into the existing Session headers.
96
105
@patch ("botocore.session.Session.get_available_regions" )
97
106
@patch ("requests.Session.post" )
98
107
@patch ("botocore.auth.SigV4Auth.add_auth" )
99
108
def test_sigv4_exporter_export_does_not_add_sigv4_if_not_valid_cw_endpoint (
100
109
self , mock_sigv4_auth , requests_mock , botocore_mock
101
110
):
111
+ """Tests that if the OTLP endpoint is not a valid CW endpoint but the credentials are valid,
112
+ SigV4 authentication method is NOT called and is NOT injected into the existing Session headers."""
113
+
102
114
# Setting the exporter response
103
115
mock_response = MagicMock ()
104
116
mock_response .status_code = 200
@@ -155,17 +167,16 @@ def test_sigv4_exporter_export_does_not_add_sigv4_if_not_valid_cw_endpoint(
155
167
cert = ANY ,
156
168
)
157
169
158
- # Tests that if the OTLP endpoint is a valid CW endpoint but no credentials are returned,
159
- # SigV4 authentication method is NOT called and is NOT
160
- # injected into the existing Session headers.
161
170
@patch ("botocore.session.Session" )
162
171
@patch ("requests.Session" )
163
172
@patch ("botocore.auth.SigV4Auth.add_auth" )
164
173
@patch .dict (os .environ , {OTEL_EXPORTER_OTLP_TRACES_ENDPOINT : OTLP_CW_ENDPOINT })
165
174
def test_sigv4_exporter_export_does_not_add_sigv4_if_not_valid_credentials (
166
175
self , mock_sigv4_auth , requests_posts_mock , botocore_mock
167
176
):
168
-
177
+ """Tests that if the OTLP endpoint is a valid CW endpoint but no credentials are returned,
178
+ SigV4 authentication method is NOT called and is NOT injected into the existing
179
+ Session headers."""
169
180
# Setting the exporter response
170
181
mock_response = MagicMock ()
171
182
mock_response .status_code = 200
@@ -201,16 +212,16 @@ def test_sigv4_exporter_export_does_not_add_sigv4_if_not_valid_credentials(
201
212
self .assertNotIn (X_AMZ_DATE_HEADER , actual_headers )
202
213
self .assertNotIn (X_AMZ_SECURITY_TOKEN_HEADER , actual_headers )
203
214
204
- # Tests that if the OTLP endpoint is valid and credentials are valid,
205
- # SigV4 authentication method is called and is
206
- # injected into the existing Session headers.
207
215
@patch ("botocore.session.Session" )
208
216
@patch ("requests.Session" )
209
217
@patch ("botocore.auth.SigV4Auth.add_auth" )
210
218
@patch .dict (os .environ , {OTEL_EXPORTER_OTLP_TRACES_ENDPOINT : OTLP_CW_ENDPOINT })
211
219
def test_sigv4_exporter_export_adds_sigv4_authentication_if_valid_cw_endpoint (
212
220
self , mock_sigv4_auth , requests_posts_mock , botocore_mock
213
221
):
222
+ """Tests that if the OTLP endpoint is valid and credentials are valid,
223
+ SigV4 authentication method is called and is
224
+ injected into the existing Session headers."""
214
225
215
226
# Setting the exporter response
216
227
mock_response = MagicMock ()
@@ -251,6 +262,7 @@ def test_sigv4_exporter_export_adds_sigv4_authentication_if_valid_cw_endpoint(
251
262
self .assertEqual (actual_headers [X_AMZ_SECURITY_TOKEN_HEADER ], self .expected_auth_security_token )
252
263
253
264
def validate_exporter_extends_http_span_exporter (self , exporter , endpoint ):
265
+ self .assertIsInstance (exporter , OTLPSpanExporter )
254
266
self .assertEqual (exporter ._endpoint , endpoint )
255
267
self .assertEqual (exporter ._certificate_file , True )
256
268
self .assertEqual (exporter ._client_certificate_file , None )
0 commit comments