Skip to content

Commit 86aa35e

Browse files
committed
feat(commons): make utilities aware of provisioned concurrency
1 parent 4f8b15a commit 86aa35e

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

packages/commons/src/Utility.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,43 @@
5151
* ```
5252
*/
5353
export class Utility {
54+
#initializationType: 'unknown' | 'on-demand' | 'provisioned-concurrency';
5455
protected coldStart = true;
5556
protected readonly defaultServiceName: string = 'service_undefined';
5657

58+
public constructor() {
59+
this.#initializationType = this.getInitializationType();
60+
if (this.#initializationType !== 'on-demand') {
61+
this.coldStart = false;
62+
}
63+
}
64+
65+
/**
66+
* Get the value of the `POWERTOOLS_SERVICE_NAME` environment variable.
67+
*/
68+
protected getInitializationType():
69+
| 'unknown'
70+
| 'on-demand'
71+
| 'provisioned-concurrency' {
72+
const envVarValue = process.env.AWS_LAMBDA_INITIALIZATION_TYPE?.trim();
73+
if (envVarValue === 'on-demand') {
74+
return 'on-demand';
75+
}
76+
if (envVarValue === 'provisioned-concurrency') {
77+
return 'provisioned-concurrency';
78+
}
79+
return 'unknown';
80+
}
81+
5782
/**
5883
* Get the cold start status of the current execution environment.
5984
*
6085
* The method also flips the cold start status to `false` after the first invocation.
6186
*/
6287
protected getColdStart(): boolean {
88+
if (this.#initializationType !== 'on-demand') {
89+
return false;
90+
}
6391
if (this.coldStart) {
6492
this.coldStart = false;
6593

packages/commons/tests/unit/Utility.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ describe('Class: Utility', () => {
1515
public validateServiceName(serviceName: string): boolean {
1616
return this.isValidServiceName(serviceName);
1717
}
18+
public getInitializationType():
19+
| 'unknown'
20+
| 'on-demand'
21+
| 'provisioned-concurrency' {
22+
return super.getInitializationType();
23+
}
1824
}
1925

2026
it('returns the correct cold start value', () => {
@@ -27,6 +33,15 @@ describe('Class: Utility', () => {
2733
expect(utility.dummyMethod()).toBe(false);
2834
});
2935

36+
it('returns the correct cold start value when provisioned concurrency is used', () => {
37+
// Prepare
38+
process.env.AWS_LAMBDA_INITIALIZATION_TYPE = 'provisioned-concurrency';
39+
const utility = new TestUtility();
40+
41+
// Act & Assess
42+
expect(utility.dummyMethod()).toBe(false);
43+
});
44+
3045
it('flips the cold start value', () => {
3146
// Prepare
3247
const utility = new TestUtility();
@@ -54,4 +69,23 @@ describe('Class: Utility', () => {
5469
expect(utility.validateServiceName('serverlessAirline')).toBe(true);
5570
expect(utility.validateServiceName('')).toBe(false);
5671
});
72+
73+
it.each([
74+
{ value: 'on-demand', expected: 'on-demand' },
75+
{ value: 'provisioned-concurrency', expected: 'provisioned-concurrency' },
76+
{ value: '', expected: 'unknown' },
77+
])(
78+
'returns the correct initialization type ($value)',
79+
({ value, expected }) => {
80+
// Prepare
81+
process.env.AWS_LAMBDA_INITIALIZATION_TYPE = value;
82+
const utility = new TestUtility();
83+
84+
// Act
85+
const initializationType = utility.getInitializationType();
86+
87+
// Assess
88+
expect(initializationType).toBe(expected);
89+
}
90+
);
5791
});

packages/commons/tests/unit/awsSdkUtils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {
66
PT_VERSION as version,
77
} from '../../src/index.js';
88

9+
vi.hoisted(() => {
10+
process.env.AWS_EXECUTION_ENV = '';
11+
});
12+
913
describe('Helpers: awsSdk', () => {
1014
describe('Function: userAgentMiddleware', () => {
1115
beforeAll(() => {

packages/commons/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { defineProject } from 'vitest/config';
33
export default defineProject({
44
test: {
55
environment: 'node',
6+
setupFiles: ['../testing/src/setupEnv.ts'],
67
},
78
});

packages/testing/src/setupEnv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,4 @@ if (
377377
process.env._HANDLER = 'index.handler';
378378
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
379379
process.env.AWS_XRAY_LOGGING_LEVEL = 'silent';
380+
process.env.AWS_LAMBDA_INITIALIZATION_TYPE = 'on-demand';

0 commit comments

Comments
 (0)