|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from unittest import TestCase |
| 6 | +from unittest.mock import ANY, MagicMock, PropertyMock, patch |
| 7 | + |
| 8 | +import requests |
| 9 | +from botocore.credentials import Credentials |
| 10 | + |
| 11 | +from amazon.opentelemetry.distro.exporter.otlp.aws.common.aws_auth_session import AwsAuthSession |
| 12 | +from amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter import OTLPAwsLogExporter |
| 13 | +from amazon.opentelemetry.distro.exporter.otlp.aws.traces.otlp_aws_span_exporter import OTLPAwsSpanExporter |
| 14 | +from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter |
| 15 | +from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( |
| 16 | + DEFAULT_COMPRESSION, |
| 17 | + DEFAULT_TIMEOUT, |
| 18 | + OTLPSpanExporter, |
| 19 | +) |
| 20 | +from opentelemetry.exporter.otlp.proto.http.version import __version__ |
| 21 | + |
| 22 | +AWS_OTLP_TRACES_ENDPOINT = "https://xray.us-east-1.amazonaws.com/v1/traces" |
| 23 | +AWS_OTLP_LOGS_ENDPOINT = "https://logs.us-east-1.amazonaws.com/v1/logs" |
| 24 | + |
| 25 | +USER_AGENT = "OTel-OTLP-Exporter-Python/" + __version__ |
| 26 | +CONTENT_TYPE = "application/x-protobuf" |
| 27 | +AUTHORIZATION_HEADER = "Authorization" |
| 28 | +X_AMZ_DATE_HEADER = "X-Amz-Date" |
| 29 | +X_AMZ_SECURITY_TOKEN_HEADER = "X-Amz-Security-Token" |
| 30 | + |
| 31 | +mock_credentials = Credentials(access_key="test_access_key", secret_key="test_secret_key", token="test_session_token") |
| 32 | + |
| 33 | + |
| 34 | +class TestAwsExporter(TestCase): |
| 35 | + |
| 36 | + def test_sigv4_exporter_init_default(self): |
| 37 | + """Tests that the default exporter is is still an instance of upstream's exporter""" |
| 38 | + |
| 39 | + test_cases = [ |
| 40 | + [OTLPAwsSpanExporter(endpoint=AWS_OTLP_TRACES_ENDPOINT), AWS_OTLP_TRACES_ENDPOINT, OTLPSpanExporter], |
| 41 | + [OTLPAwsLogExporter(endpoint=AWS_OTLP_LOGS_ENDPOINT), AWS_OTLP_LOGS_ENDPOINT, OTLPLogExporter], |
| 42 | + ] |
| 43 | + |
| 44 | + for tc in test_cases: |
| 45 | + self.validate_exporter_extends_http_exporter(exporter=tc[0], endpoint=tc[1], type=tc[2]) |
| 46 | + |
| 47 | + @patch("pkg_resources.get_distribution", side_effect=ImportError("test error")) |
| 48 | + @patch.dict("sys.modules", {"botocore": None}, clear=False) |
| 49 | + @patch("requests.Session.request", return_value=requests.Response()) |
| 50 | + def test_aws_auth_session_no_botocore(self, _, __): |
| 51 | + """Tests that aws_auth_session will not inject SigV4 Headers if botocore is not installed.""" |
| 52 | + |
| 53 | + session = AwsAuthSession("us-east-1", "xray") |
| 54 | + actual_headers = {"test": "test"} |
| 55 | + |
| 56 | + session.request("POST", AWS_OTLP_TRACES_ENDPOINT, data="", headers=actual_headers) |
| 57 | + |
| 58 | + self.assertNotIn(AUTHORIZATION_HEADER, actual_headers) |
| 59 | + self.assertNotIn(X_AMZ_DATE_HEADER, actual_headers) |
| 60 | + self.assertNotIn(X_AMZ_SECURITY_TOKEN_HEADER, actual_headers) |
| 61 | + |
| 62 | + @patch("requests.Session.request", return_value=requests.Response()) |
| 63 | + @patch("botocore.session.Session.get_credentials", return_value=None) |
| 64 | + def test_aws_auth_session_no_credentials(self, _, __): |
| 65 | + """Tests that aws_auth_session will not inject SigV4 Headers if retrieving credentials returns None.""" |
| 66 | + |
| 67 | + session = AwsAuthSession("us-east-1", "xray") |
| 68 | + actual_headers = {"test": "test"} |
| 69 | + |
| 70 | + session.request("POST", AWS_OTLP_TRACES_ENDPOINT, data="", headers=actual_headers) |
| 71 | + |
| 72 | + self.assertNotIn(AUTHORIZATION_HEADER, actual_headers) |
| 73 | + self.assertNotIn(X_AMZ_DATE_HEADER, actual_headers) |
| 74 | + self.assertNotIn(X_AMZ_SECURITY_TOKEN_HEADER, actual_headers) |
| 75 | + |
| 76 | + @patch("requests.Session.request", return_value=requests.Response()) |
| 77 | + @patch("botocore.session.Session.get_credentials", return_value=mock_credentials) |
| 78 | + def test_aws_auth_session(self, _, __): |
| 79 | + """Tests that aws_auth_session will inject SigV4 Headers if botocore is installed.""" |
| 80 | + |
| 81 | + session = AwsAuthSession("us-east-1", "xray") |
| 82 | + actual_headers = {"test": "test"} |
| 83 | + |
| 84 | + session.request("POST", AWS_OTLP_TRACES_ENDPOINT, data="", headers=actual_headers) |
| 85 | + |
| 86 | + self.assertIn(AUTHORIZATION_HEADER, actual_headers) |
| 87 | + self.assertIn(X_AMZ_DATE_HEADER, actual_headers) |
| 88 | + self.assertIn(X_AMZ_SECURITY_TOKEN_HEADER, actual_headers) |
| 89 | + |
| 90 | + def validate_exporter_extends_http_exporter(self, exporter, endpoint, type): |
| 91 | + self.assertIsInstance(exporter, type) |
| 92 | + self.assertIsInstance(exporter._session, AwsAuthSession) |
| 93 | + self.assertEqual(exporter._endpoint, endpoint) |
| 94 | + self.assertEqual(exporter._certificate_file, True) |
| 95 | + self.assertEqual(exporter._client_certificate_file, None) |
| 96 | + self.assertEqual(exporter._client_key_file, None) |
| 97 | + self.assertEqual(exporter._timeout, DEFAULT_TIMEOUT) |
| 98 | + self.assertIs(exporter._compression, DEFAULT_COMPRESSION) |
| 99 | + self.assertEqual(exporter._headers, {}) |
| 100 | + self.assertIn("User-Agent", exporter._session.headers) |
| 101 | + self.assertEqual( |
| 102 | + exporter._session.headers.get("Content-Type"), |
| 103 | + CONTENT_TYPE, |
| 104 | + ) |
| 105 | + self.assertEqual(exporter._session.headers.get("User-Agent"), USER_AGENT) |
0 commit comments