Skip to content

Commit 0c2cbd2

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

File tree

11 files changed

+1309
-135
lines changed

11 files changed

+1309
-135
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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): string {
48+
this.#getDimensions()[name] = value;
49+
return value;
50+
}
51+
52+
addDimensionSet(dimensionSet: Dimensions): Dimensions {
53+
this.#getDimensionSets().push({ ...dimensionSet });
54+
return dimensionSet;
55+
}
56+
57+
getDimensions(): Dimensions {
58+
return { ...this.#getDimensions() };
59+
}
60+
61+
getDimensionSets(): Dimensions[] {
62+
return this.#getDimensionSets().map((set) => ({ ...set }));
63+
}
64+
65+
clear(): void {
66+
if (InvokeStore.getContext() === undefined) {
67+
this.#fallbackDimensions = {};
68+
this.#fallbackDimensionSets = [];
69+
return;
70+
}
71+
72+
InvokeStore.set(this.#dimensionsKey, {});
73+
InvokeStore.set(this.#dimensionSetsKey, []);
74+
}
75+
76+
getDimensionCount(defaultDimensions: Dimensions): number {
77+
const dimensions = this.#getDimensions();
78+
const dimensionSets = this.#getDimensionSets();
79+
const dimensionSetsCount = dimensionSets.reduce(
80+
(total, dimensionSet) => total + Object.keys(dimensionSet).length,
81+
0
82+
);
83+
return (
84+
Object.keys(dimensions).length +
85+
Object.keys(defaultDimensions).length +
86+
dimensionSetsCount
87+
);
88+
}
89+
}
90+
91+
export { DimensionsStore };
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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): string {
32+
this.#getStorage()[key] = value;
33+
return value;
34+
}
35+
36+
public getAll(): Record<string, string> {
37+
return { ...this.#getStorage() };
38+
}
39+
40+
public clear(): void {
41+
if (InvokeStore.getContext() === undefined) {
42+
this.#fallbackStorage = {};
43+
return;
44+
}
45+
46+
InvokeStore.set(this.#metadataKey, {});
47+
}
48+
}
49+
50+
export { MetadataStore };

0 commit comments

Comments
 (0)