1313from sentry import analytics
1414from sentry .auth .exceptions import IdentityNotValid
1515from sentry .integrations .models .repository_project_path_config import RepositoryProjectPathConfig
16+ from sentry .integrations .source_code_management .metrics import (
17+ SCMIntegrationInteractionEvent ,
18+ SCMIntegrationInteractionType ,
19+ )
1620from sentry .locks import locks
1721from sentry .models .commit import Commit
1822from sentry .models .group import Group
2630 PullRequestCommit ,
2731)
2832from sentry .models .repository import Repository
33+ from sentry .shared_integrations .exceptions import ApiRateLimitedError
2934from sentry .users .models .identity import Identity
3035from sentry .utils import metrics
3136from sentry .utils .cache import cache
@@ -86,6 +91,12 @@ def integration_name(self) -> str:
8691 def get_client (self ) -> CommitContextClient :
8792 raise NotImplementedError
8893
94+ def record_event (self , event : SCMIntegrationInteractionType ):
95+ return SCMIntegrationInteractionEvent (
96+ interaction_type = event ,
97+ provider_key = self .integration_name ,
98+ )
99+
89100 def get_blame_for_files (
90101 self , files : Sequence [SourceLineInfo ], extra : Mapping [str , Any ]
91102 ) -> list [FileBlameInfo ]:
@@ -94,16 +105,25 @@ def get_blame_for_files(
94105
95106 files: list of FileBlameInfo objects
96107 """
97- try :
98- client = self .get_client ()
99- except Identity .DoesNotExist :
100- return []
101- try :
102- response = client .get_blame_for_files (files , extra )
103- except IdentityNotValid :
104- return []
105-
106- return response
108+ with self .record_event (
109+ SCMIntegrationInteractionType .GET_BLAME_FOR_FILES
110+ ).capture () as lifecycle :
111+ try :
112+ client = self .get_client ()
113+ except Identity .DoesNotExist :
114+ sentry_sdk .capture_exception ()
115+ return []
116+ try :
117+ response = client .get_blame_for_files (files , extra )
118+ except IdentityNotValid :
119+ sentry_sdk .capture_exception ()
120+ return []
121+ # Swallow rate limited errors so we don't log them as exceptions
122+ except ApiRateLimitedError as e :
123+ sentry_sdk .capture_exception (e )
124+ lifecycle .record_halt (e )
125+ return []
126+ return response
107127
108128 def get_commit_context_all_frames (
109129 self , files : Sequence [SourceLineInfo ], extra : Mapping [str , Any ]
0 commit comments