Skip to content

Hybrid Tracing (DO NOT MERGE) - #1

Closed
DarcyRaynerDD wants to merge 18 commits into
pr-branchfrom
master
Closed

Hybrid Tracing (DO NOT MERGE)#1
DarcyRaynerDD wants to merge 18 commits into
pr-branchfrom
master

Conversation

@DarcyRaynerDD

Copy link
Copy Markdown
Contributor

This contains the first approach for hybrid tracing using node.

How it works

We implement hybrid tracing by piggbacking on AWS Xray traces. In a lambda environment, AWS sets the environment variable _X_AMZN_TRACE_ID, which usually has a value formatted like this:

_X_AMZN_TRACE_ID="X-Amzn-Trace-Id: Root=1-aaaaaaaa-aaaaaaaaaaaaaaaaaaaaaaaa;Parent=aaaaaaaaaaaaaaaa;Sampled=1"

We convert this, (deterministically), into Datadog hybrid tracing headers, and attach to every outgoing request.

x-datadog-trace-id: 1111111111111111
x-datadog-parent-id: 1111111111111111
x-datadog-sampling-priority: 2

If the lambda received the Datadog hybrid tracing headers in a request, those values are used instead of the ones derived from xray. We also save those headers into a dummy xray subsegment, (so the crawler can read them).

@swfunc swfunc 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 left a few optional notes mostly asking to clarify a few things with more comments. Overall it looks great. The tests are 💯.

Comment thread src/trace/patch-http.ts
function patchMethod(mod: typeof http | typeof https, method: "get" | "request", contextService: TraceContextService) {
shimmer.wrap(mod, method, (original) => {
const fn = (arg1: any, arg2: any, arg3: any) => {
const { options, callback } = normalizeArgs(arg1, arg2, arg3);

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.

Could you add a comment here to clarify what's happening with the arg normalizing / why it's needed?

Comment thread src/trace/patch-http.ts
}
}

function normalizeArgs(

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.

A docstring here would be helpful

Comment thread src/trace/patch-http.ts Outdated
return { options, callback };
}

function requestOptionsWithTraceContext(

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.

do you think it would be more clear to rename this to something like getRequestOptionsWithTraceContext? I was a little confused at first thinking this was making the request

Comment thread src/trace/index.ts Outdated
@@ -0,0 +1,3 @@
export { patchHttp } from "./patch-http";
export { extractTraceContext as readTraceContext } from "./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.

what's the reason for renaming this?

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.

Good catch, I think that was an accidental automatic refactoring.

Comment thread src/trace/context.ts
* Reads the trace context from either an incoming lambda event, or the process environment.
* @param event An incoming lambda event. This must have incoming trace headers in order to be read.
* @param env The process environment that may contain an xray trace id environment variable. This we be used
* if the event doesn't contain trace headers.

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.

could you add something here about how the _X_AMZN_TRACE_ID is set to be unique for each execution by X-Ray? I was a little confused at first thinking the env variable was set by the user for some reason


import { convertToAPMParentID, TraceContext } from "./context";

export class TraceContextService {

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.

Could you add a short comment here about the class?

Comment thread src/trace/context.ts
try {
addTraceContextToXray(trace);
} catch {
// This might fail if running in an environment where xray isn't set up, (like for local development).

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.

do you think it would be helpful for customers if to add a log line here?

@swfunc swfunc 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.

thanks for the changes 👍

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.

2 participants