1010from dotenv import load_dotenv
1111from fcache .cache import FileCache # type: ignore
1212from sentry_sdk .integrations .aiohttp import AioHttpIntegration
13+ from sentry_sdk .integrations .logging import LoggingIntegration
1314
1415from dipdup import __spec_version__ , __version__ , spec_reindex_mapping , spec_version_mapping
1516from dipdup .codegen import DEFAULT_DOCKER_ENV_FILE , DEFAULT_DOCKER_IMAGE , DEFAULT_DOCKER_TAG , DipDupCodeGenerator
@@ -29,6 +30,29 @@ class CLIContext:
2930 logging_config : LoggingConfig
3031
3132
33+ def init_sentry (config : DipDupConfig ) -> None :
34+ if not config .sentry :
35+ return
36+ if config .sentry .debug :
37+ level , event_level = logging .DEBUG , logging .WARNING
38+ else :
39+ level , event_level = logging .INFO , logging .ERROR
40+
41+ integrations = [
42+ AioHttpIntegration (),
43+ LoggingIntegration (
44+ level = level ,
45+ event_level = event_level ,
46+ ),
47+ ]
48+ sentry_sdk .init (
49+ dsn = config .sentry .dsn ,
50+ environment = config .sentry .environment ,
51+ integrations = integrations ,
52+ release = __version__ ,
53+ )
54+
55+
3256@click .group ()
3357@click .version_option (__version__ )
3458@click .option ('--config' , '-c' , type = str , multiple = True , help = 'Path to dipdup YAML config' , default = ['dipdup.yml' ])
@@ -44,6 +68,7 @@ async def cli(ctx, config: List[str], env_file: List[str], logging_config: str):
4468 _logging_config = LoggingConfig .load (path )
4569 _logging_config .apply ()
4670
71+ # NOTE: Apply env files before loading config
4772 for env_path in env_file :
4873 env_path = join (os .getcwd (), env_path )
4974 if not exists (env_path ):
@@ -52,19 +77,14 @@ async def cli(ctx, config: List[str], env_file: List[str], logging_config: str):
5277 load_dotenv (env_path , override = True )
5378
5479 _config = DipDupConfig .load (config )
80+ init_sentry (_config )
81+
5582 if _config .spec_version not in spec_version_mapping :
5683 raise ConfigurationError ('Unknown `spec_version`, correct ones: {}' )
5784 if _config .spec_version != __spec_version__ and ctx .invoked_subcommand != 'migrate' :
5885 reindex = spec_reindex_mapping [__spec_version__ ]
5986 raise MigrationRequiredError (None , _config .spec_version , __spec_version__ , reindex )
6087
61- if _config .sentry :
62- sentry_sdk .init (
63- dsn = _config .sentry .dsn ,
64- environment = _config .sentry .environment ,
65- integrations = [AioHttpIntegration ()],
66- )
67-
6888 ctx .obj = CLIContext (
6989 config_paths = config ,
7090 config = _config ,
0 commit comments