-
Notifications
You must be signed in to change notification settings - Fork 622
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
opentelemetry-instrumentation-aws-lambda
: Adding option to disable context propagation
#1466
Merged
ocelotl
merged 4 commits into
open-telemetry:main
from
codeboten:codeboten/add-disable-aws-context-propagation
Nov 23, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -134,7 +134,9 @@ def _default_event_context_extractor(lambda_event: Any) -> Context: | |
|
||
|
||
def _determine_parent_context( | ||
lambda_event: Any, event_context_extractor: Callable[[Any], Context] | ||
lambda_event: Any, | ||
event_context_extractor: Callable[[Any], Context], | ||
disable_aws_context_propagation: bool = False, | ||
) -> Context: | ||
"""Determine the parent context for the current Lambda invocation. | ||
|
@@ -144,17 +146,25 @@ def _determine_parent_context( | |
Args: | ||
lambda_event: user-defined, so it could be anything, but this | ||
method counts it being a map with a 'headers' key | ||
event_context_extractor: a method which takes the Lambda | ||
Event as input and extracts an OTel Context from it. By default, | ||
the context is extracted from the HTTP headers of an API Gateway | ||
request. | ||
disable_aws_context_propagation: By default, this instrumentation | ||
will try to read the context from the `_X_AMZN_TRACE_ID` environment | ||
variable set by Lambda, set this to `True` to disable this behavior. | ||
Returns: | ||
A Context with configuration found in the carrier. | ||
""" | ||
parent_context = None | ||
|
||
xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) | ||
if not disable_aws_context_propagation: | ||
xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) | ||
|
||
if xray_env_var: | ||
parent_context = AwsXRayPropagator().extract( | ||
{TRACE_HEADER_KEY: xray_env_var} | ||
) | ||
if xray_env_var: | ||
parent_context = AwsXRayPropagator().extract( | ||
{TRACE_HEADER_KEY: xray_env_var} | ||
) | ||
|
||
if ( | ||
parent_context | ||
|
@@ -258,6 +268,7 @@ def _instrument( | |
flush_timeout, | ||
event_context_extractor: Callable[[Any], Context], | ||
tracer_provider: TracerProvider = None, | ||
disable_aws_context_propagation: bool = False, | ||
): | ||
def _instrumented_lambda_handler_call( | ||
call_wrapped, instance, args, kwargs | ||
|
@@ -269,7 +280,9 @@ def _instrumented_lambda_handler_call( | |
lambda_event = args[0] | ||
|
||
parent_context = _determine_parent_context( | ||
lambda_event, event_context_extractor | ||
lambda_event, | ||
event_context_extractor, | ||
disable_aws_context_propagation, | ||
) | ||
|
||
span_kind = None | ||
|
@@ -368,6 +381,9 @@ def _instrument(self, **kwargs): | |
Event as input and extracts an OTel Context from it. By default, | ||
the context is extracted from the HTTP headers of an API Gateway | ||
request. | ||
``disable_aws_context_propagation``: By default, this instrumentation | ||
will try to read the context from the `_X_AMZN_TRACE_ID` environment | ||
variable set by Lambda, set this to `True` to disable this behavior. | ||
""" | ||
lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER)) | ||
# pylint: disable=attribute-defined-outside-init | ||
|
@@ -377,11 +393,12 @@ def _instrument(self, **kwargs): | |
) = lambda_handler.rsplit(".", 1) | ||
|
||
flush_timeout_env = os.environ.get( | ||
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT, "" | ||
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT, None | ||
) | ||
flush_timeout = 30000 | ||
try: | ||
flush_timeout = int(flush_timeout_env) | ||
if flush_timeout_env is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
flush_timeout = int(flush_timeout_env) | ||
except ValueError: | ||
logger.warning( | ||
"Could not convert OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT value %s to int", | ||
|
@@ -396,6 +413,9 @@ def _instrument(self, **kwargs): | |
"event_context_extractor", _default_event_context_extractor | ||
), | ||
tracer_provider=kwargs.get("tracer_provider"), | ||
disable_aws_context_propagation=kwargs.get( | ||
"disable_aws_context_propagation", False | ||
), | ||
) | ||
|
||
def _uninstrument(self, **kwargs): | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
.get
without a defaultNone
already anyways?i.e. shouldn't this just be: