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

Request Logging #311

Merged
merged 31 commits into from
Oct 2, 2020
Merged

Request Logging #311

merged 31 commits into from
Oct 2, 2020

Conversation

yashrsharma44
Copy link
Collaborator

@yashrsharma44 yashrsharma44 commented Jun 29, 2020

This PR achieves to add in a Request/Response logger in grpc_middlewares v2 branch.

Fixes #310

Current TODO -

If any suggestions are there, feel free to add in more 😅

Signed-off-by: Yash Sharma yashrsharma44@gmail.com

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Hm, I still don't get the need for separate logger.

We have already one reporter. One Request is needed? the reporter is also request.

The only missing log line would be on start which we can do based on decider, no? 🤔

interceptors/logging/logging.go Outdated Show resolved Hide resolved
interceptors/logging/request_logging.go Outdated Show resolved Hide resolved
Copy link

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

So far so good. I've left a couple of suggestions.

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Hm, not sure about it either, will take a look closer how we could implement it and let's talk on our 1:2 cc @kakkoyun

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
@bwplotka
Copy link
Collaborator

bwplotka commented Jul 2, 2020

There is easy way how to do what we want btw (:

@yashrsharma44
Copy link
Collaborator Author

yashrsharma44 commented Jul 6, 2020

After having a 1:2 call with @bwplotka and @kakkoyun, we decided to modify the objectives of this PR -

  • Having a precall setup before the actual logging starts
    • Decider to check if logging should be enabled (Implemented at the interceptor level. If not enabled, we return a NoopReporter)
    • Decider to check if logging should be enabled for a given method( To be implemented using the options of the grpc.Options)
    • Log the starting messages (We check using a boolean if the current message is the first and log it in the PostMsgSend/PostMsgRecv level)
  • PostMsgSend / PostMsgRecv can log the payload (currently to be worked on in the next PR)
  • PostCall to log the errors and corresponding error-logs

This is a bit different from the original implementation because in the earlier PR, we were logging all the messages of the stream, which is quite verbose and irrelevant.

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice, looking awesome, just minor nits!

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/logging.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors_test.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
@yashrsharma44 yashrsharma44 marked this pull request as ready for review July 13, 2020 12:40
@yashrsharma44
Copy link
Collaborator Author

yashrsharma44 commented Jul 13, 2020

Hey Everyone, this PR is ready to be merged.
Would love a final round of review - @kakkoyun @bwplotka @johanbrandhorst

@yashrsharma44 yashrsharma44 force-pushed the request-logging branch 4 times, most recently from 564b8f0 to fddea8d Compare July 13, 2020 13:23
Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Hm, I somehow don't see the latest revision of this, let's discuss

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
@yashrsharma44
Copy link
Collaborator Author

Tests failing due to - #319

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

It's really LGTM, just small nits on the actual message logged (: and maybe decider.

This PR shows that we might be missing details in the initial design about the "interface" of this change.

The interface is really:

  • how user will configure
  • what users will see on different configuration.

Can we add some design section for it to make it clear? (: It would avoid some confusion.

IMO:

  • Human readable consistent message, and having all variables as fields (e.g type, method as well!)
  • finished call and started call is really enough if we would have component:server field or something, right?
  • What about single decider:
type LogDecision struct {
     LogStarted, LogFinished bool
}

type Decider func (....) Decision

The alternative is:

type Decider int
const (
    NoLog Decider = iota
    LogStarted
    LogFinished
  
)

WDYT? (:

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/options.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
@yashrsharma44 yashrsharma44 changed the title Request/Response Logging Request Logging Jul 22, 2020
@yashrsharma44
Copy link
Collaborator Author

yashrsharma44 commented Jul 23, 2020

Changes made uptil now -

  • Human Readable Messages - All the log entries are changed to use space instead of _.
  • Using key-value pairs for storing the kind, type and component.
  • Changed to start call and finished call.

Changes that need to be made -

  • Add a design/example section for using the API(Mentioned in my original TODO)
  • Single Decider with multiple options, and an ENUM based interface to configure the options in an easy way.

@yashrsharma44
Copy link
Collaborator Author

yashrsharma44 commented Jul 23, 2020

I would like to extend the Decider as using a struct instead of ENUM, because -

  • We would have 2^2 possibilities, so we need to keep 4 variables for checking the option of logging.
  • We can use a struct for storing two variables to check if we want to log at start and end. This way, with minimal fuss, all the conditions are checked.

Interface of new Decider -

type LogDecision struct {
     LogStarted, LogFinished bool
}
func NewLogDecision(LogStarted, LogFinished bool) *LogDecision {
     return &LogDecision {
          LogStarted : LogStarted,
          LogFinished : LogFinished,
     }
}

type Decider func (methodName string, err error) *LogDecision

// Example implementation of the decider
func LogDecider(methodName string, err error) *LogDecision {
          // Have condition check
          return NewLogDecision(logStart, LogFinish)
}

WDYT?

Copy link

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

nit

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
@kakkoyun
Copy link

@yashrsharma44 Ok, the interface is apparently different than I had assumed so you can ignore my comments or even better we can update the interface since this is a v2 branch.

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Some notes after 1:1

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors_test.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors_test.go Outdated Show resolved Hide resolved
interceptors/logging/interceptors_test.go Show resolved Hide resolved
@bwplotka
Copy link
Collaborator

Looks like we want decider to NOT have error and do adaptive logging base on error, using errCode to Log Level, 💪

Looks like we are going into simpler functions, which means I think I would vote for enums

@yashrsharma44
Copy link
Collaborator Author

Once we fixate on the Decider implementation, I can start with the examples 🤓

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice, Looking good! Small things only.

interceptors/logging/interceptors.go Outdated Show resolved Hide resolved
interceptors/logging/logging.go Show resolved Hide resolved
interceptors/logging/logging.go Show resolved Hide resolved
Add changes from bartek's review
…rom the server side

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
…rt and end of request and response start messages

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
* Unary Server testing
* Stream server testing
* Unary Error testing

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>

nitpickings

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>

more nitpickings

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
* return early for multiple cases
* intialize the level early

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
* Now enabling logging logs out all the details of the request/response
* Breaks the initial logging api

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
… encoding

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
*  Human Readable Messages - All the log entries are changed to use space instead of `_`.
* Using key-value pairs for storing the kind, type and component.
* Changed to start call and finished call.

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
* Abstracted out repititive fields into a single method
* Added test fields, instead of creating a whole suite
* Changed field values to adhere the common convention

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
…suite rather than creating a new suite

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
* Currently 3 types of logging configured
* NoLogCall - No logging
* LogFinishCall - For logging the end of the call
* LogAllCall - For logging the start and end of the call

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>

More comments removal

Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Signed-off-by: Yash Sharma <yashrsharma44@gmail.com>
Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Tiny nit only, we can fix that later (:

LGTM, Thanks! 👍

code := c.opts.codeFunc(err)
logger = logger.With("grpc.code", code.String())
if err != nil {
logger = logger.With("grpc.error", fmt.Sprintf("%v", err))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
logger = logger.With("grpc.error", fmt.Sprintf("%v", err))
logger = logger.With("grpc.error", err.Error())

@bwplotka bwplotka merged commit 73cf2ae into grpc-ecosystem:v2 Oct 2, 2020
@@ -47,12 +60,12 @@ func DefaultErrorToCode(err error) codes.Code {
}

// Decider function defines rules for suppressing any interceptor logs
type Decider func(fullMethodName string, err error) bool
type Decider func(fullMethodName string) Decision
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would have been nice not to have skipped the error parameter here. The logging can also be configured using the error as well,(since status code can be derived from the response error).

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.

v2: Feature Request : Request/Response Logging
4 participants