Coverage for gwcelery/sentry/integrations/subprocess.py: 32%

22 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-11-14 05:52 +0000

1from subprocess import CalledProcessError 

2 

3from sentry_sdk.integrations import Integration 

4from sentry_sdk.scope import add_global_event_processor 

5 

6 

7class SubprocessIntegration(Integration): 

8 """Capture stderr and stdout from CalledProcessError exceptions.""" 

9 

10 identifier = 'subprocess' 

11 

12 @staticmethod 

13 def setup_once(): 

14 

15 @add_global_event_processor 

16 def capture(event, hint): 

17 if 'exc_info' not in hint: 

18 return event 

19 

20 _, e, _ = hint['exc_info'] 

21 if not isinstance(e, CalledProcessError): 

22 return event 

23 

24 breadcrumbs = event.get('breadcrumbs') 

25 if not breadcrumbs: 

26 return event 

27 

28 data = breadcrumbs[-1].setdefault('data', {}) 

29 for key in ['stderr', 'stdout']: 

30 value = getattr(e, key) 

31 data[key] = value.decode(errors='replace') 

32 

33 return event