Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Parse XRay header
Browse files Browse the repository at this point in the history
  • Loading branch information
hamitb committed Sep 12, 2019
1 parent e9b07ea commit b9337a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const envVariableKeys = {
THUNDRA_DISABLE_METRIC: 'thundra_agent_lambda_metric_disable',
THUNDRA_DISABLE_LOG: 'thundra_agent_lambda_log_disable',
THUNDRA_DISABLE_XRAY: 'thundra_agent_lambda_xray_disable',
_X_AMZN_TRACE_ID: '_X_AMZN_TRACE_ID',

THUNDRA_LAMBDA_TRACE_REQUEST_SKIP: 'thundra_agent_lambda_trace_request_skip',
THUNDRA_LAMBDA_TRACE_RESPONSE_SKIP: 'thundra_agent_lambda_trace_response_skip',
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/Invocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ class Invocation {
this.invocationData.spanId = this.pluginContext.spanId;
this.invocationData.traceId = this.pluginContext.traceId;

const xrayTraceInfo = Utils.getXRayTraceInfo();

if (xrayTraceInfo.traceID) {
this.invocationData.tags['aws.xray.trace.id'] = xrayTraceInfo.traceID;
}
if (xrayTraceInfo.segmentID) {
this.invocationData.tags['aws.xray.segment.id'] = xrayTraceInfo.segmentID;
}

this.invocationData.tags['aws.lambda.memory_limit'] = this.pluginContext.maxMemory;
this.invocationData.tags['aws.lambda.arn'] = originalContext.invokedFunctionArn;
this.invocationData.tags['aws.account_no'] = Utils.getAWSAccountNo(originalContext.invokedFunctionArn);
Expand Down
26 changes: 26 additions & 0 deletions src/plugins/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,32 @@ class Utils {
return '';
}
}

static getXRayTraceInfo() {
let traceID: string = '';
let segmentID: string = '';
const xrayTraceHeader: string = Utils.getConfiguration(envVariableKeys._X_AMZN_TRACE_ID);
if (xrayTraceHeader) {
for (const traceHeaderPart of xrayTraceHeader.split(';')) {
const traceInfo = traceHeaderPart.split('=');
if (traceInfo.length !== 2) {
continue;
}
const [traceInfoKey, traceInfoVal] = traceInfo;

if (traceInfoKey === 'Root') {
traceID = traceInfoVal;
} else if (traceInfoKey === 'Parent') {
segmentID = traceInfoVal;
}
}
}

return {
traceID,
segmentID,
};
}
}

export default Utils;

0 comments on commit b9337a4

Please sign in to comment.