generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 27
SigV4 Authentication support for http/protobuf exporter #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
b629da3
added sigv4 authentication to otlp exporter
liustve bd4e1d6
added unit tests
liustve 7187839
removed logging
liustve 6422607
more testing
liustve 6462b66
Merge branch 'aws-observability:main' into sigv4_support
liustve a34e899
added extra test
liustve 5fc6cfb
fixing sanitation issue
liustve db6d384
formatting
liustve 579efb3
fix arbitrary url error
liustve f97fd24
linting imports
liustve 451f194
linting fix
liustve 6ce4d68
linting fix
liustve 364f9de
linting fix
liustve f217ed1
lint fix
liustve 5637278
linting fix
liustve 8e1d0eb
lint fix
liustve bb591a2
linting fix
liustve ad4c0a0
linting fix
liustve f943b07
made botocore an optional dependency if not using otlp cw endpoint
liustve c796162
comments + linting fix
liustve 0b65642
linting fix
liustve 561fa01
linting fix
liustve 4a46f99
Merge branch 'main' into sigv4_support
liustve bba778d
addressing comments
liustve 1c8004c
linting fix
liustve de0e89f
linting fix
liustve c207167
tests + linting fix
liustve 556b037
renaming
liustve b7749f8
lint
liustve e9bf1f1
linting + test fix
liustve 56d7470
linting fix
liustve 532ab25
linting fix
liustve 6cb0f55
fixed test
liustve 9a47ab3
lint fix + test fix
liustve 407bcdc
linting fix
liustve 46d7586
changed to broader exception
liustve 01e74f8
linting fix
liustve 667ac52
removed is xray otlp endpoint validation in the span exporter
liustve 1b33012
linting fix
liustve c7d8410
removed unused import
liustve 5aa970e
Merge branch 'main' into sigv4_support
liustve 6cf44bd
removed validation for aws region
liustve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import re | ||
|
|
||
|
|
||
| def is_otlp_endpoint_cloudwatch(otlp_endpoint=None): | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Detects if it's the OTLP endpoint in CloudWatchs | ||
| if not otlp_endpoint: | ||
| return False | ||
|
|
||
| pattern = r"https://xray\.([a-z0-9-]+)\.amazonaws\.com/v1/traces$" | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return bool(re.match(pattern, otlp_endpoint.lower())) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/otlp_sigv4_exporter.py
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import logging | ||
| from typing import Dict, Optional | ||
|
|
||
| import requests | ||
| from grpc import Compression | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| from amazon.opentelemetry.distro._utils import is_otlp_endpoint_cloudwatch | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
|
|
||
| AWS_SERVICE = "xray" | ||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| """The OTLPAwsSigV4Exporter extends the functionality of the OTLPSpanExporter to allow SigV4 authentication if the | ||
| configured traces endpoint is a CloudWatch OTLP endpoint https://xray.[AWSRegion].amazonaws.com/v1/traces""" | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class OTLPAwsSigV4Exporter(OTLPSpanExporter): | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def __init__( | ||
| self, | ||
| endpoint: Optional[str] = None, | ||
| certificate_file: Optional[str] = None, | ||
| client_key_file: Optional[str] = None, | ||
| client_certificate_file: Optional[str] = None, | ||
| headers: Optional[Dict[str, str]] = None, | ||
| timeout: Optional[int] = None, | ||
| compression: Optional[Compression] = None, | ||
| rsession: Optional[requests.Session] = None, | ||
| ): | ||
|
|
||
| # Represents the region of the CloudWatch OTLP endpoint to send the traces to. | ||
| # If the endpoint has been verified to be valid, this should not be None | ||
|
|
||
| self._aws_region = None | ||
|
|
||
| if endpoint and is_otlp_endpoint_cloudwatch(endpoint): | ||
|
|
||
| # Defensive check to verify that the application being auto instrumented has | ||
| # botocore installed. | ||
| try: | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # pylint: disable=import-outside-toplevel | ||
| from botocore import auth, awsrequest, session | ||
|
|
||
| self.boto_auth = auth | ||
| self.boto_aws_request = awsrequest | ||
| self.boto_session = session.Session() | ||
| self._aws_region = self._validate_exporter_endpoint(endpoint) | ||
|
|
||
| except ImportError: | ||
| _logger.error( | ||
| "botocore is required to export traces to %s. Please install it using `pip install botocore`", | ||
| endpoint, | ||
| ) | ||
|
|
||
| else: | ||
| _logger.error( | ||
| "Invalid XRay traces endpoint: %s. Resolving to OTLPSpanExporter to handle exporting. " | ||
| "The traces endpoint follows the pattern https://xray.[AWSRegion].amazonaws.com/v1/traces. " | ||
| "For example, for the US West (Oregon) (us-west-2) Region, the endpoint will be " | ||
| "https://xray.us-west-2.amazonaws.com/v1/traces.", | ||
| endpoint, | ||
| ) | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| super().__init__( | ||
| endpoint=endpoint, | ||
| certificate_file=certificate_file, | ||
| client_key_file=client_key_file, | ||
| client_certificate_file=client_certificate_file, | ||
| headers=headers, | ||
| timeout=timeout, | ||
| compression=compression, | ||
| session=rsession, | ||
| ) | ||
|
|
||
| def _export(self, serialized_data: bytes): | ||
| if self._aws_region: | ||
| request = self.boto_aws_request.AWSRequest( | ||
| method="POST", | ||
| url=self._endpoint, | ||
| data=serialized_data, | ||
| headers={"Content-Type": "application/x-protobuf"}, | ||
| ) | ||
|
|
||
| credentials = self.boto_session.get_credentials() | ||
|
|
||
| if credentials is not None: | ||
| signer = self.boto_auth.SigV4Auth(credentials, AWS_SERVICE, self._aws_region) | ||
|
|
||
| try: | ||
| signer.add_auth(request) | ||
| self._session.headers.update(dict(request.headers)) | ||
|
|
||
| except self.boto_auth.NoCredentialsError as signing_error: | ||
| _logger.error("Failed to sign request: %s", signing_error) | ||
|
|
||
| else: | ||
| _logger.error("Failed to get credentials to export span to OTLP CloudWatch endpoint") | ||
|
|
||
| return super()._export(serialized_data) | ||
|
|
||
| def _validate_exporter_endpoint(self, endpoint: str) -> Optional[str]: | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not endpoint: | ||
| return None | ||
|
|
||
| region = endpoint.split(".")[1] | ||
| xray_regions = self.boto_session.get_available_regions(AWS_SERVICE) | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if region not in xray_regions: | ||
|
|
||
| _logger.error( | ||
| "Invalid AWS region: %s. Valid regions are %s. Resolving to default endpoint.", region, xray_regions | ||
| ) | ||
|
|
||
| return None | ||
|
|
||
| return region | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.