- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 252
 
feat: add tracing to multichain account service #7006
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
          
     Open
      
      
            montelaidev
  wants to merge
  13
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
fix/mul-1186
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +1,076
        
        
          −107
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      117ee20
              
                feat: add tracing to multichain account service
              
              
                montelaidev af3d908
              
                chore: update changelog
              
              
                montelaidev d5c1eb3
              
                fix: lint
              
              
                montelaidev 3190bca
              
                fix: add test coverage
              
              
                montelaidev 291b8c6
              
                Merge remote-tracking branch 'origin/main' into fix/mul-1186
              
              
                montelaidev 5170994
              
                refactor: discover to AccountProviderWrapper
              
              
                montelaidev 0f351eb
              
                fix: lint
              
              
                montelaidev 90d5e8c
              
                fix: check provider before determining name
              
              
                montelaidev 82bf3f7
              
                Merge remote-tracking branch 'origin/main' into fix/mul-1186
              
              
                montelaidev a06ed0a
              
                chore: add comment
              
              
                montelaidev 56a9cc2
              
                refactor: move trace to concrete provider
              
              
                montelaidev a38300d
              
                Merge remote-tracking branch 'origin/main' into fix/mul-1186
              
              
                montelaidev ccec24f
              
                Merge remote-tracking branch 'origin/main' into fix/mul-1186
              
              
                montelaidev 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
  
    
      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
    
  
  
    
              
  
    
      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 @@ | ||
| export * from './traces'; | 
        
          
  
    
      
          
            76 changes: 76 additions & 0 deletions
          
          76 
        
  packages/multichain-account-service/src/analytics/traces.test.ts
  
  
      
      
   
        
      
      
    
  
    
      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,76 @@ | ||
| import type { TraceRequest } from '@metamask/controller-utils'; | ||
| 
     | 
||
| import { traceFallback } from './traces'; | ||
| import { TraceName } from '../constants/traces'; | ||
| 
     | 
||
| describe('MultichainAccountService - Traces', () => { | ||
| describe('TraceName', () => { | ||
| it('contains expected trace names', () => { | ||
| expect(TraceName).toStrictEqual({ | ||
| SnapDiscoverAccounts: 'Snap Discover Accounts', | ||
| EvmDiscoverAccounts: 'EVM Discover Accounts', | ||
| }); | ||
| }); | ||
| }); | ||
| 
     | 
||
| describe('traceFallback', () => { | ||
| let mockTraceRequest: TraceRequest; | ||
| 
     | 
||
| beforeEach(() => { | ||
| mockTraceRequest = { | ||
| name: TraceName.SnapDiscoverAccounts, | ||
| id: 'trace-id-123', | ||
| tags: {}, | ||
| }; | ||
| }); | ||
| 
     | 
||
| it('returns undefined when no function is provided', async () => { | ||
| const result = await traceFallback(mockTraceRequest); | ||
| 
     | 
||
| expect(result).toBeUndefined(); | ||
| }); | ||
| 
     | 
||
| it('executes the provided function and return its result', async () => { | ||
| const mockResult = 'test-result'; | ||
| const mockFn = jest.fn().mockReturnValue(mockResult); | ||
| 
     | 
||
| const result = await traceFallback(mockTraceRequest, mockFn); | ||
| 
     | 
||
| expect(mockFn).toHaveBeenCalledTimes(1); | ||
| expect(mockFn).toHaveBeenCalledWith(); | ||
| expect(result).toBe(mockResult); | ||
| }); | ||
| 
     | 
||
| it('executes async function and return its result', async () => { | ||
| const mockResult = { data: 'async-result' }; | ||
| const mockAsyncFn = jest.fn().mockResolvedValue(mockResult); | ||
| 
     | 
||
| const result = await traceFallback(mockTraceRequest, mockAsyncFn); | ||
| 
     | 
||
| expect(mockAsyncFn).toHaveBeenCalledTimes(1); | ||
| expect(result).toBe(mockResult); | ||
| }); | ||
| 
     | 
||
| it('handles function that throws an error', async () => { | ||
| const mockError = new Error('Test error'); | ||
| const mockFn = jest.fn().mockImplementation(() => { | ||
| throw mockError; | ||
| }); | ||
| 
     | 
||
| await expect(traceFallback(mockTraceRequest, mockFn)).rejects.toThrow( | ||
| mockError, | ||
| ); | ||
| expect(mockFn).toHaveBeenCalledTimes(1); | ||
| }); | ||
| 
     | 
||
| it('handles function that returns a rejected promise', async () => { | ||
| const mockError = new Error('Async error'); | ||
| const mockFn = jest.fn().mockRejectedValue(mockError); | ||
| 
     | 
||
| await expect(traceFallback(mockTraceRequest, mockFn)).rejects.toThrow( | ||
| mockError, | ||
| ); | ||
| expect(mockFn).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); | 
        
          
  
    
      
          
            25 changes: 25 additions & 0 deletions
          
          25 
        
  packages/multichain-account-service/src/analytics/traces.ts
  
  
      
      
   
        
      
      
    
  
    
      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,25 @@ | ||
| import type { | ||
| TraceCallback, | ||
| TraceContext, | ||
| TraceRequest, | ||
| } from '@metamask/controller-utils'; | ||
| 
     | 
||
| /** | ||
| * Fallback function for tracing. | ||
| * This function is used when no specific trace function is provided. | ||
| * It executes the provided function in a trace context if available. | ||
| * | ||
| * @param _request - The trace request containing additional data and context. | ||
| * @param fn - The function to execute within the trace context. | ||
| * @returns A promise that resolves to the result of the executed function. | ||
| * If no function is provided, it resolves to undefined. | ||
| */ | ||
| export const traceFallback: TraceCallback = async <ReturnType>( | ||
| _request: TraceRequest, | ||
| fn?: (context?: TraceContext) => ReturnType, | ||
| ): Promise<ReturnType> => { | ||
| if (!fn) { | ||
| return undefined as ReturnType; | ||
| } | ||
| return await Promise.resolve(fn()); | ||
| }; | 
  
    
      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,4 @@ | ||
| export enum TraceName { | ||
| 'SnapDiscoverAccounts' = 'Snap Discover Accounts', | ||
                
      
                  montelaidev marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
        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. I still think   | 
||
| 'EvmDiscoverAccounts' = 'EVM Discover Accounts', | ||
| } | ||
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Unbound Trace Callbacks Break Custom Contexts
The
tracecallback is unnecessarily bound to theMultichainAccountServiceinstance when passed to account providers. This binding can break custom trace functions that rely on their originalthiscontext, as trace callbacks typically don't expect a specificthis.