Skip to content

Commit

Permalink
Move span pointer to inferred span; add env var to toggle span pointe…
Browse files Browse the repository at this point in the history
…rs (#607)
  • Loading branch information
nhulston authored Jan 30, 2025
1 parent 8fb4a3a commit 142439e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const coldStartTracingEnvVar = "DD_COLD_START_TRACING";
export const minColdStartTraceDurationEnvVar = "DD_MIN_COLD_START_DURATION";
export const coldStartTraceSkipLibEnvVar = "DD_COLD_START_TRACE_SKIP_LIB";
export const localTestingEnvVar = "DD_LOCAL_TESTING";
export const addSpanPointersEnvVar = "DD_TRACE_AWS_ADD_SPAN_POINTERS";

interface GlobalConfig {
/**
Expand Down Expand Up @@ -95,6 +96,7 @@ export const defaultConfig: Config = {
minColdStartTraceDuration: 3,
coldStartTraceSkipLib: "",
localTesting: false,
addSpanPointers: true,
} as const;

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

if (userConfig === undefined || userConfig.addSpanPointers === undefined) {
const result = getEnvValue(addSpanPointersEnvVar, "true").toLowerCase();
config.addSpanPointers = result === "true";
}

return config;
}

Expand Down
1 change: 1 addition & 0 deletions src/trace/listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe("TraceListener", () => {
injectLogContext: false,
minColdStartTraceDuration: 3,
coldStartTraceSkipLib: "",
addSpanPointers: true,
};
const context = {
invokedFunctionArn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda",
Expand Down
21 changes: 18 additions & 3 deletions src/trace/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export interface TraceConfig {
* Libraries to ignore from cold start traces
*/
coldStartTraceSkipLib: string;
/**
* Whether to enable span pointers
* @default true
*/
addSpanPointers: boolean;
}

export class TraceListener {
Expand Down Expand Up @@ -137,7 +142,9 @@ export class TraceListener {
this.triggerTags = extractTriggerTags(event, context, eventSource);
this.stepFunctionContext = StepFunctionContextService.instance().context;

this.spanPointerAttributesList = getSpanPointerAttributes(eventSource, event);
if (this.config.addSpanPointers) {
this.spanPointerAttributesList = getSpanPointerAttributes(eventSource, event);
}
}

/**
Expand Down Expand Up @@ -201,9 +208,17 @@ export class TraceListener {
}
}

if (this.wrappedCurrentSpan && this.spanPointerAttributesList) {
let rootSpan = this.inferredSpan;
if (!rootSpan) {
rootSpan = this.wrappedCurrentSpan;
}
if (this.spanPointerAttributesList) {
for (const attributes of this.spanPointerAttributesList) {
this.wrappedCurrentSpan.span.addSpanPointer(attributes.kind, attributes.direction, attributes.hash);
try {
rootSpan.span.addSpanPointer(attributes.kind, attributes.direction, attributes.hash);
} catch (e) {
logDebug("Failed to add span pointer");
}
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/span-pointers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function processDynamoDbEvent(event: any): SpanPointerAttributes[] {

const keys = record.dynamodb?.Keys;
const eventSourceARN = record.eventSourceARN;
const tableName = record.eventSourceARN ? getTableNameFromARN(eventSourceARN) : undefined;
const tableName = eventSourceARN ? getTableNameFromARN(eventSourceARN) : undefined;

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

0 comments on commit 142439e

Please sign in to comment.