Skip to content

Commit 3769a69

Browse files
authored
feat(tracer): adopted Utility class & updated unit tests (#549)
* feat: adopted Utility class & updated unit tests * docs: added notice in docs * build: update to [email protected]
1 parent 672e6a8 commit 3769a69

File tree

6 files changed

+16
-168
lines changed

6 files changed

+16
-168
lines changed

Diff for: docs/core/tracer.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ For a **complete list** of supported environment variables, refer to [this secti
4848

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

51+
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.
52+
5153
=== "handler.ts"
5254

5355
```typescript hl_lines="1 4"

Diff for: packages/tracing/package-lock.json

+8-123
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/tracing/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"url": "https://github.com/awslabs/aws-lambda-powertools-typescript/issues"
6565
},
6666
"dependencies": {
67-
"@aws-lambda-powertools/commons": "^0.2.0",
67+
"@aws-lambda-powertools/commons": "^0.6.0",
6868
"aws-xray-sdk-core": "^3.3.3"
6969
}
7070
}

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)