-
Notifications
You must be signed in to change notification settings - Fork 45
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
Explicit trace ID propagation for SFN w/o Hashing #537
Merged
Merged
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7c127a3
add logic to extract traceID from _datadog header
avedmala 8658dea
rename test
avedmala 3eccb20
fix type
avedmala 97b189a
added root arn case
avedmala eaa09f3
trigger ci
avedmala 2baec69
Add `http.route` tags for API Gateway (#524)
nhulston 3c4014b
feat: [SVLS-5677] DynamoDB Stream event span pointers (#522)
apiarian-datadog 6f54a00
trigger ci
avedmala 4e0afdb
Merge remote-tracking branch 'origin/main' into avedmala/sfn-span-link
avedmala 25780a8
use default propagator.extract
avedmala 85a157f
lint
avedmala aad3cd8
lint
avedmala c3681de
updated to use trace/parent hash from _datadog
avedmala 7d1d475
lint
avedmala e9a7d46
skip is context complete check
avedmala a6464b4
remove unused import
avedmala 068c2fc
fix legacy lambda parsing with new header
avedmala 00850d8
using context object instead of pre-hashed values
avedmala c23911d
Merge branch 'main' into avedmala/sfn-span-link-no-hash
avedmala 832ae1b
fixed trigger tags and tests
avedmala e2e732b
pull sfn trace id generation out into a helper
avedmala 3bfc1e7
added unit tests
avedmala 6abb3f3
update test data
avedmala cae62b1
rename stepfunctions to states
avedmala 49dbaf2
update current serverless version to v1
avedmala c47fc62
Update trigger comment
avedmala 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 |
---|---|---|
|
@@ -356,9 +356,8 @@ def extract_context_from_kinesis_event(event, lambda_context): | |
return extract_context_from_lambda_context(lambda_context) | ||
|
||
|
||
def _deterministic_sha256_hash(s: str, part: str) -> (int, int): | ||
def _deterministic_sha256_hash(s: str, part: str) -> int: | ||
sha256_hash = hashlib.sha256(s.encode()).hexdigest() | ||
|
||
# First two chars is '0b'. zfill to ensure 256 bits, but we only care about the first 128 bits | ||
binary_hash = bin(int(sha256_hash, 16))[2:].zfill(256) | ||
if part == HIGHER_64_BITS: | ||
|
@@ -371,36 +370,74 @@ def _deterministic_sha256_hash(s: str, part: str) -> (int, int): | |
return result | ||
|
||
|
||
def _parse_high_64_bits(trace_tags: str) -> str: | ||
# todo: testme | ||
if trace_tags: | ||
for tag in trace_tags.split(","): | ||
if "_dd.p.tid=" in tag: | ||
return tag.split("=")[1] | ||
|
||
return "" | ||
|
||
|
||
def _sfn_context_to_parent_id(context: dict) -> int: | ||
# todo: testme | ||
execution_id = context.get("Execution").get("Id") | ||
state_name = context.get("State").get("Name") | ||
state_entered_time = context.get("State").get("EnteredTime") | ||
|
||
return _deterministic_sha256_hash( | ||
f"{execution_id}#{state_name}#{state_entered_time}", HIGHER_64_BITS | ||
) | ||
|
||
|
||
def extract_context_from_step_functions(event, lambda_context): | ||
""" | ||
Only extract datadog trace context when Step Functions Context Object is injected | ||
into lambda's event dict. | ||
""" | ||
# todo: update docstring | ||
try: | ||
execution_id = event.get("Execution").get("Id") | ||
state_name = event.get("State").get("Name") | ||
state_entered_time = event.get("State").get("EnteredTime") | ||
# returning 128 bits since 128bit traceId will be break up into | ||
# traditional traceId and _dd.p.tid tag | ||
# https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240 | ||
trace_id = _deterministic_sha256_hash(execution_id, LOWER_64_BITS) | ||
|
||
parent_id = _deterministic_sha256_hash( | ||
f"{execution_id}#{state_name}#{state_entered_time}", HIGHER_64_BITS | ||
) | ||
meta = {} | ||
dd_data = event.get("_datadog") | ||
|
||
if dd_data and dd_data.get("serverless-version") == "v2": | ||
if "x-datadog-trace-id" in dd_data: # lambda root | ||
trace_id = dd_data.get("x-datadog-trace-id") | ||
high_64_bit_trace_id = _parse_high_64_bits(dd_data.get("x-datadog-tags")) | ||
if high_64_bit_trace_id: | ||
meta["_dd.p.tid"] = high_64_bit_trace_id | ||
else: # sfn root | ||
trace_id = _deterministic_sha256_hash( | ||
dd_data.get("RootExecutionId"), LOWER_64_BITS | ||
) | ||
meta["_dd.p.tid"] = hex( | ||
_deterministic_sha256_hash( | ||
dd_data.get("RootExecutionId"), HIGHER_64_BITS | ||
) | ||
)[2:] | ||
|
||
parent_id = _sfn_context_to_parent_id(dd_data) | ||
else: | ||
execution_id = event.get("Execution").get("Id") | ||
# returning 128 bits since 128bit traceId will be break up into | ||
# traditional traceId and _dd.p.tid tag | ||
# https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240 | ||
trace_id = _deterministic_sha256_hash(execution_id, LOWER_64_BITS) | ||
# take the higher 64 bits as _dd.p.tid tag and use hex to encode | ||
# [2:] to remove '0x' in the hex str | ||
meta["_dd.p.tid"] = hex( | ||
_deterministic_sha256_hash(execution_id, HIGHER_64_BITS) | ||
)[2:] | ||
|
||
parent_id = _sfn_context_to_parent_id(event) | ||
|
||
sampling_priority = SamplingPriority.AUTO_KEEP | ||
return Context( | ||
trace_id=trace_id, | ||
span_id=parent_id, | ||
sampling_priority=sampling_priority, | ||
# take the higher 64 bits as _dd.p.tid tag and use hex to encode | ||
# [2:] to remove '0x' in the hex str | ||
meta={ | ||
"_dd.p.tid": hex( | ||
_deterministic_sha256_hash(execution_id, HIGHER_64_BITS) | ||
)[2:] | ||
}, | ||
meta=meta, | ||
) | ||
except Exception as e: | ||
logger.debug("The Step Functions trace extractor returned with error %s", e) | ||
|
@@ -411,8 +448,13 @@ def is_legacy_lambda_step_function(event): | |
""" | ||
Check if the event is a step function that called a legacy lambda | ||
""" | ||
event = event.get("Payload", {}) | ||
return "Execution" in event and "StateMachine" in event and "State" in event | ||
if not isinstance(event, dict) or "Payload" not in event: | ||
return False | ||
|
||
event = event.get("Payload") | ||
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. |
||
return "_datadog" in event or ( | ||
"Execution" in event and "StateMachine" in event and "State" in event | ||
) | ||
|
||
|
||
def extract_context_custom_extractor(extractor, event, lambda_context): | ||
|
@@ -667,6 +709,7 @@ def create_inferred_span( | |
event_source: _EventSource = None, | ||
decode_authorizer_context: bool = True, | ||
): | ||
logger.debug("abhinav event %s", event) | ||
if event_source is None: | ||
event_source = parse_event_source(event) | ||
try: | ||
|
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
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.
What is the difference between v1 and v2? Should we start from version v2 or v1?
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.
There's no v1, I think we can start from v1 instead if we want
I think @kimi-p suggested v2, maybe because the "legacy" context is implied to be v1
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.
In my opinion, "V2" may be somewhat confusing to customers because there is no explicit reference to "V1" at all.
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.
sgtm!