APMSVLS-65 Extract Trace Context From AppSync Events #660
+0
−0
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.
This PR adds logic to extract the trace context of an event that comes from a traced service through AppSync to a Python lambda. When a lambda is invoked from an AppSync API which was called in a RUM-instrumented front-end, the datadog trace context is located under
event["request"]["headers"]
rather than other locations from different event types. The extraction logic is placed in the helper functionextract_context_from_request_header_or_context
, which is called in the extract_dd_trace_context function in tracing.py. The helper function indexes into the request key in the event, after which theextract_context_from_http_event_or_context
function will get the header field and extract datadog context properly.What does this PR do?
Currently, if a customer has a setup where they use RUM to start a trace that goes through AppSync and triggers a Lambda function, we ask them to write a custom function to extract the trace context from the AppSync event that is passed to the Lambda. See an example of such a function here. It would be simpler for customers and for ourselves to extract the trace context in the lambda tracer layers ourselves, as we do for invocations from other sources (ex. SQS, API Gateway, etc.). This PR does so for the Python tracer.
Motivation
Based on this ticket, will reduce amount of code changes a customer has to make in order connect traces between RUM and a Lambda function when there is an AWS AppSync API in between them.
Testing Guidelines
Unit tested in
test_tracing.py
by adding events in tests/event_samples and including the tests in _test_extract_dd_trace_context. The events added,rum-appsync.json
,rum-appsync-no-headers.json
, andrum-appsync-request-not-dict.json
, are based on a sample request from the Datadog APM page of a trace that followed RUM -> AppSync -> Lambda. Some of the sample events are malformed/formatted differently than expected to ensure that exceptions are not raised if we encounter an event with a different format than anticipated. I ran a coverage test using pytest-cov to ensure that all new lines of code from this PR were tested.Ran integration tests using scripts/run_integration_tests.sh. Added a new input event called
appsync.json
and updated the snapshot so that integration tests also cover this new supported case.Uploaded my changes as a layer to AWS and tested whether a trace that followed RUM -> AppSync -> Lambda was shown as connected in the Datadog UI without needing a custom extractor, which is the goal of this PR. A successfully connected trace can be found here. The ARN for the testing version of the Python Lambda layer is
arn:aws:lambda:us-east-1:425362996713:layer:Python39-RITHIKA:3
Also used this layer to check distributed tracing in an API Gateway -> Lambda -> SQS -> Lambda setup to ensure that other tracing functionality was not broken by change.Additional Notes
Types of Changes
Check all that apply