-
Notifications
You must be signed in to change notification settings - Fork 43
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
Update/Replace go packages #333
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh man, logger library change is painful. Thanks for handling it.
So I haven't gone through every single change here, as there are many. But one thing sticks out - our logging is a mess. Both old logxi
and new zerolog
are structured loggers, but logxi
was rather weak at enforcing it, while zerolog
is more stringent in this regard. So with logxi
the usual way of passing parameter to logger was logger.Debug(message, key1, value1, key2, value2, …)
, and we seem to often abuse it in way like logger.Error("stuff failed", err)
, where err becomes key1
. With zerolog
, the way of logging looks more like logger.Error().Str(key1, value1).Str(key2, value2).Msg(message)
. Or logger.Error().Err(err).Msg("stuff failed")
. The problem I see here is that in this PR we put all the key-value pairs into the message by turning logger.Debug("message", key1, value1, key2, value2, …)
into logger.Debug().Msgf("message %s %s %s %s …", key1, value1, key2, value2, …)
. I'd prefer to keep the key-value and message separation.
So to summarize:
- Please use
Err
for passing errors (which you mostly already do). Not sure ifAnErr
comes in handy anywhere. - Please use
Str
(or some other type-safe function from https://pkg.go.dev/github.com/rs/zerolog#Event) for key-value pairs. - Please come up with some guideline for messages (the
Msg
orMsgf
part). My proposal would be that they are written like "operation - message", so for example "fetching manifest - something bad happened" or "login cb - invalid oauth state". - The loggers were named (
logger = log.New("auth")
), I suppose this can be useful to preserve (like withzerolog.New(zerolog.ConsoleWriter{…}).With().Str("context", "auth")
). - Please fix some of the logging mess/abuse we have.
Some examples:
logger.Debug("auth metrics", "malformed authorization header", authHeader)
could become logger.Debug().Str("header", authHeader").Msg("auth metrics - malformed authorization header")
logger.Error("login cb", "expected to have a valid desiredurl item in session data")
could become logger.Error().Msg("login cb - expected to have a valid desiredurl item in session data")
logger.Error("addApp - cloning app", "error", err.Error(), "app", app, "sourceAppID", sourceAppID)
could become logger.Error().Err(err).Str("app", app).Str("sourceAppID", sourceAppID).Msg("addApp - cloning app failed")
cmd/nebraska/auth/githubauth.go
Outdated
@@ -154,9 +154,9 @@ func (gha *githubAuth) Authenticate(c *gin.Context) (teamID string, replied bool | |||
session.Set("state", oauthState) | |||
session.Set("desiredurl", c.Request.URL.String()) | |||
sessionSave(c, session, "authMissingTeamID") | |||
logger.Debug("authenticate", "oauthstate", oauthState) | |||
logger.Debug().Msgf("authenticate oauthstate %s", oauthState) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way logxi worked was logger.Debug(message, key1, value1, key2, value2, …)
. So in order to have the same with zerolog, something like logger.Debug().Str(key1, value1).Str(key2, value2).Msg(message)
. So I think that in this case, it should be something like logger.Debug().Str("oauthstate", oauthstate).Msg("authenticate")
. Note that the Msg
must come last.
cmd/nebraska/auth/auth.go
Outdated
|
||
"github.com/kinvolk/nebraska/cmd/nebraska/ginhelpers" | ||
) | ||
|
||
var ( | ||
logger = log.New("auth") | ||
logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about zerolog.New(zerolog.ConsoleWriter{…}).With().Str("context", "auth")
?
cmd/nebraska/metrics.go
Outdated
@@ -62,7 +62,7 @@ func getMetricsRefreshInterval() time.Duration { | |||
|
|||
refreshInterval, err := time.ParseDuration(refreshIntervalEnvValue) | |||
if err != nil || refreshInterval <= 0 { | |||
logger.Warn("invalid NEBRASKA_METRICS_UPDATE_INTERVAL value, it must be acceptable by time.ParseDuration and positive", "value", refreshIntervalEnvValue) | |||
logger.Warn().Msgf("invalid NEBRASKA_METRICS_UPDATE_INTERVAL value %s, it must be acceptable by time.ParseDuration and positive value", refreshIntervalEnvValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd make it logger.Warn().Str("value", refreshIntervalEnvValue).Msg("invalid …")
.
cmd/nebraska/metrics.go
Outdated
@@ -85,7 +85,7 @@ func registerAndInstrumentMetrics(ctl *controller) error { | |||
<-metricsTicker | |||
err := calculateMetrics(ctl) | |||
if err != nil { | |||
logger.Error("registerAndInstrumentMetrics updating the metrics", "error", err.Error()) | |||
logger.Error().Msgf("registerAndInstrumentMetrics updating the metrics") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing Err(err)
. Also there's no formatting to do, so simple Msg
should suffice.
cmd/nebraska/nebraska.go
Outdated
@@ -57,7 +57,7 @@ var ( | |||
|
|||
func main() { | |||
if err := mainWithError(); err != nil { | |||
logger.Error(err.Error()) | |||
logger.Error().Err(err).Msg("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have something like an empty message, then maybe consider using Send()
instead of Msg("")
.
pkg/omaha/omaha.go
Outdated
@@ -223,12 +226,12 @@ func (h *Handler) prepareUpdateCheck(appResp *omahaSpec.AppResponse, pkg *api.Pa | |||
} | |||
|
|||
func trace(v interface{}) { | |||
if logger.IsDebug() { | |||
if gin.IsDebugging() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could zerolog.GlobalLevel()
be used here instead? https://pkg.go.dev/github.com/rs/zerolog#GlobalLevel
update_nebraska_db_ts.sh
Outdated
@@ -0,0 +1,20 @@ | |||
#!/bin/env bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this script for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was probably mistakenly added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As Krzesimir had noticed, there's a debug script that has been mistakenly added to the PR.
The choice of Zerolog means changing the logging calls in a lot of places. I am curious to know why it was used over Logrus (it's in no-new-features mode but seems to be actively maintained, and is compatible with the log Go API).
update_nebraska_db_ts.sh
Outdated
@@ -0,0 +1,20 @@ | |||
#!/bin/env bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was probably mistakenly added.
4dd4484
to
63161b9
Compare
Semver/v4 is compatible with go modules and thus this patch replaces semver with semver/v4.
63161b9
to
9db8d55
Compare
Logxi is not into active development and thus we need to replace it with a better logging library which is also into active development. zerolog is fast and simple to use and meets the above criteria as well.
9db8d55
to
3f5f9d0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is one more thing to do, which is to set the default logging level to "info", and then either add a debug
flag or some environment variable to set the logging to the "debug" level.
fixes #97