Skip to content

Commit 2a9645b

Browse files
committed
feat(metrics): use async local storage for metrics
1 parent 30a01b4 commit 2a9645b

File tree

12 files changed

+1243
-81
lines changed

12 files changed

+1243
-81
lines changed

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/metrics/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"url": "https://github.com/aws-powertools/powertools-lambda-typescript/issues"
8989
},
9090
"dependencies": {
91-
"@aws-lambda-powertools/commons": "2.27.0"
91+
"@aws-lambda-powertools/commons": "2.27.0",
92+
"@aws/lambda-invoke-store": "0.1.0"
9293
},
9394
"keywords": [
9495
"aws",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { InvokeStore } from '@aws/lambda-invoke-store';
2+
import type { Dimensions } from './types/Metrics.js';
3+
4+
/**
5+
* Manages storage of metrics dimensions with automatic context detection.
6+
*
7+
* This class abstracts the storage mechanism for metrics, automatically
8+
* choosing between AsyncLocalStorage (when in async context) and a fallback
9+
* object (when outside async context). The decision is made at runtime on
10+
* every method call to support Lambda's transition to async contexts.
11+
*/
12+
class DimensionsStore {
13+
#fallbackDimensions: Dimensions = {};
14+
#fallbackDimensionSets: Dimensions[] = [];
15+
16+
#getDimensions(): Dimensions {
17+
if (InvokeStore.getContext() === undefined) {
18+
return this.#fallbackDimensions;
19+
}
20+
21+
let stored = InvokeStore.get('dimensions') as Dimensions | undefined;
22+
if (stored == null) {
23+
stored = {};
24+
InvokeStore.set('dimensions', stored);
25+
}
26+
return stored;
27+
}
28+
29+
#getDimensionSets(): Dimensions[] {
30+
if (InvokeStore.getContext() !== undefined) {
31+
let stored = InvokeStore.get('dimensionSets') as Dimensions[] | undefined;
32+
if (stored == null) {
33+
stored = [];
34+
InvokeStore.set('dimensionSets', stored);
35+
}
36+
return stored;
37+
}
38+
return this.#fallbackDimensionSets;
39+
}
40+
41+
addDimension(name: string, value: string): void {
42+
this.#getDimensions()[name] = value;
43+
}
44+
45+
addDimensionSet(dimensionSet: Dimensions): void {
46+
this.#getDimensionSets().push({ ...dimensionSet });
47+
}
48+
49+
getDimensions(): Dimensions {
50+
return { ...this.#getDimensions() };
51+
}
52+
53+
getDimensionSets(): Dimensions[] {
54+
return this.#getDimensionSets().map((set) => ({ ...set }));
55+
}
56+
57+
clear(): void {
58+
if (InvokeStore.getContext() !== undefined) {
59+
InvokeStore.set('dimensions', {});
60+
InvokeStore.set('dimensionSets', []);
61+
} else {
62+
this.#fallbackDimensions = {};
63+
this.#fallbackDimensionSets = [];
64+
}
65+
}
66+
}
67+
68+
export { DimensionsStore };
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { InvokeStore } from '@aws/lambda-invoke-store';
2+
3+
/**
4+
* Manages storage of metrics #metadata with automatic context detection.
5+
*
6+
* This class abstracts the storage mechanism for metrics, automatically
7+
* choosing between AsyncLocalStorage (when in async context) and a fallback
8+
* object (when outside async context). The decision is made at runtime on
9+
* every method call to support Lambda's transition to async contexts.
10+
*/
11+
class MetadataStore {
12+
#fallbackStorage: Record<string, string> = {};
13+
14+
#getStorage(): Record<string, string> {
15+
if (InvokeStore.getContext() === undefined) {
16+
return this.#fallbackStorage;
17+
}
18+
19+
let stored = InvokeStore.get('#metadata') as
20+
| Record<string, string>
21+
| undefined;
22+
if (stored == null) {
23+
stored = {};
24+
InvokeStore.set('#metadata', stored);
25+
}
26+
return stored;
27+
}
28+
29+
public set(key: string, value: string): void {
30+
this.#getStorage()[key] = value;
31+
}
32+
33+
public getAll(): Record<string, string> {
34+
return { ...this.#getStorage() };
35+
}
36+
37+
public clear(): void {
38+
if (InvokeStore.getContext() !== undefined) {
39+
InvokeStore.set('#metadata', {});
40+
} else {
41+
this.#fallbackStorage = {};
42+
}
43+
}
44+
}
45+
46+
export { MetadataStore };

0 commit comments

Comments
 (0)