Skip to content

Explicit trace ID propagation for SFN #526

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

Closed
wants to merge 18 commits into from
Closed

Conversation

avedmala
Copy link
Contributor

@avedmala avedmala commented Oct 29, 2024

What does this PR do?

Add logic to extract trace context from _datadog for Step Functions cases where...

  1. Root of the overall execution is a Lambda. This means we have the x-datadog-trace-id but we need to compute the x-datadog-parent-id using x-datadog-parent-id-hash
  2. Root of the overall execution is another Step Function. This means we need to compute both x-datadog-trace-id and x-datadog-parent-id from their respective hashes
  3. Keeping legacy code where there's no _datadog and we instead figure out context from the Execution and State info

Motivation

Testing Guidelines

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@avedmala avedmala marked this pull request as ready for review October 29, 2024 18:47
@avedmala avedmala requested a review from a team as a code owner October 29, 2024 18:47
@avedmala avedmala changed the title SFN Span Linking Updates Explicit trace ID propagation for SFN Oct 29, 2024
Copy link
Contributor

@kimi-p kimi-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test with SFN -> Lambda with payload of _datadog and see if these two spans will link? You might need to build the python layer and use it. Thanks!

This is the command that joey shared to me when I was building py layer


# python
docker buildx build -t datadog-lambda-python-amd64:3.9 . --no-cache \
        --build-arg image=python:3.9 \
        --build-arg runtime=python3.9 \
        --platform linux/amd64 \
        --progress=plain \
        -o ./.layers/tmp/python && \
pushd ./.layers/tmp && zip -q -r datadog_lambda_py-amd64-3.9.zip ./ && \
sso_sand_run aws lambda publish-layer-version \
 --layer-name "Datadog-Python39" \
 --region sa-east-1 \
 --zip-file  "fileb://datadog_lambda_py-amd64-3.9.zip" && popd

Copy link
Contributor

@joeyzhao2018 joeyzhao2018 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced that this is any exception for propagator.extract.
I'll update my proposed changes later.

@@ -357,8 +358,10 @@ def extract_context_from_kinesis_event(event, lambda_context):


def _deterministic_sha256_hash(s: str, part: str) -> (int, int):
sha256_hash = hashlib.sha256(s.encode()).hexdigest()
return _sha256_to_binary_part(hashlib.sha256(s.encode()).hexdigest(), part)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split this up with a helper so we have an entrypoint for our sha256_hash from the x-datadog-trace-id-hash and x-datadog-parent-id-hash

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

dd_data.get("x-datadog-trace-id-hash"), HIGHER_64_BITS
)
)[2:]
else:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole branch should be the old behavior

@avedmala avedmala requested a review from joeyzhao2018 November 6, 2024 17:43
Copy link
Contributor

@joeyzhao2018 joeyzhao2018 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

Copy link
Contributor

@kimi-p kimi-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's test this change with reducer change in staging before merge it.

if "_datadog" in event:
dd_data = event.get("_datadog")
parent_id = _sha256_to_binary_part(
dd_data.get("x-datadog-parent-id-hash"), HIGHER_64_BITS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Since this extract_context_from_step_functions() is executed when we know _datadog.serverless-version=v2, we don't need to check if this hash key exists. The v2 contract is that they need to have the _datadog.x-datadog-parent-id-hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misunderstanding, we're not checking if the hash key exists right?

We're computing the parent_id no matter what, the only thing we check in this branch is if x-datadog-trace-id exists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're saying it's the same EventTypes.STEP_FUNCTIONS whether its the old format with context object or new one with _datadog. Both cases will enter extract_context_from_step_functions()

If we want to separate them, one thing we could do is make a new V2 event with a V2 extractor to go along with it

Comment on lines +392 to +400
else: # sfn root
trace_id = _sha256_to_binary_part(
dd_data.get("x-datadog-trace-id-hash"), LOWER_64_BITS
)
meta["_dd.p.tid"] = hex(
_sha256_to_binary_part(
dd_data.get("x-datadog-trace-id-hash"), HIGHER_64_BITS
)
)[2:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

else is not necessary since the if clause has a return (...read more)

If the code in the if branch returns a value, do not have the else branch present.

View in Datadog  Leave us feedback  Documentation

if not isinstance(event, dict) or "Payload" not in event:
return False

event = event.get("Payload")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

variable name is the same as a function parameter (...read more)

A function parameter should only be read and not be modified. If your intent is to modify the value of the parameter, return the value in the function and handle the new value in the caller of the function.

View in Datadog  Leave us feedback  Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants