Coverage for src/backend/InvenTree/InvenTree/setting/tracing.py: 57%

17 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Start-up configuration options for InvenTree tracing integrations.""" 

2 

3import structlog 

4 

5from InvenTree.config import get_boolean_setting, get_setting 

6from InvenTree.tracing import setup_instruments, setup_tracing 

7 

8logger = structlog.get_logger('inventree') 

9 

10 

11def configure_tracing(db_engine: str, enabled: bool, tags: dict) -> dict: 

12 """Configure tracing integrations for InvenTree. 

13 

14 Arguments: 

15 db_engine: The database engine being used 

16 enabled: Whether tracing is enabled 

17 tags: A dictionary of tags to be included in tracing spans, with keys prefixed by 

18 """ 

19 endpoint = get_setting('INVENTREE_TRACING_ENDPOINT', 'tracing.endpoint', None) 

20 headers = ( 

21 get_setting('INVENTREE_TRACING_HEADERS', 'tracing.headers', None, typecast=dict) 

22 or {} 

23 ) 

24 

25 if enabled and endpoint: 25 ↛ 26line 25 didn't jump to line 26 because the condition on line 25 was never true

26 logger.info('Tracing enabled with endpoint: %s', endpoint) 

27 

28 TRACING_DETAILS = { 

29 'endpoint': endpoint, 

30 'headers': headers, 

31 'resources_input': { 

32 **{'inventree.env.' + k: v for k, v in tags.items()}, 

33 **get_setting( 

34 'INVENTREE_TRACING_RESOURCES', 

35 'tracing.resources', 

36 default_value=None, 

37 typecast=dict, 

38 ), 

39 }, 

40 'console': get_boolean_setting( 

41 'INVENTREE_TRACING_CONSOLE', 'tracing.console', False 

42 ), 

43 'auth': get_setting( 

44 'INVENTREE_TRACING_AUTH', 'tracing.auth', default_value=None, typecast=dict 

45 ), 

46 'is_http': get_setting('INVENTREE_TRACING_IS_HTTP', 'tracing.is_http', True), 

47 'append_http': get_boolean_setting( 

48 'INVENTREE_TRACING_APPEND_HTTP', 'tracing.append_http', True 

49 ), 

50 } 

51 

52 if not enabled: 52 ↛ 55line 52 didn't jump to line 55 because the condition on line 52 was always true

53 return None 

54 

55 if endpoint: 

56 setup_tracing(**TRACING_DETAILS) 

57 setup_instruments(db_engine) 

58 else: 

59 logger.warning('OpenTelemetry tracing not enabled because endpoint is not set') 

60 

61 return TRACING_DETAILS