Skip to content

Commit

Permalink
use event types in main even before the logger is set up. (#4219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel May committed Nov 9, 2021
1 parent 25c974a commit f95e9ef
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
52 changes: 52 additions & 0 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABCMeta, abstractmethod
import argparse
from dataclasses import dataclass
from typing import Any, List, Optional, Dict
from dbt import ui
Expand Down Expand Up @@ -68,6 +69,51 @@ def cli_msg(self) -> str:
raise Exception("cli_msg not implemented for cli event")


class MainKeyboardInterrupt(InfoLevel, CliEventABC):
def cli_msg(self) -> str:
return "ctrl-c"


@dataclass
class MainEncounteredError(ErrorLevel, CliEventABC):
e: BaseException

def cli_msg(self) -> str:
return f"Encountered an error:\n{str(self.e)}"


@dataclass
class MainStackTrace(DebugLevel, CliEventABC):
stack_trace: str

def cli_msg(self) -> str:
return self.stack_trace


@dataclass
class MainReportVersion(InfoLevel, CliEventABC):
v: str # could be VersionSpecifier instead if we resolved some circular imports

def cli_msg(self):
return f"Running with dbt{self.v}"


@dataclass
class MainReportArgs(DebugLevel, CliEventABC):
args: argparse.Namespace

def cli_msg(self):
return f"running dbt with arguments {str(self.args)}"


@dataclass
class MainTrackingUserState(DebugLevel, CliEventABC):
user_state: str

def cli_msg(self):
return f"Tracking: {self.user_state}"


class ParsingStart(InfoLevel, CliEventABC):
def cli_msg(self) -> str:
return "Start parsing."
Expand Down Expand Up @@ -1728,6 +1774,12 @@ def cli_msg(self) -> str:
#
# TODO remove these lines once we run mypy everywhere.
if 1 == 0:
MainReportVersion('')
MainKeyboardInterrupt()
MainEncounteredError(BaseException(''))
MainStackTrace('')
MainReportVersion('')
MainTrackingUserState('')
ParsingStart()
ParsingCompiling()
ParsingWritingManifest()
Expand Down
32 changes: 14 additions & 18 deletions core/dbt/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List
from dbt.logger import GLOBAL_LOGGER as logger, log_cache_events, log_manager
from dbt.logger import log_cache_events, log_manager

import argparse
import os.path
Expand All @@ -9,6 +9,11 @@
from pathlib import Path

import dbt.version
from dbt.events.functions import fire_event
from dbt.events.types import (
MainEncounteredError, MainKeyboardInterrupt, MainReportVersion, MainReportArgs,
MainTrackingUserState, MainStackTrace
)
import dbt.flags as flags
import dbt.task.build as build_task
import dbt.task.clean as clean_task
Expand All @@ -34,7 +39,6 @@
from dbt.utils import ExitCodes
from dbt.config.profile import DEFAULT_PROFILES_DIR, read_user_config
from dbt.exceptions import (
RuntimeException,
InternalException,
NotImplementedException,
FailedToConnectException
Expand Down Expand Up @@ -127,24 +131,17 @@ def main(args=None):
exit_code = ExitCodes.ModelError.value

except KeyboardInterrupt:
logger.info("ctrl-c")
# if the logger isn't configured yet, it will use the default logger
fire_event(MainKeyboardInterrupt())
exit_code = ExitCodes.UnhandledError.value

# This can be thrown by eg. argparse
except SystemExit as e:
exit_code = e.code

except BaseException as e:
logger.warning("Encountered an error:")
logger.warning(str(e))

if log_manager.initialized:
logger.debug(traceback.format_exc())
elif not isinstance(e, RuntimeException):
# if it did not come from dbt proper and the logger is not
# initialized (so there's no safe path to log to), log the
# stack trace at error level.
logger.error(traceback.format_exc())
fire_event(MainEncounteredError(e=e))
fire_event(MainStackTrace(stack_trace=traceback.format_exc()))
exit_code = ExitCodes.UnhandledError.value

sys.exit(exit_code)
Expand Down Expand Up @@ -208,7 +205,7 @@ def track_run(task):
)
except (NotImplementedException,
FailedToConnectException) as e:
logger.error('ERROR: {}'.format(e))
fire_event(MainEncounteredError(e=e))
dbt.tracking.track_invocation_end(
config=task.config, args=task.args, result_type="error"
)
Expand All @@ -228,21 +225,20 @@ def run_from_args(parsed):
# set log_format in the logger
parsed.cls.pre_init_hook(parsed)

logger.info("Running with dbt{}".format(dbt.version.installed))
fire_event(MainReportVersion(v=dbt.version.installed))

# this will convert DbtConfigErrors into RuntimeExceptions
# task could be any one of the task objects
task = parsed.cls.from_args(args=parsed)

logger.debug("running dbt with arguments {parsed}", parsed=str(parsed))
fire_event(MainReportArgs(args=parsed))

log_path = None
if task.config is not None:
log_path = getattr(task.config, 'log_path', None)
# we can finally set the file logger up
log_manager.set_path(log_path)
if dbt.tracking.active_user is not None: # mypy appeasement, always true
logger.debug("Tracking: {}".format(dbt.tracking.active_user.state()))
fire_event(MainTrackingUserState(dbt.tracking.active_user.state()))

results = None

Expand Down

0 comments on commit f95e9ef

Please sign in to comment.