Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tracer): adopted Utility class & updated unit tests #549

Merged
merged 3 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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