- 
                Notifications
    
You must be signed in to change notification settings  - Fork 568
 
feat(integrations): add ability to auto-deactivate lower-level integrations based on map #5052
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
      
      
            shellmayr
  wants to merge
  4
  commits into
  master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
shellmayr/feat/auto-deactivate-lower-level-integartions
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
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.
          
          
      
        
          +259
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| import pytest | ||
| 
     | 
||
| from sentry_sdk import get_client | ||
| from sentry_sdk.integrations import _INTEGRATION_DEACTIVATES | ||
| 
     | 
||
| 
     | 
||
| try: | ||
| from sentry_sdk.integrations.langchain import LangchainIntegration | ||
| 
     | 
||
| has_langchain = True | ||
| except Exception: | ||
| has_langchain = False | ||
| 
     | 
||
| try: | ||
| from sentry_sdk.integrations.openai import OpenAIIntegration | ||
| 
     | 
||
| has_openai = True | ||
| except Exception: | ||
| has_openai = False | ||
| 
     | 
||
| try: | ||
| from sentry_sdk.integrations.anthropic import AnthropicIntegration | ||
| 
     | 
||
| has_anthropic = True | ||
| except Exception: | ||
| has_anthropic = False | ||
| 
     | 
||
| 
     | 
||
| pytestmark = pytest.mark.skipif( | ||
| not (has_langchain and has_openai and has_anthropic), | ||
| reason="Requires langchain, openai, and anthropic packages to be installed", | ||
| ) | ||
| 
         
      Comment on lines
    
      +29
     to 
      +32
    
   
  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. The tests are skipped in CI at the moment. They should run in CI. 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. See #5061  | 
||
| 
     | 
||
| 
     | 
||
| def test_integration_deactivates_map_exists(): | ||
| assert "langchain" in _INTEGRATION_DEACTIVATES | ||
| assert "openai" in _INTEGRATION_DEACTIVATES["langchain"] | ||
| assert "anthropic" in _INTEGRATION_DEACTIVATES["langchain"] | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_langchain_auto_deactivates_openai_and_anthropic( | ||
| sentry_init, reset_integrations | ||
| ): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| if LangchainIntegration in integration_types: | ||
| assert OpenAIIntegration not in integration_types | ||
| assert AnthropicIntegration not in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_user_can_override_with_explicit_openai(sentry_init, reset_integrations): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| integrations=[OpenAIIntegration()], | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| assert OpenAIIntegration in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_user_can_override_with_explicit_anthropic(sentry_init, reset_integrations): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| integrations=[AnthropicIntegration()], | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| assert AnthropicIntegration in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_user_can_override_with_both_explicit_integrations( | ||
| sentry_init, reset_integrations | ||
| ): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| integrations=[OpenAIIntegration(), AnthropicIntegration()], | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| assert OpenAIIntegration in integration_types | ||
| assert AnthropicIntegration in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_disabling_langchain_allows_openai_and_anthropic( | ||
| sentry_init, reset_integrations | ||
| ): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| disabled_integrations=[LangchainIntegration], | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| assert LangchainIntegration not in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_explicit_langchain_still_deactivates_others(sentry_init, reset_integrations): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=False, | ||
| integrations=[LangchainIntegration()], | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| if LangchainIntegration in integration_types: | ||
| assert OpenAIIntegration not in integration_types | ||
| assert AnthropicIntegration not in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_langchain_and_openai_both_explicit_both_active( | ||
| sentry_init, reset_integrations | ||
| ): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=False, | ||
| integrations=[LangchainIntegration(), OpenAIIntegration()], | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| assert LangchainIntegration in integration_types | ||
| assert OpenAIIntegration in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_no_langchain_means_openai_and_anthropic_can_auto_enable( | ||
| sentry_init, reset_integrations, monkeypatch | ||
| ): | ||
| import sys | ||
| import sentry_sdk.integrations | ||
| 
     | 
||
| old_iter = sentry_sdk.integrations.iter_default_integrations | ||
| 
     | 
||
| def filtered_iter(with_auto_enabling): | ||
| for cls in old_iter(with_auto_enabling): | ||
| if cls.identifier != "langchain": | ||
| yield cls | ||
| 
     | 
||
| monkeypatch.setattr( | ||
| sentry_sdk.integrations, "iter_default_integrations", filtered_iter | ||
| ) | ||
| 
     | 
||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| assert LangchainIntegration not in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_deactivation_with_default_integrations_enabled( | ||
| sentry_init, reset_integrations | ||
| ): | ||
| sentry_init( | ||
| default_integrations=True, | ||
| auto_enabling_integrations=True, | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| if LangchainIntegration in integration_types: | ||
| assert OpenAIIntegration not in integration_types | ||
| assert AnthropicIntegration not in integration_types | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.forked | ||
| def test_only_auto_enabling_integrations_without_defaults( | ||
| sentry_init, reset_integrations | ||
| ): | ||
| sentry_init( | ||
| default_integrations=False, | ||
| auto_enabling_integrations=True, | ||
| ) | ||
| 
     | 
||
| client = get_client() | ||
| integration_types = { | ||
| type(integration) for integration in client.integrations.values() | ||
| } | ||
| 
     | 
||
| if LangchainIntegration in integration_types: | ||
| assert OpenAIIntegration not in integration_types | ||
| assert AnthropicIntegration not in integration_types | ||
      
      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.