Skip to content

Commit b73b723

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

File tree

12 files changed

+1266
-103
lines changed

12 files changed

+1266
-103
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
return this.#fallbackDimensionSets;
32+
}
33+
34+
let stored = InvokeStore.get('dimensionSets') as Dimensions[] | undefined;
35+
if (stored == null) {
36+
stored = [];
37+
InvokeStore.set('dimensionSets', stored);
38+
}
39+
return stored;
40+
}
41+
42+
addDimension(name: string, value: string): void {
43+
this.#getDimensions()[name] = value;
44+
}
45+
46+
addDimensionSet(dimensionSet: Dimensions): void {
47+
this.#getDimensionSets().push({ ...dimensionSet });
48+
}
49+
50+
getDimensions(): Dimensions {
51+
return { ...this.#getDimensions() };
52+
}
53+
54+
getDimensionSets(): Dimensions[] {
55+
return this.#getDimensionSets().map((set) => ({ ...set }));
56+
}
57+
58+
clear(): void {
59+
if (InvokeStore.getContext() === undefined) {
60+
this.#fallbackDimensions = {};
61+
this.#fallbackDimensionSets = [];
62+
return;
63+
}
64+
65+
InvokeStore.set('dimensions', {});
66+
InvokeStore.set('dimensionSets', []);
67+
}
68+
}
69+
70+
export { DimensionsStore };
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
this.#fallbackStorage = {};
40+
return;
41+
}
42+
43+
InvokeStore.set('#metadata', {});
44+
}
45+
}
46+
47+
export { MetadataStore };

0 commit comments

Comments
 (0)