Skip to content

[Serverless] Auto-instrument AWS Lambda Handler - #3506

Merged
maxday merged 50 commits into
masterfrom
maxday/auto-instrument-aws-lambda-handler
Apr 28, 2022
Merged

maxday merged 50 commits into
masterfrom
maxday/auto-instrument-aws-lambda-handler

Conversation

@maxday

@maxday maxday commented Apr 18, 2022 •

Copy link
Copy Markdown
Contributor

What Does This Do

Add support for AWS Handler auto-instrumentation

Note : Since the invocation span is created in the extension (to unify logic to detect downstream AWS services across all AWS Lambda supported runtimes), we need to create a dummy span with the exact same spanID + traceID. This allows any newly created span inherit from the correct context.

Screen Shot 2022-04-18 at 9 33 08 AM

Motivation

Code-free instrumentation for AWS Lambda

@maxday
maxday requested a review from a team as a code owner April 18, 2022 13:40
implementation deps.okhttp

api 'com.fasterxml.jackson.core:jackson-core:2.11.0'
api 'com.fasterxml.jackson.core:jackson-databind:2.11.0'

@maxday maxday Apr 18, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how bad this is for non serverless usage
We definitely need Jackson (or similar) to send a JSON version of the context / user event but maybe we could load this only for a serverless build?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed


@Override
public String instrumentedType() {
return this.instrumentedType;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clever

@randomanderson randomanderson left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'll start the ball rolling (this is not a full review, just a few things I noticed):

  • DummyContext is not needed. Since the headers returned are the standard datadog headers, you can use the normal extraction mechanism. See propagate().extract() Edit: I realize DummyContext assumes the ids rather than using it as a parent

There are problems with the dependencies. Adding classes to the bootstrap means they will be at the top of the classpath hierarchy, with something like jackson, this is sure to cause conflicts.

(normally)

  • LambdaHandler should be in the instrumentation module and listed as a helper class
  • Dependencies should be listed in the instrumentation module
  • We use Moshi for JSON serialization

The bigger issue is that all classes used by the instrumentation need to be injected into the instrumented classloader. So even just using okhttp and moshi means injecting a lot of classes (all of which have to be listed as helpers).

The correct solution is probably implementing the LambdaHandler in dd-trace-core and having the instrumentation call that. This way, there are no extra dependencies and you can reuse the Okhttp client

@maxday maxday changed the title [WIP][Serverless] Auto-instrument AWS Lambda Handler [Serverless] Auto-instrument AWS Lambda Handler Apr 26, 2022
import java.util.Collections;
import java.util.Map;

public class DummyLambdaContext implements AgentSpan.Context {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that this context is not needed, and the code could just use the ExtractedContext and only fill in the interesting values in it. That would also mean that the changes to buildSpanContext can be removed as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I just realized that the spanId part is different. Still think that it could have a better name and extend ExtractedContext with that part.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

you're right! I've renamed it to LambdaContext which now extends ExtractedContext. This has also drastically simplified the changes to CoreTracer

@bantonsson bantonsson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

There is failure in the check task that needs to be fixed. You can see the problem here https://output.circle-artifacts.com/output/job/31aaaaf5-9915-4b41-8399-e4e2a6bc9817/artifacts/0/reports/dd-trace-core/codenarc/test.html

If you want to run the check locally, then do:

./gradlew dd-trace-core:check -PskipTests

@bantonsson

Copy link
Copy Markdown
Contributor

Also, please squash when merging, or simply squash locally and push -f. We don't want all these small commits in the history.

@bantonsson

Copy link
Copy Markdown
Contributor

Some more check failures 😉

This will run check for the whole project:

./gradlew check -PskipTests


private static final String DATADOG_TRACE_ID = "x-datadog-trace-id";
private static final String DATADOG_SPAN_ID = "x-datadog-span-id";
private static final String DATADOG_SAMPLING_PRIORITY = "x-datadog-sampling-priority";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💯

@hghotra hghotra left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍🏼

@maxday
maxday merged commit e02a393 into master Apr 28, 2022
@maxday
maxday deleted the maxday/auto-instrument-aws-lambda-handler branch April 28, 2022 13:50
@github-actions github-actions Bot added this to the 0.101.0 milestone Apr 28, 2022
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