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

Struct log for adapter call sites #4189

Merged
merged 16 commits into from
Nov 8, 2021

Conversation

iknox-fa
Copy link
Contributor

@iknox-fa iknox-fa commented Nov 2, 2021

Updated logging call sites for core/dbt/adapters

Nathaniel May and others added 7 commits October 26, 2021 13:57
add event type modeling and fire_event calls
* updated context dir to new structured logging
handle exec info
update log call sites with new event system
graph call sites for structured logging
@cla-bot cla-bot bot added the cla:yes label Nov 2, 2021
@iknox-fa iknox-fa changed the title WIP call sites for adapters Struct log for adapter call sites Nov 4, 2021
@iknox-fa iknox-fa marked this pull request as ready for review November 4, 2021 13:57
Copy link
Contributor

@nathaniel-may nathaniel-may left a comment

Choose a reason for hiding this comment

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

We definitely need to address the lazy log situation which I completely didn't understand until now. Of course the obvious solution runs us into a mypy bug so this will be fun to handle. I'll put up a PR to make message evaluation lazy in fire_event, so our solution here can assume that's already in.

lazy_log('before adding: {!s}', self.dump_graph)
fire_event(AddRelation(relation=cached))
# TODO: conditionally fire this event (logger.disabled, if it was std python logger)
fire_event(DumpBeforeAddGraph(graph=self.dump_graph()))
Copy link
Contributor

Choose a reason for hiding this comment

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

OH I see what lazy_log is doing here. I think it's guarding against log levels. If we're on info level, but this is debug level, because of strict evaluation, the long running dump graph will get called even though the message will never be displayed. The way the event module works right now, we force the message early so lazy logging won't help. I think this might be worth solving inside fire_event so we as developers don't have to decide whether the construction of a message is long running or not, it only constructs each message as they are needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

that being said... we're going to have the same problem with creating the event type. That will evaluate self.dump_graph() as part of __init__. We should change the event types to take a function value instead, and apply the empty arguments at message creation time. Let's cache it in the event type so it doesn't have to be called again.

We're basically threading laziness farther down the pipeline.

@@ -0,0 +1,44 @@
from typing import (
Copy link
Contributor

Choose a reason for hiding this comment

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

is the plan for this file to grow a lot for now and then we can delete it when we turn on mypy everywhere?

Comment on lines 541 to 545
class DumpBeforeAddGraph(DebugLevel, CliEventABC):
graph: Dict[str, List[str]]

def cli_msg(self) -> str:
return f"before adding: {str(self.graph)}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Here's the kind of thing I think we should be doing:

from dataclasses import dataclass
from typing import Callable, Dict, List


def dump_graph() -> Dict[str, List[str]]:
    raise Exception('stub')

@dataclass
class Foo():
    graph_thunk:  Callable[[], Dict[str, List[str]]]

    def bar(self) -> str:
        # this runs into a mypy bug: https://github.com/python/mypy/issues/6910
        # I'll leave it up to you how you want to get around it.
        return f"some msg with the graph:\n{self.graph_thunk()}"

Foo(graph_thunk=dump_graph)

Comment on lines +74 to +79
# N.B: a temporary string replacement regex to strip the HH:MM:SS from the log line if present.
# TODO: make this go away when structured logging is stable
log_msg = re.sub("(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d \| )", "", log['message'])
prefix = 'On {}: '.format(model_name)

if log['message'].startswith(prefix):
msg = log['message'][len(prefix):]
if log_msg.startswith(prefix):
msg = log_msg[len(prefix):]
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this addition about?

@nathaniel-may nathaniel-may mentioned this pull request Nov 4, 2021
4 tasks
@nathaniel-may
Copy link
Contributor

Please see #4210 for the preserved laziness in fire_event.

Copy link
Contributor

@nathaniel-may nathaniel-may left a comment

Choose a reason for hiding this comment

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

Other than this straggler I think we're good. Mypy should have caught it though...


with self.lock:
self._setdefault(cached)

lazy_log('after adding: {!s}', self.dump_graph)
fire_event(DumpAfterAddGraph(graph_func=self.dump_graph()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fire_event(DumpAfterAddGraph(graph_func=self.dump_graph()))
fire_event(DumpAfterAddGraph(graph_func=self.dump_graph))

@nathaniel-may nathaniel-may self-requested a review November 8, 2021 16:02
@emmyoop emmyoop force-pushed the feature/structured-logging branch from 89e478f to 66cece0 Compare November 8, 2021 20:32
@nathaniel-may nathaniel-may merged commit cacb97c into feature/structured-logging Nov 8, 2021
@nathaniel-may nathaniel-may deleted the call_sites_adapters branch November 8, 2021 23:31
kwigley pushed a commit that referenced this pull request Nov 9, 2021
graph call sites for structured logging

Co-authored-by: Nathaniel May <nathaniel.may@fishtownanalytics.com>
Co-authored-by: Emily Rockman <emily.rockman@dbtlabs.com>
nathaniel-may pushed a commit that referenced this pull request Nov 9, 2021
graph call sites for structured logging

Co-authored-by: Nathaniel May <nathaniel.may@fishtownanalytics.com>
Co-authored-by: Emily Rockman <emily.rockman@dbtlabs.com>
iknox-fa added a commit that referenced this pull request Feb 8, 2022
graph call sites for structured logging

Co-authored-by: Nathaniel May <nathaniel.may@fishtownanalytics.com>
Co-authored-by: Emily Rockman <emily.rockman@dbtlabs.com>

automatic commit by git-black, original commits:
  b2aea11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants