-
Notifications
You must be signed in to change notification settings - Fork 176
feat(metrics): use async local storage for metrics #4663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a3340db
50c1ebb
afa3e92
b614c56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { InvokeStore } from '@aws/lambda-invoke-store'; | ||
import type { Dimensions } from './types/Metrics.js'; | ||
|
||
/** | ||
* Manages storage of metrics dimensions with automatic context detection. | ||
* | ||
* This class abstracts the storage mechanism for metrics, automatically | ||
* choosing between AsyncLocalStorage (when in async context) and a fallback | ||
* object (when outside async context). The decision is made at runtime on | ||
* every method call to support Lambda's transition to async contexts. | ||
*/ | ||
class DimensionsStore { | ||
svozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readonly #dimensionsKey = Symbol('powertools.metrics.dimensions'); | ||
svozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readonly #dimensionSetsKey = Symbol('powertools.metrics.dimensionSets'); | ||
|
||
#fallbackDimensions: Dimensions = {}; | ||
#fallbackDimensionSets: Dimensions[] = []; | ||
#defaultDimensions: Dimensions = {}; | ||
|
||
#getDimensions(): Dimensions { | ||
if (InvokeStore.getContext() === undefined) { | ||
return this.#fallbackDimensions; | ||
} | ||
|
||
let stored = InvokeStore.get(this.#dimensionsKey) as Dimensions | undefined; | ||
if (stored == null) { | ||
stored = {}; | ||
InvokeStore.set(this.#dimensionsKey, stored); | ||
} | ||
return stored; | ||
} | ||
|
||
#getDimensionSets(): Dimensions[] { | ||
if (InvokeStore.getContext() === undefined) { | ||
return this.#fallbackDimensionSets; | ||
} | ||
|
||
let stored = InvokeStore.get(this.#dimensionSetsKey) as | ||
| Dimensions[] | ||
| undefined; | ||
if (stored == null) { | ||
stored = []; | ||
InvokeStore.set(this.#dimensionSetsKey, stored); | ||
} | ||
return stored; | ||
} | ||
|
||
public addDimension(name: string, value: string): string { | ||
this.#getDimensions()[name] = value; | ||
return value; | ||
} | ||
|
||
public addDimensionSet(dimensionSet: Dimensions): Dimensions { | ||
this.#getDimensionSets().push({ ...dimensionSet }); | ||
return dimensionSet; | ||
} | ||
|
||
public getDimensions(): Dimensions { | ||
return { ...this.#getDimensions() }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we spreading and creating a new object here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I don't want it to be possible to get a reference to the underlying object and be able to mutate it without going through the store interface. |
||
} | ||
|
||
public getDimensionSets(): Dimensions[] { | ||
return this.#getDimensionSets().map((set) => ({ ...set })); | ||
} | ||
|
||
public clearRequestDimensions(): void { | ||
if (InvokeStore.getContext() === undefined) { | ||
this.#fallbackDimensions = {}; | ||
this.#fallbackDimensionSets = []; | ||
return; | ||
} | ||
|
||
InvokeStore.set(this.#dimensionsKey, {}); | ||
InvokeStore.set(this.#dimensionSetsKey, []); | ||
} | ||
|
||
public clearDefaultDimensions(): void { | ||
this.#defaultDimensions = {}; | ||
} | ||
|
||
public getDimensionCount(): number { | ||
const dimensions = this.#getDimensions(); | ||
const dimensionSets = this.#getDimensionSets(); | ||
const dimensionSetsCount = dimensionSets.reduce( | ||
(total, dimensionSet) => total + Object.keys(dimensionSet).length, | ||
0 | ||
); | ||
return ( | ||
Object.keys(dimensions).length + | ||
Object.keys(this.#defaultDimensions).length + | ||
dimensionSetsCount | ||
); | ||
} | ||
|
||
public setDefaultDimensions(dimensions: Dimensions): void { | ||
this.#defaultDimensions = { ...dimensions }; | ||
} | ||
|
||
public getDefaultDimensions(): Dimensions { | ||
return { ...this.#defaultDimensions }; | ||
} | ||
} | ||
|
||
export { DimensionsStore }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { InvokeStore } from '@aws/lambda-invoke-store'; | ||
|
||
/** | ||
* Manages storage of metrics #metadata with automatic context detection. | ||
* | ||
* This class abstracts the storage mechanism for metrics, automatically | ||
* choosing between AsyncLocalStorage (when in async context) and a fallback | ||
* object (when outside async context). The decision is made at runtime on | ||
* every method call to support Lambda's transition to async contexts. | ||
*/ | ||
class MetadataStore { | ||
readonly #metadataKey = Symbol('powertools.metrics.metadata'); | ||
|
||
#fallbackStorage: Record<string, string> = {}; | ||
|
||
#getStorage(): Record<string, string> { | ||
if (InvokeStore.getContext() === undefined) { | ||
return this.#fallbackStorage; | ||
} | ||
|
||
let stored = InvokeStore.get(this.#metadataKey) as | ||
| Record<string, string> | ||
| undefined; | ||
if (stored == null) { | ||
stored = {}; | ||
InvokeStore.set(this.#metadataKey, stored); | ||
} | ||
return stored; | ||
} | ||
|
||
public set(key: string, value: string): string { | ||
this.#getStorage()[key] = value; | ||
return value; | ||
} | ||
|
||
public getAll(): Record<string, string> { | ||
return { ...this.#getStorage() }; | ||
} | ||
|
||
public clear(): void { | ||
if (InvokeStore.getContext() === undefined) { | ||
this.#fallbackStorage = {}; | ||
return; | ||
} | ||
|
||
InvokeStore.set(this.#metadataKey, {}); | ||
} | ||
} | ||
|
||
export { MetadataStore }; |
Uh oh!
There was an error while loading. Please reload this page.