generated from amazon-archives/__template_Apache-2.0
    
        
        - 
                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
          
     Merged
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a3340db
              
                feat(metrics): use async local storage for metrics
              
              
                svozza 50c1ebb
              
                add note about removing withResolvers utility whne we drop support fo…
              
              
                svozza afa3e92
              
                add public keyword to methods in DimensionsStore
              
              
                svozza b614c56
              
                remove unnecessary array copy in serializeMetrics method
              
              
                svozza 220d4b8
              
                Merge branch 'main' into async-local-storage-metrics
              
              
                svozza File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  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.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | 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; | ||
| } | ||
|  | ||
| addDimension(name: string, value: string): string { | ||
|         
                  svozza marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| this.#getDimensions()[name] = value; | ||
| return value; | ||
| } | ||
|  | ||
| addDimensionSet(dimensionSet: Dimensions): Dimensions { | ||
| this.#getDimensionSets().push({ ...dimensionSet }); | ||
| return dimensionSet; | ||
| } | ||
|  | ||
| getDimensions(): Dimensions { | ||
| return { ...this.#getDimensions() }; | ||
|         
                  svozza marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| } | ||
|  | ||
| getDimensionSets(): Dimensions[] { | ||
|         
                  svozza marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| return this.#getDimensionSets().map((set) => ({ ...set })); | ||
| } | ||
|  | ||
| clearRequestDimensions(): void { | ||
|         
                  svozza marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| if (InvokeStore.getContext() === undefined) { | ||
| this.#fallbackDimensions = {}; | ||
| this.#fallbackDimensionSets = []; | ||
| return; | ||
| } | ||
|  | ||
| InvokeStore.set(this.#dimensionsKey, {}); | ||
| InvokeStore.set(this.#dimensionSetsKey, []); | ||
| } | ||
|  | ||
| clearDefaultDimensions(): void { | ||
| this.#defaultDimensions = {}; | ||
| } | ||
|  | ||
| 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 | ||
| ); | ||
| } | ||
|  | ||
| setDefaultDimensions(dimensions: Dimensions): void { | ||
| this.#defaultDimensions = { ...dimensions }; | ||
| } | ||
|  | ||
| getDefaultDimensions(): Dimensions { | ||
| return { ...this.#defaultDimensions }; | ||
| } | ||
| } | ||
|  | ||
| export { DimensionsStore }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 }; | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.