Skip to content

Commit

Permalink
feat(tracer): adopted Utility class & updated unit tests (#549)
Browse files Browse the repository at this point in the history
* feat: adopted Utility class & updated unit tests

* docs: added notice in docs

* build: update to commons@0.6.0
  • Loading branch information
dreamorosi authored Feb 22, 2022
1 parent 672e6a8 commit 3769a69
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 168 deletions.
2 changes: 2 additions & 0 deletions docs/core/tracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ For a **complete list** of supported environment variables, refer to [this secti

#### Example using AWS Serverless Application Model (SAM)

The `Tracer` utility is instantiated outside of the Lambda handler. In doing this, the same instance can be used across multiple invocations inside the same execution environment. This allows `Metrics` to be aware of things like whether or not a given invocation had a cold start or not.

=== "handler.ts"

```typescript hl_lines="1 4"
Expand Down
131 changes: 8 additions & 123 deletions packages/tracing/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/tracing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"url": "https://github.com/awslabs/aws-lambda-powertools-typescript/issues"
},
"dependencies": {
"@aws-lambda-powertools/commons": "^0.2.0",
"@aws-lambda-powertools/commons": "^0.6.0",
"aws-xray-sdk-core": "^3.3.3"
}
}
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 3769a69

Please sign in to comment.