Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context call sites #4164

Merged
merged 2 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
)
from dbt.contracts.graph.compiled import CompiledResource
from dbt.exceptions import raise_compiler_error, MacroReturn
from dbt.logger import GLOBAL_LOGGER as logger
from dbt.events.functions import fire_event
from dbt.events.types import MacroEventInfo, MacroEventDebug
from dbt.version import __version__ as dbt_version

# These modules are added to the context. Consider alternative
Expand Down Expand Up @@ -443,9 +444,9 @@ def log(msg: str, info: bool = False) -> str:
{% endmacro %}"
"""
if info:
logger.info(msg)
fire_event(MacroEventInfo(msg))
else:
logger.debug(msg)
fire_event(MacroEventDebug(msg))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just popping in to say that this does get called a lot, for lots of different reasons—this is what's called any time a message is logged from within the Jinja context. That could be for materializations, wrapped by other macros in packages, to offer visibility in complex operations, or any custom user-space code.

I think MacroEvent is a fine name; log() is called as a macro, though it can be called from outside macros (models, hooks, etc). I also think it's fair that this is not going to be nearly as structured/consistent as built-in dbt logging.

Eventually, we might think about offering a more structured version of the log macro, e.g. in which users can pass a key-value dictionary and an f-string-like message. That's for later, this is good for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth a tech-debt ticket?

Copy link
Contributor

@jtcohen6 jtcohen6 Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe! I wouldn't be surprised if there's contextual info freely available in the Jinja context, which we can pass along with the user-provided message to make it that much richer. E.g. the "parent" macro calling this log() macro. But I don't actually know that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened an issue to keep thinking about this: #4184

return ''

@contextproperty
Expand Down
1 change: 0 additions & 1 deletion core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
wrapped_exports,
)
from dbt.config import IsFQNResource
from dbt.logger import GLOBAL_LOGGER as logger # noqa
from dbt.node_types import NodeType

from dbt.utils import (
Expand Down
18 changes: 18 additions & 0 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ def cli_msg(self) -> str:
return f"Performance info: {self.path}"


@dataclass
class MacroEventInfo(InfoLevel, CliEventABC):
msg: str

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


@dataclass
class MacroEventDebug(DebugLevel, CliEventABC):
msg: str

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


# since mypy doesn't run on every file we need to suggest to mypy that every
# class gets instantiated. But we don't actually want to run this code.
# making the conditional `if False` causes mypy to skip it as dead code so
Expand All @@ -122,3 +138,5 @@ def cli_msg(self) -> str:
ManifestChecked()
ManifestFlatGraphBuilt()
ReportPerformancePath(path='')
MacroEventInfo(msg='')
MacroEventDebug(msg='')