Skip to content

Commit de86d97

Browse files
committed
Exposed annotateColdStarted
1 parent 57bf02d commit de86d97

File tree

5 files changed

+86
-46
lines changed

5 files changed

+86
-46
lines changed

docs/core/tracer.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
8585
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
8686
// Create subsegment for the function
8787
const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`);
88-
// TODO: expose tracer.annotateColdStart()
89-
this.putAnnotation('ColdStart', tracer.isColdStart());
88+
tracer.annotateColdStart()
9089

9190
let res;
9291
try {

packages/tracing/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ export const handler = async (_event: any, context: any) => {
7070
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
7171
// Create subsegment for the function
7272
const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`);
73-
// TODO: expose tracer.annotateColdStart()
74-
this.putAnnotation('ColdStart', tracer.isColdStart());
73+
tracer.annotateColdStart()
7574

7675
let res;
7776
try {

packages/tracing/src/Tracer.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
8383
* const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
8484
* // Create subsegment for the function
8585
* const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`);
86-
* // TODO: expose tracer.annotateColdStart()
87-
* this.putAnnotation('ColdStart', tracer.isColdStart());
86+
* tracer.annotateColdStart()
8887
*
8988
* let res;
9089
* try {
@@ -104,6 +103,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
104103
* ```
105104
*/
106105
class Tracer implements TracerInterface {
106+
107107
public static coldStart: boolean = true;
108108

109109
public provider: ProviderServiceInterface;
@@ -163,6 +163,25 @@ class Tracer implements TracerInterface {
163163
this.putMetadata(`${methodName} response`, data);
164164
}
165165

166+
/**
167+
* Add ColdStart annotation to the current segment or subsegment.
168+
*
169+
* If Tracer has been initialized outside of the Lambda handler then the same instance
170+
* of Tracer will be reused throghout the lifecycle of that same Lambda execution environment
171+
* and this method will return `false` after the first invocation.
172+
*
173+
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
174+
*/
175+
public annotateColdStart(): void {
176+
if (Tracer.coldStart === true) {
177+
Tracer.coldStart = false;
178+
179+
if (this.tracingEnabled === true) {
180+
this.putAnnotation('ColdStart', true);
181+
}
182+
}
183+
}
184+
166185
/**
167186
* Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
168187
*
@@ -376,6 +395,27 @@ class Tracer implements TracerInterface {
376395
return descriptor;
377396
};
378397
}
398+
399+
/**
400+
* Retrieve the current value of `ColdStart`.
401+
*
402+
* If Tracer has been initialized outside of the Lambda handler then the same instance
403+
* of Tracer will be reused throghout the lifecycle of that same Lambda execution environment
404+
* and this method will return `false` after the first invocation.
405+
*
406+
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
407+
*
408+
* @returns boolean - `true` if is cold start, otherwise `false`
409+
*/
410+
public static getColdStart(): boolean {
411+
if (Tracer.coldStart === true) {
412+
Tracer.coldStart = false;
413+
414+
return true;
415+
}
416+
417+
return false;
418+
}
379419

380420
/**
381421
* Get the active segment or subsegment in the current scope.
@@ -408,27 +448,6 @@ class Tracer implements TracerInterface {
408448
return segment;
409449
}
410450

411-
/**
412-
* Retrieve the current value of `ColdStart`.
413-
*
414-
* If Tracer has been initialized outside of the Lambda handler then the same instance
415-
* of Tracer will be reused throghout the lifecycle of that same Lambda execution environment
416-
* and this method will return `false` after the first invocation.
417-
*
418-
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
419-
*
420-
* @returns boolean - `true` if is cold start, otherwise `false`
421-
*/
422-
public static isColdStart(): boolean {
423-
if (Tracer.coldStart === true) {
424-
Tracer.coldStart = false;
425-
426-
return true;
427-
}
428-
429-
return false;
430-
}
431-
432451
/**
433452
* Get the current value of the `tracingEnabled` property.
434453
*
@@ -532,16 +551,6 @@ class Tracer implements TracerInterface {
532551
public setSegment(segment: Segment | Subsegment): void {
533552
return this.provider.setSegment(segment);
534553
}
535-
536-
/**
537-
* Add ColdStart annotation to the current segment or subsegment.
538-
* Used internally by decoratorators and middlewares.
539-
*/
540-
private annotateColdStart(): void {
541-
if (Tracer.isColdStart()) {
542-
this.putAnnotation('ColdStart', true);
543-
}
544-
}
545554

546555
/**
547556
* Getter for `customConfigService`.

packages/tracing/src/middleware/middy.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ const captureLambdaHandler = (target: Tracer): middy.MiddlewareObj => {
3131
if (target.isTracingEnabled()) {
3232
const subsegment = new Subsegment(`## ${request.context.functionName}`);
3333
target.setSegment(subsegment);
34-
35-
if (Tracer.isColdStart()) {
36-
target.putAnnotation('ColdStart', true);
37-
}
34+
target.annotateColdStart();
3835
}
3936
};
4037

packages/tracing/tests/unit/Tracer.test.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,42 @@ describe('Class: Tracer', () => {
2424
process.env = ENVIRONMENT_VARIABLES;
2525
});
2626

27+
describe('Method: annotateColdStart', () => {
28+
29+
test('when called while tracing is disabled, it does nothing', () => {
30+
31+
// Prepare
32+
const tracer: Tracer = new Tracer({ enabled: false });
33+
const putAnnotationSpy = jest.spyOn(tracer, 'putAnnotation');
34+
35+
// Act
36+
tracer.annotateColdStart();
37+
38+
// Assess
39+
expect(putAnnotationSpy).toBeCalledTimes(0);
40+
41+
});
42+
43+
test('when called multiple times, it annotates the first time and then never again', () => {
44+
45+
// Prepare
46+
const tracer: Tracer = new Tracer();
47+
const putAnnotationSpy = jest.spyOn(tracer, 'putAnnotation').mockImplementation(() => null);
48+
49+
// Act
50+
tracer.annotateColdStart();
51+
tracer.annotateColdStart();
52+
tracer.annotateColdStart();
53+
tracer.annotateColdStart();
54+
55+
// Assess
56+
expect(putAnnotationSpy).toBeCalledTimes(1);
57+
expect(putAnnotationSpy).toBeCalledWith('ColdStart', true);
58+
59+
});
60+
61+
});
62+
2763
describe('Method: addResponseAsMetadata', () => {
2864

2965
test('when called while tracing is disabled, it does nothing', () => {
@@ -144,15 +180,15 @@ describe('Class: Tracer', () => {
144180

145181
});
146182

147-
describe('Method: isColdStart', () => {
183+
describe('Method: getColdStart', () => {
148184

149185
test('when called, it returns false the first time and always true after that', () => {
150186

151187
// Assess
152-
expect(Tracer.isColdStart()).toBe(true);
153-
expect(Tracer.isColdStart()).toBe(false);
154-
expect(Tracer.isColdStart()).toBe(false);
155-
expect(Tracer.isColdStart()).toBe(false);
188+
expect(Tracer.getColdStart()).toBe(true);
189+
expect(Tracer.getColdStart()).toBe(false);
190+
expect(Tracer.getColdStart()).toBe(false);
191+
expect(Tracer.getColdStart()).toBe(false);
156192

157193
});
158194

0 commit comments

Comments
 (0)