-
Notifications
You must be signed in to change notification settings - Fork 51
durable: extract trace context from checkpoints #818
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
Open
joeyzhao2018
wants to merge
14
commits into
main
Choose a base branch
from
joey/durable-tracecontext-extraction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−1
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
20b0054
durable: extract trace context from checkpoints and input payload
joeyzhao2018 604a2a1
Merge branch 'main' into joey/durable-tracecontext-extraction
joeyzhao2018 4db58ed
reorganize the code
joeyzhao2018 2aedd77
further simplify
joeyzhao2018 499aa45
separate out the cross-execution case
joeyzhao2018 f11ab2a
clean up tests
joeyzhao2018 b3f83df
format
joeyzhao2018 62d8b69
the extraction part of the simplification done by dd-trace-py side in…
joeyzhao2018 42247d0
format
joeyzhao2018 958daf0
format
joeyzhao2018 bb9e643
reformat again
joeyzhao2018 9c4e70c
update .flake8 to be compatible with black per black doc https://blac…
joeyzhao2018 806de56
Merge branch 'main' into joey/durable-tracecontext-extraction
joeyzhao2018 205dd2b
use regular propagator.extract
joeyzhao2018 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 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| [flake8] | ||
| max-line-length = 100 | ||
| max-line-length = 100 | ||
| extend-ignore = E203,E701 |
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 |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ | |
| DD_TRACE_JAVA_TRACE_ID_PADDING = "00000000" | ||
| HIGHER_64_BITS = "HIGHER_64_BITS" | ||
| LOWER_64_BITS = "LOWER_64_BITS" | ||
| _TRACE_CHECKPOINT_PREFIX = "_datadog_" | ||
|
|
||
|
|
||
| def _dsm_set_checkpoint(context_json, event_type, arn): | ||
|
|
@@ -546,6 +547,60 @@ def extract_context_from_step_functions(event, lambda_context): | |
| return extract_context_from_lambda_context(lambda_context) | ||
|
|
||
|
|
||
| def _extract_context_from_durable_checkpoint(operation): | ||
| # Checkpoint data is written by the dd-trace-py in Datadog style | ||
| # (x-datadog-* headers). Extraction goes through the standard | ||
| # propagator.extract path, which honors DD_TRACE_PROPAGATION_STYLE_EXTRACT. | ||
| # The default extract list (datadog, tracecontext, baggage) already | ||
| # includes datadog. Customers who override the extract list MUST keep | ||
| # datadog in it. | ||
| if not isinstance(operation, dict): | ||
| return None | ||
|
|
||
| step_details = operation.get("StepDetails") | ||
| if not isinstance(step_details, dict): | ||
| return None | ||
|
|
||
| result = step_details.get("Result") | ||
| if isinstance(result, str): | ||
| try: | ||
| result = json.loads(result) | ||
| except Exception: | ||
| return None | ||
|
|
||
| if not isinstance(result, dict): | ||
| return None | ||
|
|
||
| return propagator.extract(result) | ||
|
|
||
|
|
||
| def extract_context_from_durable_execution(event): | ||
| operations = event.get("InitialExecutionState", {}).get("Operations") | ||
| if isinstance(operations, dict): | ||
| operations = list(operations.values()) | ||
| if not isinstance(operations, list) or not operations: | ||
| return None | ||
|
|
||
| highest = -1 | ||
| best_operation = None | ||
| for operation in operations: | ||
| if not isinstance(operation, dict): | ||
| continue | ||
| name = operation.get("Name") | ||
| if not isinstance(name, str) or not name.startswith(_TRACE_CHECKPOINT_PREFIX): | ||
| continue | ||
| suffix = name[len(_TRACE_CHECKPOINT_PREFIX) :] | ||
| try: | ||
| number = int(suffix) | ||
| except (TypeError, ValueError): | ||
| continue | ||
| if number > highest: | ||
| highest = number | ||
| best_operation = operation | ||
|
|
||
| return _extract_context_from_durable_checkpoint(best_operation) | ||
|
|
||
|
|
||
| def extract_context_custom_extractor(extractor, event, lambda_context): | ||
| """ | ||
| Extract Datadog trace context using a custom trace extractor function | ||
|
|
@@ -633,9 +688,12 @@ def extract_dd_trace_context( | |
| global dd_trace_context | ||
| trace_context_source = None | ||
| event_source = parse_event_source(event) | ||
| context = None | ||
|
|
||
| if extractor is not None: | ||
| context = extract_context_custom_extractor(extractor, event, lambda_context) | ||
| elif isinstance(event, dict) and "DurableExecutionArn" in event: | ||
| context = extract_context_from_durable_execution(event) | ||
|
Comment on lines
+695
to
+696
Contributor
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. What happens in the first invocation here? Do we expect the first invocation to extract the context with the other methods? |
||
| elif isinstance(event, (set, dict)) and "request" in event: | ||
| context = extract_context_from_request_header_or_context( | ||
| event, lambda_context, event_source | ||
|
|
||
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
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.
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.
nit: should these functions live in datadog_lambda/durable.py?