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

Logging #47

Merged
merged 9 commits into from
Dec 8, 2022
Merged

Logging #47

merged 9 commits into from
Dec 8, 2022

Conversation

peterjan
Copy link
Member

@peterjan peterjan commented Dec 4, 2022

This PR adds structured logging to the autopilot. I thought of using logrus but apparently that's in maintenance mode. Seems like zap is the new go-to for structured logging and it's super fast.

The idea is to use named loggers that add a "component" context to the log entries. Structured logging is definitely a bonus since we'll be able to easily parse relevant info for a single host or subsystem. I played around with the config a little bit but kept it simple for now. There's also a zaptest package that allows creating a test logger to which you can pass testing.T, which is pretty nice because you get logs only in case of failure or when you run it with -v. Removed that though for the time being.

autopilot/autopilot.go Outdated Show resolved Hide resolved
autopilot/logging.go Outdated Show resolved Hide resolved
autopilot/autopilot.go Show resolved Hide resolved
autopilot/autopilot.go Outdated Show resolved Hide resolved
autopilot/contractor.go Outdated Show resolved Hide resolved
autopilot/scanner.go Outdated Show resolved Hide resolved
@peterjan peterjan mentioned this pull request Dec 7, 2022
err = logger.Sync()
closeFn()
return
}
Copy link
Member

Choose a reason for hiding this comment

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

this is a siad/skyd pattern that I am trying to avoid replicating. 😅

The rule of thumb is: Don't call constructors within a constructor. Doing so is convenient for the caller (since they can pass one dir argument instead of a bunch of interfaces), but it has three big downsides:

  • It prevents you from using interfaces. In this case, we are stuck with a logger that writes to a file, even during testing.
  • It forces the constructed object to be responsible for cleaning up its dependencies. (Should an autopilot really be responsible for closing a logger?)
  • It prevents you from sharing a dependency. What if we want all of our subsystems to share a logger?

There are exceptions, of course. But to me, this is a pretty clear example of where we should be passing a Logger interface instead of calling a constructor within a constructor.

I think the constructor-within-a-constructor pattern derives from a more general design philosophy of building systems out of "actors," by which I mean "black boxes that have a lot of state and make a lot of decisions." I'm moving away from that philosophy, and towards systems built from "dumb" components, which have less state and make fewer decisions. Essentially, the goal is to centralize the complexity of the system by moving it out of the individual components.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense 👍 - couldn't pass an interface because the guys at Uber decided to remove the interface and make it a struct. Should be fine though, it actually cleaned up the cleanup function, thanks!

@peterjan peterjan merged commit 42c4c10 into master Dec 8, 2022
@peterjan peterjan deleted the pj/logging branch December 8, 2022 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants