Skip to content

Commit 529079c

Browse files
committed
add unit testing + formatting
1 parent 329007f commit 529079c

File tree

4 files changed

+108
-210
lines changed

4 files changed

+108
-210
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/exporter/otlp/aws/common/aws_auth_session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class AwsAuthSession(requests.Session):
1111

1212
def __init__(self, aws_region, service):
1313

14+
self._has_required_dependencies = False
15+
1416
# Requires botocore to be installed to sign the headers. However,
1517
# some users might not need to use this exporter. In order not conflict
1618
# with existing behavior, we check for botocore before initializing this exporter.
@@ -62,6 +64,5 @@ def request(self, method, url, data=None, headers=None, *args, **kwargs):
6264

6365
return super().request(method, url, data=data, headers=headers, *args, **kwargs)
6466

65-
6667
def close(self):
6768
super().close()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
ApplicationSignalsExporterProvider,
1616
AwsOpenTelemetryConfigurator,
1717
_custom_import_sampler,
18-
_customize_span_exporter,
1918
_customize_metric_exporters,
2019
_customize_sampler,
20+
_customize_span_exporter,
2121
_customize_span_processors,
2222
_export_unsampled_span_for_lambda,
2323
_is_application_signals_enabled,
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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)

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

Lines changed: 0 additions & 208 deletions
This file was deleted.

0 commit comments

Comments
 (0)