Skip to content

Commit f03601b

Browse files
authored
feat: Modify end time of lambda load span to be last cold start node … (#372)
* feat: Modify end time of lambda load span to be last cold start node end time instead of start time of function. * fix: lint * feat: Refactor to use the minimum of the beginning of the wrapped current span, or the end of the last cold start node
1 parent 02afb68 commit f03601b

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/trace/cold-start-tracer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe("ColdStartTracer", () => {
6969
name: "my-lambda-span",
7070
} as any as SpanWrapper,
7171
lambdaFunctionName: "my-function-name",
72-
coldStartSpanFinishTime: 500,
72+
currentSpanStartTime: 500,
7373
minDuration: 1,
7474
ignoreLibs: "",
7575
};
@@ -159,7 +159,7 @@ describe("ColdStartTracer", () => {
159159
name: "my-lambda-span",
160160
} as any as SpanWrapper,
161161
lambdaFunctionName: "my-function-name",
162-
coldStartSpanFinishTime: 500,
162+
currentSpanStartTime: 500,
163163
minDuration: 1,
164164
ignoreLibs: "myChildModule,myCoreModule",
165165
};

src/trace/cold-start-tracer.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface ColdStartTracerConfig {
66
tracerWrapper: TracerWrapper;
77
parentSpan?: SpanWrapper;
88
lambdaFunctionName?: string;
9-
coldStartSpanFinishTime: number; // Equivalent to the Lambda Span Start Time
9+
currentSpanStartTime: number;
1010
minDuration: number;
1111
ignoreLibs: string;
1212
}
@@ -15,28 +15,29 @@ export class ColdStartTracer {
1515
private tracerWrapper: TracerWrapper;
1616
private parentSpan?: SpanWrapper;
1717
private lambdaFunctionName?: string;
18-
private coldStartSpanFinishTime: number;
18+
private currentSpanStartTime: number;
1919
private minDuration: number;
2020
private ignoreLibs: string[];
2121

2222
constructor(coldStartTracerConfig: ColdStartTracerConfig) {
2323
this.tracerWrapper = coldStartTracerConfig.tracerWrapper;
2424
this.parentSpan = coldStartTracerConfig.parentSpan;
2525
this.lambdaFunctionName = coldStartTracerConfig.lambdaFunctionName;
26-
this.coldStartSpanFinishTime = coldStartTracerConfig.coldStartSpanFinishTime;
26+
this.currentSpanStartTime = coldStartTracerConfig.currentSpanStartTime;
2727
this.minDuration = coldStartTracerConfig.minDuration;
2828
this.ignoreLibs = coldStartTracerConfig.ignoreLibs.split(",");
2929
}
3030

3131
trace(rootNodes: RequireNode[]) {
3232
const coldStartSpanStartTime = rootNodes[0]?.startTime;
33-
const coldStartSpan = this.createColdStartSpan(coldStartSpanStartTime, this.parentSpan);
33+
const coldStartSpanEndTime = Math.min(rootNodes[rootNodes.length - 1]?.endTime, this.currentSpanStartTime);
34+
const coldStartSpan = this.createColdStartSpan(coldStartSpanStartTime, coldStartSpanEndTime, this.parentSpan);
3435
for (const coldStartNode of rootNodes) {
3536
this.traceTree(coldStartNode, coldStartSpan);
3637
}
3738
}
3839

39-
private createColdStartSpan(startTime: number, parentSpan: SpanWrapper | undefined): SpanWrapper {
40+
private createColdStartSpan(startTime: number, endTime: number, parentSpan: SpanWrapper | undefined): SpanWrapper {
4041
const options: SpanOptions = {
4142
tags: {
4243
service: "aws.lambda",
@@ -50,7 +51,7 @@ export class ColdStartTracer {
5051
options.childOf = parentSpan.span;
5152
}
5253
const newSpan = new SpanWrapper(this.tracerWrapper.startSpan("aws.lambda.load", options), {});
53-
newSpan.finish(this.coldStartSpanFinishTime);
54+
newSpan.finish(endTime);
5455
return newSpan;
5556
}
5657

src/trace/listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ export class TraceListener {
160160
if (coldStartNodes.length > 0 && didFunctionColdStart()) {
161161
const coldStartConfig: ColdStartTracerConfig = {
162162
tracerWrapper: this.tracerWrapper,
163-
coldStartSpanFinishTime: this.wrappedCurrentSpan?.startTime(),
164163
parentSpan: this.inferredSpan || this.wrappedCurrentSpan,
165164
lambdaFunctionName: this.context?.functionName,
165+
currentSpanStartTime: this.wrappedCurrentSpan?.startTime(),
166166
minDuration: this.config.minColdStartTraceDuration,
167167
ignoreLibs: this.config.coldStartTraceSkipLib,
168168
};

0 commit comments

Comments
 (0)