Skip to content

Commit 142439e

Browse files
authored
Move span pointer to inferred span; add env var to toggle span pointers (#607)
1 parent 8fb4a3a commit 142439e

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const coldStartTracingEnvVar = "DD_COLD_START_TRACING";
5151
export const minColdStartTraceDurationEnvVar = "DD_MIN_COLD_START_DURATION";
5252
export const coldStartTraceSkipLibEnvVar = "DD_COLD_START_TRACE_SKIP_LIB";
5353
export const localTestingEnvVar = "DD_LOCAL_TESTING";
54+
export const addSpanPointersEnvVar = "DD_TRACE_AWS_ADD_SPAN_POINTERS";
5455

5556
interface GlobalConfig {
5657
/**
@@ -95,6 +96,7 @@ export const defaultConfig: Config = {
9596
minColdStartTraceDuration: 3,
9697
coldStartTraceSkipLib: "",
9798
localTesting: false,
99+
addSpanPointers: true,
98100
} as const;
99101

100102
export const _metricsQueue: MetricsQueue = new MetricsQueue();
@@ -412,6 +414,11 @@ function getConfig(userConfig?: Partial<Config>): Config {
412414
config.localTesting = result === "true" || result === "1";
413415
}
414416

417+
if (userConfig === undefined || userConfig.addSpanPointers === undefined) {
418+
const result = getEnvValue(addSpanPointersEnvVar, "true").toLowerCase();
419+
config.addSpanPointers = result === "true";
420+
}
421+
415422
return config;
416423
}
417424

src/trace/listener.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe("TraceListener", () => {
7878
injectLogContext: false,
7979
minColdStartTraceDuration: 3,
8080
coldStartTraceSkipLib: "",
81+
addSpanPointers: true,
8182
};
8283
const context = {
8384
invokedFunctionArn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda",

src/trace/listener.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export interface TraceConfig {
6969
* Libraries to ignore from cold start traces
7070
*/
7171
coldStartTraceSkipLib: string;
72+
/**
73+
* Whether to enable span pointers
74+
* @default true
75+
*/
76+
addSpanPointers: boolean;
7277
}
7378

7479
export class TraceListener {
@@ -137,7 +142,9 @@ export class TraceListener {
137142
this.triggerTags = extractTriggerTags(event, context, eventSource);
138143
this.stepFunctionContext = StepFunctionContextService.instance().context;
139144

140-
this.spanPointerAttributesList = getSpanPointerAttributes(eventSource, event);
145+
if (this.config.addSpanPointers) {
146+
this.spanPointerAttributesList = getSpanPointerAttributes(eventSource, event);
147+
}
141148
}
142149

143150
/**
@@ -201,9 +208,17 @@ export class TraceListener {
201208
}
202209
}
203210

204-
if (this.wrappedCurrentSpan && this.spanPointerAttributesList) {
211+
let rootSpan = this.inferredSpan;
212+
if (!rootSpan) {
213+
rootSpan = this.wrappedCurrentSpan;
214+
}
215+
if (this.spanPointerAttributesList) {
205216
for (const attributes of this.spanPointerAttributesList) {
206-
this.wrappedCurrentSpan.span.addSpanPointer(attributes.kind, attributes.direction, attributes.hash);
217+
try {
218+
rootSpan.span.addSpanPointer(attributes.kind, attributes.direction, attributes.hash);
219+
} catch (e) {
220+
logDebug("Failed to add span pointer");
221+
}
207222
}
208223
}
209224
return false;

src/utils/span-pointers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function processDynamoDbEvent(event: any): SpanPointerAttributes[] {
114114

115115
const keys = record.dynamodb?.Keys;
116116
const eventSourceARN = record.eventSourceARN;
117-
const tableName = record.eventSourceARN ? getTableNameFromARN(eventSourceARN) : undefined;
117+
const tableName = eventSourceARN ? getTableNameFromARN(eventSourceARN) : undefined;
118118

119119
if (!tableName || !keys) {
120120
logDebug("Unable to calculate hash because of missing parameters.");

0 commit comments

Comments
 (0)