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

Update/Replace go packages #333

Merged
merged 4 commits into from
Feb 24, 2021
Merged

Update/Replace go packages #333

merged 4 commits into from
Feb 24, 2021

Conversation

ashu8912
Copy link
Contributor

fixes #97

Copy link
Member

@krnowak krnowak left a 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 if AnErr 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 or Msgf 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 with zerolog.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")

@@ -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)
Copy link
Member

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.


"github.com/kinvolk/nebraska/cmd/nebraska/ginhelpers"
)

var (
logger = log.New("auth")
logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr})
Copy link
Member

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")?

@@ -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)
Copy link
Member

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 …").

@@ -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")
Copy link
Member

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.

@@ -57,7 +57,7 @@ var (

func main() {
if err := mainWithError(); err != nil {
logger.Error(err.Error())
logger.Error().Err(err).Msg("")
Copy link
Member

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("").

@@ -223,12 +226,12 @@ func (h *Handler) prepareUpdateCheck(appResp *omahaSpec.AppResponse, pkg *api.Pa
}

func trace(v interface{}) {
if logger.IsDebug() {
if gin.IsDebugging() {
Copy link
Member

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

@@ -0,0 +1,20 @@
#!/bin/env bash
Copy link
Member

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?

Copy link
Collaborator

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.

Copy link
Collaborator

@joaquimrocha joaquimrocha left a 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).

@@ -0,0 +1,20 @@
#!/bin/env bash
Copy link
Collaborator

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.

@ashu8912 ashu8912 force-pushed the update-go-packages branch 2 times, most recently from 4dd4484 to 63161b9 Compare February 17, 2021 18:44
Semver/v4 is compatible with go modules and thus this patch replaces
semver with semver/v4.
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.
Copy link
Member

@krnowak krnowak left a 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.

@joaquimrocha joaquimrocha merged commit ee6f943 into master Feb 24, 2021
@joaquimrocha joaquimrocha deleted the update-go-packages branch February 24, 2021 10:59
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.

Review the golang packages we use
3 participants