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

Add zerolog logging middleware #225

Closed
wants to merge 2 commits into from
Closed

Add zerolog logging middleware #225

wants to merge 2 commits into from

Conversation

Ahmet-Kaplan
Copy link

Inspired from adrien-f PR, I want to add Zerolog logging into that project.

For benchmarks, you can visit logbench site. As seen, Zerolog is much faster than Logrus and Zap.

@codecov-io
Copy link

codecov-io commented Aug 26, 2019

Codecov Report

Merging #225 into master will decrease coverage by 1.24%.
The diff coverage is 3.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #225      +/-   ##
==========================================
- Coverage   71.55%   70.31%   -1.25%     
==========================================
  Files          37       38       +1     
  Lines        1392     1381      -11     
==========================================
- Hits          996      971      -25     
- Misses        347      363      +16     
+ Partials       49       47       -2
Impacted Files Coverage Δ
ratelimit/ratelimit.go 100% <ø> (ø) ⬆️
logging/zerolog/ctxzr/context.go 0% <0%> (ø)
logging/zap/payload_interceptors.go 84.61% <100%> (ø) ⬆️
retry/retry.go 75% <0%> (-1.12%) ⬇️
logging/logrus/payload_interceptors.go 84.12% <0%> (ø) ⬆️
logging/logrus/grpclogger.go 0% <0%> (ø) ⬆️
tags/interceptors.go 88.88% <0%> (ø) ⬆️
tracing/opentracing/client_interceptors.go 76.81% <0%> (+2.52%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e0797f4...317004d. Read the comment docs.

Copy link
Contributor

@domgreen domgreen left a comment

Choose a reason for hiding this comment

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

Hey thanks for looking to add ZeroLog into the codebase 0️⃣ 📖

My main thing is I've not used ZeroLog before so would be good to get some more knowledge around it and how it is used. It looks to me that it is a strongly typed logging library and this is probably where its speed comes from.
I would like the addition of new loggers to be as similar as possible in approach as other loggers that exist in the code base this will make it much easier to make it generic and easier to refactor in the future.
Would you be able to do another pass and have a look if we can factor out the CtxLogger in favour fo adding the fields to the logger iteslef?
Also having a look at getting the DefaultDuration more inline with the others that are already in the codebase.

"github.com/rs/zerolog"
"google.golang.org/grpc"
"path"
"time"
Copy link
Contributor

Choose a reason for hiding this comment

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

can you run goimports if you havnt already :)

var (
// ClientField is used in every client-side log statement made through grpc_zap. Can be overwritten before initialization.
//ClientField = zap.String("span.kind", "client")
)
Copy link
Contributor

Choose a reason for hiding this comment

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

lets remove this var block and the commented code 👍


func logFinalClientLine(o *options, logger *ctxzr.CtxLogger, startTime time.Time, err error, msg string) {
code := o.codeFunc(err)
var level = o.levelFunc(code)
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
var level = o.levelFunc(code)
level := o.levelFunc(code)

code := o.codeFunc(err)
var level = o.levelFunc(code)

clientLogger := logger.Logger.WithLevel(level).Err(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure on the implementation of zero log but would hope this .Err(err) doesnt log it as an error log if the err is nil

Copy link
Contributor

Choose a reason for hiding this comment

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

It will, IME.

var level = o.levelFunc(code)

clientLogger := logger.Logger.WithLevel(level).Err(err)
args := make(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

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

could we create and assign values all in one for code, msg, duration?

}

func logCall(ctx context.Context, options *options, msg string, code codes.Code, startTime time.Time) {

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

extractedLogger := ctxzr.Extract(ctx)

var level = options.levelFunc(code)
logger := extractedLogger.Logger.WithLevel(level)
Copy link
Contributor

Choose a reason for hiding this comment

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

could you inline level here?

type CtxLogger struct {
Logger *zerolog.Logger
Fields map[string]interface{}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there anyway that we dont have to use our own CtxLogger? And instead add the fields onto the zerolog.Logger?


var (
ctxMarkerKey = &ctxMarker{}
nullLogger = &zerolog.Logger{}
Copy link
Contributor

Choose a reason for hiding this comment

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

can we inline this?

@@ -1,7 +1,7 @@
package ratelimit

import (
"golang.org/x/net/context"
"context"
Copy link
Contributor

Choose a reason for hiding this comment

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

can you rebase against master think this should have been fixed there already

@mingrammer
Copy link

Any update?

@rcmachado
Copy link

I was looking for inspiration in other middlewares to implement a zerolog gRPC middleware - I'm happy that I've found this PR :)

@Ahmet-Kaplan Is there anything we could do to help with this PR? Having this merged would be really helpful.

@tegk
Copy link
Contributor

tegk commented May 13, 2020

@Ahmet-Kaplan could you have a look at the v2 logging interface and perhaps try to get it merged there?

@irridia
Copy link
Contributor

irridia commented Jun 3, 2020

I only noticed this PR after I'd already submitted mine. :-( #297 follows the existing code structure more closely, FWIW.

@yashrsharma44
Copy link
Collaborator

As #297 addresses the changes proposed by this PR, closing this.

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.

8 participants