|
| 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 | + #defaultDimensions: Dimensions = {}; |
| 19 | + |
| 20 | + #getDimensions(): Dimensions { |
| 21 | + if (InvokeStore.getContext() === undefined) { |
| 22 | + return this.#fallbackDimensions; |
| 23 | + } |
| 24 | + |
| 25 | + let stored = InvokeStore.get(this.#dimensionsKey) as Dimensions | undefined; |
| 26 | + if (stored == null) { |
| 27 | + stored = {}; |
| 28 | + InvokeStore.set(this.#dimensionsKey, stored); |
| 29 | + } |
| 30 | + return stored; |
| 31 | + } |
| 32 | + |
| 33 | + #getDimensionSets(): Dimensions[] { |
| 34 | + if (InvokeStore.getContext() === undefined) { |
| 35 | + return this.#fallbackDimensionSets; |
| 36 | + } |
| 37 | + |
| 38 | + let stored = InvokeStore.get(this.#dimensionSetsKey) as |
| 39 | + | Dimensions[] |
| 40 | + | undefined; |
| 41 | + if (stored == null) { |
| 42 | + stored = []; |
| 43 | + InvokeStore.set(this.#dimensionSetsKey, stored); |
| 44 | + } |
| 45 | + return stored; |
| 46 | + } |
| 47 | + |
| 48 | + public addDimension(name: string, value: string): string { |
| 49 | + this.#getDimensions()[name] = value; |
| 50 | + return value; |
| 51 | + } |
| 52 | + |
| 53 | + public addDimensionSet(dimensionSet: Dimensions): Dimensions { |
| 54 | + this.#getDimensionSets().push({ ...dimensionSet }); |
| 55 | + return dimensionSet; |
| 56 | + } |
| 57 | + |
| 58 | + public getDimensions(): Dimensions { |
| 59 | + return { ...this.#getDimensions() }; |
| 60 | + } |
| 61 | + |
| 62 | + public getDimensionSets(): Dimensions[] { |
| 63 | + return this.#getDimensionSets().map((set) => ({ ...set })); |
| 64 | + } |
| 65 | + |
| 66 | + public clearRequestDimensions(): void { |
| 67 | + if (InvokeStore.getContext() === undefined) { |
| 68 | + this.#fallbackDimensions = {}; |
| 69 | + this.#fallbackDimensionSets = []; |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + InvokeStore.set(this.#dimensionsKey, {}); |
| 74 | + InvokeStore.set(this.#dimensionSetsKey, []); |
| 75 | + } |
| 76 | + |
| 77 | + public clearDefaultDimensions(): void { |
| 78 | + this.#defaultDimensions = {}; |
| 79 | + } |
| 80 | + |
| 81 | + public getDimensionCount(): number { |
| 82 | + const dimensions = this.#getDimensions(); |
| 83 | + const dimensionSets = this.#getDimensionSets(); |
| 84 | + const dimensionSetsCount = dimensionSets.reduce( |
| 85 | + (total, dimensionSet) => total + Object.keys(dimensionSet).length, |
| 86 | + 0 |
| 87 | + ); |
| 88 | + return ( |
| 89 | + Object.keys(dimensions).length + |
| 90 | + Object.keys(this.#defaultDimensions).length + |
| 91 | + dimensionSetsCount |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + public setDefaultDimensions(dimensions: Dimensions): void { |
| 96 | + this.#defaultDimensions = { ...dimensions }; |
| 97 | + } |
| 98 | + |
| 99 | + public getDefaultDimensions(): Dimensions { |
| 100 | + return { ...this.#defaultDimensions }; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export { DimensionsStore }; |
0 commit comments