8000 Extract trace context from ALB multiValueHeaders by nhulston · Pull Request #647 · DataDog/datadog-lambda-js · GitHub
[go: up one dir, main page]

Skip to content

Extract trace context from ALB multiValueHeaders #647

8000
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

Merged

Conversation

nhulston
Copy link
Contributor
@nhulston nhulston commented Apr 29, 2025

What does this PR do?

Support trace extraction when we receive multiValueHeaders instead of headers. This can happen if multi value headers are turned on in ALB.

The event payload structure for multiValueHeaders is defined here: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

Motivation

#603
https://datadoghq.atlassian.net/browse/SVLS-6725

Testing Guidelines

Manually, unit tests

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)

@nhulston nhulston marked this pull request as ready for review April 29, 2025 20:00
@nhulston nhulston requested a review from a team as a code owner April 29, 2025 20:00
@@ -97,6 +97,35 @@ describe("HTTPEventTraceExtractor", () => {
expect(traceContext?.source).toBe("event");
});

it("extracts trace context from payload with multiValueHeaders", () => {
mockSpanContext = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add an event from either ALB or API GW with real values to ensure this works? That way we can re-use it in Bottlecap too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just commenting for visibility -- as discussed, this change only applies to ALB. API GW can have multiValueHeaders, but trace context is always injected under the regular headers so no changes needed for API GW

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I added a real event from ALB!

@nhulston nhulston force-pushed the nicholas.hulston/extract-trace-context-multivalue-headers branch from 55e24fb to 032f3ca Compare April 30, 2025 19:13
@nhulston nhulston requested a review from duncanista April 30, 2025 19:35
@nhulston nhulston changed the title Extract trace context multiValueHeaders Extract trace context from ALB multiValueHeaders May 1, 2025
Comment on lines +48 to +54
8000 if (Array.isArray(val)) {
// MultiValueHeaders: take the first value
lowerCaseHeaders[key.toLowerCase()] = val[0] ?? "";
} else if (typeof val === "string") {
// Single‐value header
lowerCaseHeaders[key.toLowerCase()] = val;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (Array.isArray(val)) {
// MultiValueHeaders: take the first value
lowerCaseHeaders[key.toLowerCase()] = val[0] ?? "";
} else if (typeof val === "string") {
// Single‐value header
lowerCaseHeaders[key.toLowerCase()] = val;
}
if (Array.isArray(val)) {
// MultiValueHeaders: take the first value
lowerCaseHeaders[key.toLowerCase()] = val[0] ?? "";
continue;
}
// Single‐value header
lowerCaseHeaders[key.toLowerCase()] = val;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to type check key and make sure it's a string first; otherwise typescript errors TS2322: Type unknown is not assignable to type string

nhulston added a commit to DataDog/datadog-lambda-extension that referenced this pull request May 5, 2025
### What

Support trace extraction when we receive `multiValueHeaders` instead of
`headers`. This can happen if multi value headers are turned on in ALB.

We do this by creating an ALB event in `span_inferrer` -- even though
there's no inferred spans, this is still a common event type.

The event payload structure for multiValueHeaders is defined here:
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

This also has the benefit of creating these trigger tags:
<img width="400" alt="Screenshot 2025-05-01 at 4 55 34 PM"
src="https://github.com/user-attachments/assets/d02ba864-3ee3-43d8-9f4b-bdb6650544a5"
/>
<img width="250" alt="Screenshot 2025-05-01 at 4 56 38 PM"
src="https://github.com/user-attachments/assets/15c56afe-d314-4c42-a828-908d499ab62f"
/>


### Motivation
Porting DataDog/datadog-lambda-js#647 and
DataDog/datadog-agent#31542
https://datadoghq.atlassian.net/browse/SVLS-6757
@nhulston nhulston merged commit 191c5c7 into main May 5, 2025
25 checks passed
@nhulston nhulston deleted the nicholas.hulston/extract-trace-context-multivalue-headers branch May 5, 2025 15:30
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
0