Skip to content

Commit f5cae92

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

File tree

12 files changed

+1275
-103
lines changed

12 files changed

+1275
-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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
readonly #dimensionsKey = Symbol('powertools.metrics.dimensions');
14+
readonly #dimensionSetsKey = Symbol('powertools.metrics.dimensionSets');
15+
16+
#fallbackDimensions: Dimensions = {};
17+
#fallbackDimensionSets: Dimensions[] = [];
18+
19+
#getDimensions(): Dimensions {
20+
if (InvokeStore.getContext() === undefined) {
21+
return this.#fallbackDimensions;
22+
}
23+
24+
let stored = InvokeStore.get(this.#dimensionsKey) as Dimensions | undefined;
25+
if (stored == null) {
26+
stored = {};
27+
InvokeStore.set(this.#dimensionsKey, stored);
28+
}
29+
return stored;
30+
}
31+
32+
#getDimensionSets(): Dimensions[] {
33+
if (InvokeStore.getContext() === undefined) {
34+
return this.#fallbackDimensionSets;
35+
}
36+
37+
let stored = InvokeStore.get(this.#dimensionSetsKey) as
38+
| Dimensions[]
39+
| undefined;
40+
if (stored == null) {
41+
stored = [];
42+
InvokeStore.set(this.#dimensionSetsKey, stored);
43+
}
44+
return stored;
45+
}
46+
47+
addDimension(name: string, value: string): void {
48+
this.#getDimensions()[name] = value;
49+
}
50+
51+
addDimensionSet(dimensionSet: Dimensions): void {
52+
this.#getDimensionSets().push({ ...dimensionSet });
53+
}
54+
55+
getDimensions(): Dimensions {
56+
return { ...this.#getDimensions() };
57+
}
58+
59+
getDimensionSets(): Dimensions[] {
60+
return this.#getDimensionSets().map((set) => ({ ...set }));
61+
}
62+
63+
clear(): void {
64+
if (InvokeStore.getContext() === undefined) {
65+
this.#fallbackDimensions = {};
66+
this.#fallbackDimensionSets = [];
67+
return;
68+
}
69+
70+
InvokeStore.set(this.#dimensionsKey, {});
71+
InvokeStore.set(this.#dimensionSetsKey, []);
72+
}
73+
}
74+
75+
export { DimensionsStore };
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
readonly #metadataKey = Symbol('powertools.metrics.metadata');
13+
14+
#fallbackStorage: Record<string, string> = {};
15+
16+
#getStorage(): Record<string, string> {
17+
if (InvokeStore.getContext() === undefined) {
18+
return this.#fallbackStorage;
19+
}
20+
21+
let stored = InvokeStore.get(this.#metadataKey) as
22+
| Record<string, string>
23+
| undefined;
24+
if (stored == null) {
25+
stored = {};
26+
InvokeStore.set(this.#metadataKey, stored);
27+
}
28+
return stored;
29+
}
30+
31+
public set(key: string, value: string): void {
32+
this.#getStorage()[key] = value;
33+
}
34+
35+
public getAll(): Record<string, string> {
36+
return { ...this.#getStorage() };
37+
}
38+
39+
public clear(): void {
40+
if (InvokeStore.getContext() === undefined) {
41+
this.#fallbackStorage = {};
42+
return;
43+
}
44+
45+
InvokeStore.set(this.#metadataKey, {});
46+
}
47+
}
48+
49+
export { MetadataStore };

0 commit comments

Comments
 (0)