Skip to content
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

Extract Lambda Trace Context from the Event's Step Functions Context #331

Merged

Conversation

kimi-p
Copy link
Contributor

@kimi-p kimi-p commented Jan 4, 2023

What does this PR do?

Set Lambda span's trace_id and parent's span_id when Step Functions Context is detected in the event. The corresponding Lambda subtask span is set in the backend https://github.com/DataDog/logs-backend/pull/43234.

The MD5 logic on the backend is here: https://github.com/DataDog/logs-backend/blob/329f0ce114138067af8cdaef82bdbe15a06ffc58/domains/serverless/apps/logs-to-traces-reducer/src/main/java/com/dd/logs/serverless/logs_to_traces/common/HashHelper.java#L12-L32

Node: md5 is used to generate a string to be converted to trace_id in a deterministic way, so md5 is not used for any encryption purposes.

Motivation

To link Step Functions log generated span (parent) with Lambda span (child).

Testing Guidelines

Tested in staging with custom made dd-lambda-js layer. The trace is here.

image

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)

@codecov-commenter

This comment was marked as outdated.

@kimi-p kimi-p marked this pull request as ready for review January 10, 2023 21:08
@kimi-p kimi-p requested a review from a team as a code owner January 10, 2023 21:08
@kimi-p kimi-p changed the title Update Lambda Span Parent Context with Step Functions Context Extract Lambda Trace Context from the Event's Step Functions Context Jan 10, 2023
@@ -108,6 +151,14 @@ export function extractTraceContext(
}
}
return trace;
} else {
// do not send step functions context to xray
if (stepFuncContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpik: can we combine this with the block above in order to put the stepFuncContext related logic together. that is:

  if (stepFuncContext) {
    try {
      addStepFunctionContextToXray(stepFuncContext);
    } catch (error) {
      if (error instanceof Error) {
        logError("couldn't add step function metadata to xray", error as Error);
      }
    }
    if (!trace) {
      trace = readTraceFromStepFunctionsContext(stepFuncContext);
      if (trace !== undefined) {
        return trace;
      }
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great suggestion. Updated and thanks for the review!

Copy link
Contributor

@DarcyRaynerDD DarcyRaynerDD left a comment

Choose a reason for hiding this comment

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

Overall looks good. Nice work.

@@ -55,6 +56,48 @@ export interface StepFunctionContext {
"step_function.state_retry_count": number;
}

export function readTraceFromStepFunctionsContext(stepFunctionContext: StepFunctionContext): TraceContext | undefined {
return {
traceID: deterministicMd5HashToBigIntString(stepFunctionContext["step_function.execution_id"]),
Copy link
Contributor

Choose a reason for hiding this comment

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

So, this is an issue with the existing code, (which I wrote), but I don't like how are are indexing an object instead of using property names, I feel like it's bad javascript style. I think it was done that way originally so it would be easy to add as metadata to the APM span, (we have to use the "step_function." prefix so it's grouped in the UI) . Not a blocker for merging, but maybe an opportunity for refactoring.

}

export function deterministicMd5HashInBinary(s: string): string {
// Md5 here is not used as an encryption method but to generate a deterministic hash as the backend does
Copy link
Contributor

Choose a reason for hiding this comment

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

Pedantic edit, since hashing != encryption

Suggested change
// Md5 here is not used as an encryption method but to generate a deterministic hash as the backend does
// Md5 here is used here because we don't need a cryptographically secure hashing method but to generate the same trace/span ids as the backend does

@@ -1109,3 +1146,141 @@ describe("extractTraceContext", () => {
`);
});
});

describe("test_hexToBinary", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Might be worth using a table driven test here to cut down on boilerplate:
https://jestjs.io/docs/api#describeeachtablename-fn-timeout

@kimi-p kimi-p merged commit 0f1f682 into main Jan 17, 2023
@kimi-p kimi-p deleted the kimi.update_lambda_span_parent_context_with_step_function_context branch January 17, 2023 22:45
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.

4 participants