- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
          feat: Add thirdPartyErrorFilterIntegration
          #12267
        
          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 all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      dbd6b4e
              
                feat: Add `thirdPartyErrorFilterIntegration`
              
              
                timfish 0632b0c
              
                lint
              
              
                timfish 6632edc
              
                Refactor
              
              
                 c83905e
              
                Sync with https://github.com/getsentry/sentry-javascript-bundler-plug…
              
              
                 08b1e6a
              
                Merge branch 'develop' into timfish/thirdPartyErrorFilterIntegration
              
              
                 dcad85b
              
                Update comment with bundler plugin option
              
              
                 cdf8546
              
                Merge branch 'develop' into timfish/thirdPartyErrorFilterIntegration
              
              
                 9ac2f65
              
                Merge branch 'develop' into timfish/thirdPartyErrorFilterIntegration
              
              
                 bae7d02
              
                Merge branch 'develop' into timfish/thirdPartyErrorFilterIntegration
              
              
                 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
    
  
  
    
              
        
          
          
            114 changes: 114 additions & 0 deletions
          
          114 
        
  packages/core/src/integrations/third-party-errors-filter.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,114 @@ | ||
| import type { Event, EventItem } from '@sentry/types'; | ||
| import { forEachEnvelopeItem, getFramesFromEvent } from '@sentry/utils'; | ||
| import { defineIntegration } from '../integration'; | ||
| import { addMetadataToStackFrames, stripMetadataFromStackFrames } from '../metadata'; | ||
|  | ||
| interface Options { | ||
| /** | ||
| * Keys that have been provided in the Sentry bundler plugin via the the `applicationKey` option, identifying your bundles. | ||
| * | ||
| * - Webpack plugin: https://www.npmjs.com/package/@sentry/webpack-plugin#applicationkey | ||
| * - Vite plugin: https://www.npmjs.com/package/@sentry/vite-plugin#applicationkey | ||
| * - Esbuild plugin: https://www.npmjs.com/package/@sentry/esbuild-plugin#applicationkey | ||
| * - Rollup plugin: https://www.npmjs.com/package/@sentry/rollup-plugin#applicationkey | ||
| */ | ||
| filterKeys: string[]; | ||
|  | ||
| /** | ||
| * Defines how the integration should behave. "Third-Party Stack Frames" are stack frames that did not come from files marked with a matching bundle key. | ||
| * | ||
| * You can define the behaviour with one of 4 modes: | ||
| * - `drop-error-if-contains-third-party-frames`: Drop error events that contain at least one third-party stack frame. | ||
| * - `drop-error-if-exclusively-contains-third-party-frames`: Drop error events that exclusively contain third-party stack frames. | ||
| * - `apply-tag-if-contains-third-party-frames`: Keep all error events, but apply a `third_party_code: true` tag in case the error contains at least one third-party stack frame. | ||
| * - `apply-tag-if-exclusively-contains-third-party-frames`: Keep all error events, but apply a `third_party_code: true` tag in case the error contains exclusively third-party stack frames. | ||
| * | ||
| * If you chose the mode to only apply tags, the tags can then be used in Sentry to filter your issue stream by entering `!third_party_code:True` in the search bar. | ||
| */ | ||
| behaviour: | ||
| | 'drop-error-if-contains-third-party-frames' | ||
| | 'drop-error-if-exclusively-contains-third-party-frames' | ||
| | 'apply-tag-if-contains-third-party-frames' | ||
| | 'apply-tag-if-exclusively-contains-third-party-frames'; | ||
| } | ||
|  | ||
| /** | ||
| * This integration allows you to filter out, or tag error events that do not come from user code marked with a bundle key via the Sentry bundler plugins. | ||
| */ | ||
| export const thirdPartyErrorFilterIntegration = defineIntegration((options: Options) => { | ||
| return { | ||
| name: 'ThirdPartyErrorsFilter', | ||
| setup(client) { | ||
| // We need to strip metadata from stack frames before sending them to Sentry since these are client side only. | ||
| // TODO(lforst): Move this cleanup logic into a more central place in the SDK. | ||
| client.on('beforeEnvelope', envelope => { | ||
| forEachEnvelopeItem(envelope, (item, type) => { | ||
| if (type === 'event') { | ||
| const event = Array.isArray(item) ? (item as EventItem)[1] : undefined; | ||
|  | ||
| if (event) { | ||
| stripMetadataFromStackFrames(event); | ||
| item[1] = event; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }, | ||
| processEvent(event, _hint, client) { | ||
| const stackParser = client.getOptions().stackParser; | ||
| addMetadataToStackFrames(stackParser, event); | ||
|  | ||
| const frameKeys = getBundleKeysForAllFramesWithFilenames(event); | ||
|  | ||
| if (frameKeys) { | ||
| const arrayMethod = | ||
| options.behaviour === 'drop-error-if-contains-third-party-frames' || | ||
| options.behaviour === 'apply-tag-if-contains-third-party-frames' | ||
| ? 'some' | ||
| : 'every'; | ||
|  | ||
| const behaviourApplies = frameKeys[arrayMethod](keys => !keys.some(key => options.filterKeys.includes(key))); | ||
|  | ||
| if (behaviourApplies) { | ||
| const shouldDrop = | ||
| options.behaviour === 'drop-error-if-contains-third-party-frames' || | ||
| options.behaviour === 'drop-error-if-exclusively-contains-third-party-frames'; | ||
| if (shouldDrop) { | ||
| return null; | ||
| } else { | ||
| event.tags = { | ||
| ...event.tags, | ||
| third_party_code: true, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|  | ||
| return event; | ||
| }, | ||
| }; | ||
| }); | ||
|  | ||
| function getBundleKeysForAllFramesWithFilenames(event: Event): string[][] | undefined { | ||
| const frames = getFramesFromEvent(event); | ||
|  | ||
| if (!frames) { | ||
| return undefined; | ||
| } | ||
|  | ||
| return ( | ||
| frames | ||
| // Exclude frames without a filename since these are likely native code or built-ins | ||
| .filter(frame => !!frame.filename) | ||
| .map(frame => { | ||
| if (frame.module_metadata) { | ||
| return Object.keys(frame.module_metadata) | ||
| .filter(key => key.startsWith(BUNDLER_PLUGIN_APP_KEY_PREFIX)) | ||
| .map(key => key.slice(BUNDLER_PLUGIN_APP_KEY_PREFIX.length)); | ||
| } | ||
| return []; | ||
| }) | ||
| ); | ||
| } | ||
|  | ||
| const BUNDLER_PLUGIN_APP_KEY_PREFIX = '_sentryBundlerPluginAppKey:'; | ||
  
    
      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.
        
    
  
  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.
These are much better names!
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.
Yup, I agree. Your comment was a wake-up call 😂