Skip to content

Commit

Permalink
feat: adopted Utility class & updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Feb 10, 2022
1 parent 313596c commit 059ee9c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 44 deletions.
33 changes: 5 additions & 28 deletions packages/tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Handler } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { TracerInterface } from '.';
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
import { HandlerMethodDecorator, TracerOptions, MethodDecorator } from './types';
Expand Down Expand Up @@ -109,9 +110,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
* }
* ```
*/
class Tracer implements TracerInterface {

public static coldStart: boolean = true;
class Tracer extends Utility implements TracerInterface {

public provider: ProviderServiceInterface;

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

public constructor(options: TracerOptions = {}) {
super();

this.setOptions(options);
this.provider = new ProviderService();
if (!this.isTracingEnabled()) {
Expand Down Expand Up @@ -196,10 +197,7 @@ class Tracer implements TracerInterface {
*/
public annotateColdStart(): void {
if (this.isTracingEnabled()) {
this.putAnnotation('ColdStart', Tracer.coldStart);
}
if (Tracer.coldStart) {
Tracer.coldStart = false;
this.putAnnotation('ColdStart', this.getColdStart());
}
}

Expand Down Expand Up @@ -430,27 +428,6 @@ class Tracer implements TracerInterface {
return descriptor;
};
}

/**
* Retrieve the current value of `ColdStart`.
*
* If Tracer has been initialized outside the Lambda handler then the same instance
* of Tracer will be reused throughout the lifecycle of that same Lambda execution environment
* and this method will return `false` after the first invocation.
*
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
*
* @returns boolean - `true` if is cold start, otherwise `false`
*/
public static getColdStart(): boolean {
if (Tracer.coldStart) {
Tracer.coldStart = false;

return true;
}

return false;
}

/**
* Get the active segment or subsegment in the current scope.
Expand Down
15 changes: 0 additions & 15 deletions packages/tracing/tests/unit/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('Class: Tracer', () => {
};

beforeEach(() => {
Tracer.coldStart = true;
jest.clearAllMocks();
jest.resetModules();
process.env = { ...ENVIRONMENT_VARIABLES };
Expand Down Expand Up @@ -259,20 +258,6 @@ describe('Class: Tracer', () => {

});

describe('Method: getColdStart', () => {

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

// Assess
expect(Tracer.getColdStart()).toBe(true);
expect(Tracer.getColdStart()).toBe(false);
expect(Tracer.getColdStart()).toBe(false);
expect(Tracer.getColdStart()).toBe(false);

});

});

describe('Method: getSegment', () => {

test('when called outside of a namespace or without parent segment, and tracing is enabled, it throws an error', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/tracing/tests/unit/middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('Middy middleware', () => {
};

beforeEach(() => {
Tracer.coldStart = true;
jest.clearAllMocks();
jest.resetModules();
process.env = { ...ENVIRONMENT_VARIABLES };
Expand Down

0 comments on commit 059ee9c

Please sign in to comment.