Skip to content

Commit 0763143

Browse files
committed
Feature parity w/ Python #851
1 parent de86d97 commit 0763143

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

packages/tracing/src/Tracer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ class Tracer implements TracerInterface {
173173
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
174174
*/
175175
public annotateColdStart(): void {
176+
if (this.tracingEnabled === true) {
177+
this.putAnnotation('ColdStart', Tracer.coldStart);
178+
}
176179
if (Tracer.coldStart === true) {
177180
Tracer.coldStart = false;
178-
179-
if (this.tracingEnabled === true) {
180-
this.putAnnotation('ColdStart', true);
181-
}
182181
}
183182
}
184183

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Class: Tracer', () => {
4040

4141
});
4242

43-
test('when called multiple times, it annotates the first time and then never again', () => {
43+
test('when called multiple times, it annotates true the first time and then false afterwards', () => {
4444

4545
// Prepare
4646
const tracer: Tracer = new Tracer();
@@ -53,8 +53,13 @@ describe('Class: Tracer', () => {
5353
tracer.annotateColdStart();
5454

5555
// Assess
56-
expect(putAnnotationSpy).toBeCalledTimes(1);
57-
expect(putAnnotationSpy).toBeCalledWith('ColdStart', true);
56+
expect(putAnnotationSpy).toBeCalledTimes(4);
57+
expect(putAnnotationSpy.mock.calls).toEqual([
58+
[ 'ColdStart', true ],
59+
[ 'ColdStart', false ],
60+
[ 'ColdStart', false ],
61+
[ 'ColdStart', false ],
62+
]);
5863

5964
});
6065

@@ -666,7 +671,7 @@ describe('Class: Tracer', () => {
666671
.mockImplementation(() => newSubsegmentSecondInvocation);
667672
setContextMissingStrategy(() => null);
668673
const captureAsyncFuncSpy = jest.spyOn(tracer.provider, 'captureAsyncFunc');
669-
const addAnnotationSpy = jest.spyOn(tracer, 'putAnnotation');
674+
const putAnnotationSpy = jest.spyOn(tracer, 'putAnnotation');
670675
class Lambda implements LambdaInterface {
671676

672677
@tracer.captureLambdaHanlder()
@@ -687,16 +692,22 @@ describe('Class: Tracer', () => {
687692
// Assess
688693
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(2);
689694
expect(captureAsyncFuncSpy).toHaveBeenCalledWith('## foo-bar-function', expect.anything());
690-
expect(addAnnotationSpy).toHaveBeenCalledTimes(1);
691-
expect(addAnnotationSpy).toHaveBeenCalledWith('ColdStart', true);
695+
expect(putAnnotationSpy).toHaveBeenCalledTimes(2);
696+
expect(putAnnotationSpy.mock.calls).toEqual([
697+
[ 'ColdStart', true ],
698+
[ 'ColdStart', false ],
699+
]);
692700
expect(newSubsegmentFirstInvocation).toEqual(expect.objectContaining({
693701
name: '## foo-bar-function',
694702
annotations: {
695703
'ColdStart': true,
696704
}
697705
}));
698706
expect(newSubsegmentSecondInvocation).toEqual(expect.objectContaining({
699-
name: '## foo-bar-function'
707+
name: '## foo-bar-function',
708+
annotations: {
709+
'ColdStart': false,
710+
}
700711
}));
701712

702713
});

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ describe('Middy middlewares', () => {
216216
.mockImplementationOnce(() => newSubsegmentFirstInvocation)
217217
.mockImplementation(() => newSubsegmentSecondInvocation);
218218
setContextMissingStrategy(() => null);
219-
const addAnnotationSpy = jest.spyOn(tracer, 'putAnnotation');
219+
const putAnnotationSpy = jest.spyOn(tracer, 'putAnnotation');
220220
const lambdaHandler: Handler = async (_event: unknown, _context: Context) => ({
221221
foo: 'bar'
222222
});
@@ -232,16 +232,22 @@ describe('Middy middlewares', () => {
232232
expect(setSegmentSpy).toHaveBeenCalledWith(expect.objectContaining({
233233
name: '## foo-bar-function',
234234
}));
235-
expect(addAnnotationSpy).toHaveBeenCalledTimes(1);
236-
expect(addAnnotationSpy).toHaveBeenCalledWith('ColdStart', true);
235+
expect(putAnnotationSpy).toHaveBeenCalledTimes(2);
236+
expect(putAnnotationSpy.mock.calls).toEqual([
237+
[ 'ColdStart', true ],
238+
[ 'ColdStart', false ],
239+
]);
237240
expect(newSubsegmentFirstInvocation).toEqual(expect.objectContaining({
238241
name: '## foo-bar-function',
239242
annotations: {
240243
'ColdStart': true,
241244
}
242245
}));
243246
expect(newSubsegmentSecondInvocation).toEqual(expect.objectContaining({
244-
name: '## foo-bar-function'
247+
name: '## foo-bar-function',
248+
annotations: {
249+
'ColdStart': false,
250+
}
245251
}));
246252

247253
});

0 commit comments

Comments
 (0)