Skip to content

Commit cdc5f06

Browse files
committed
feat: adopted Utility class & updated unit tests
1 parent 47522fc commit cdc5f06

File tree

3 files changed

+5
-44
lines changed

3 files changed

+5
-44
lines changed

Diff for: packages/tracing/src/Tracer.ts

+5-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Handler } from 'aws-lambda';
2+
import { Utility } from '@aws-lambda-powertools/commons';
23
import { TracerInterface } from '.';
34
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
45
import { HandlerMethodDecorator, TracerOptions, MethodDecorator } from './types';
@@ -109,9 +110,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
109110
* }
110111
* ```
111112
*/
112-
class Tracer implements TracerInterface {
113-
114-
public static coldStart: boolean = true;
113+
class Tracer extends Utility implements TracerInterface {
115114

116115
public provider: ProviderServiceInterface;
117116

@@ -128,6 +127,8 @@ class Tracer implements TracerInterface {
128127
private tracingEnabled: boolean = true;
129128

130129
public constructor(options: TracerOptions = {}) {
130+
super();
131+
131132
this.setOptions(options);
132133
this.provider = new ProviderService();
133134
if (!this.isTracingEnabled()) {
@@ -196,10 +197,7 @@ class Tracer implements TracerInterface {
196197
*/
197198
public annotateColdStart(): void {
198199
if (this.isTracingEnabled()) {
199-
this.putAnnotation('ColdStart', Tracer.coldStart);
200-
}
201-
if (Tracer.coldStart) {
202-
Tracer.coldStart = false;
200+
this.putAnnotation('ColdStart', this.getColdStart());
203201
}
204202
}
205203

@@ -430,27 +428,6 @@ class Tracer implements TracerInterface {
430428
return descriptor;
431429
};
432430
}
433-
434-
/**
435-
* Retrieve the current value of `ColdStart`.
436-
*
437-
* If Tracer has been initialized outside the Lambda handler then the same instance
438-
* of Tracer will be reused throughout the lifecycle of that same Lambda execution environment
439-
* and this method will return `false` after the first invocation.
440-
*
441-
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
442-
*
443-
* @returns boolean - `true` if is cold start, otherwise `false`
444-
*/
445-
public static getColdStart(): boolean {
446-
if (Tracer.coldStart) {
447-
Tracer.coldStart = false;
448-
449-
return true;
450-
}
451-
452-
return false;
453-
}
454431

455432
/**
456433
* Get the active segment or subsegment in the current scope.

Diff for: packages/tracing/tests/unit/Tracer.test.ts

-15
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ describe('Class: Tracer', () => {
4040
};
4141

4242
beforeEach(() => {
43-
Tracer.coldStart = true;
4443
jest.clearAllMocks();
4544
jest.resetModules();
4645
process.env = { ...ENVIRONMENT_VARIABLES };
@@ -259,20 +258,6 @@ describe('Class: Tracer', () => {
259258

260259
});
261260

262-
describe('Method: getColdStart', () => {
263-
264-
test('when called, it returns false the first time and always true after that', () => {
265-
266-
// Assess
267-
expect(Tracer.getColdStart()).toBe(true);
268-
expect(Tracer.getColdStart()).toBe(false);
269-
expect(Tracer.getColdStart()).toBe(false);
270-
expect(Tracer.getColdStart()).toBe(false);
271-
272-
});
273-
274-
});
275-
276261
describe('Method: getSegment', () => {
277262

278263
test('when called outside of a namespace or without parent segment, and tracing is enabled, it throws an error', () => {

Diff for: packages/tracing/tests/unit/middy.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ describe('Middy middleware', () => {
3232
};
3333

3434
beforeEach(() => {
35-
Tracer.coldStart = true;
3635
jest.clearAllMocks();
3736
jest.resetModules();
3837
process.env = { ...ENVIRONMENT_VARIABLES };

0 commit comments

Comments
 (0)