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: Allow request logging config to determine log level #4956

Closed
wants to merge 1 commit into from

Conversation

philipgough
Copy link
Contributor

@philipgough philipgough commented Dec 15, 2021

Perhaps I am misunderstanding the original intentions, but the request logging configuration seems overly complicated.

I started working on documenting the config in #4934 and there are some comments there, but overall I find the config is unclear.

This PR changes the implementation slightly, since my take is that if I have gone to the trouble of configuring request logging, all I want to do is specify at what level I believe that should be output and have that determined by the --log.level

This change allows the 'level' config in request logging 'options'
to determine the output level for those logs.

Prior to this, without setting --log.level flag on the component
to debug, no logs were emitted.

With this implementation, I think it would make sense to restrict the logging options to one of debug, info

If this approach seems reasonable to the maintainers we would need to tackle gRPC in similar fashion.

  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user.

Changes

Verification

@philipgough philipgough force-pushed the query-log-impl branch 2 times, most recently from a526e09 to ae59973 Compare December 15, 2021 15:02
This change allows the 'level' config in request logging 'options'
to determine the output level for those logs.

Prior to this, without setting --log.level flag on the component
to debug, no logs were emitted.

Signed-off-by: Philip Gough <philip.p.gough@gmail.com>
@@ -24,17 +24,17 @@ type HTTPServerMiddleware struct {
}

func (m *HTTPServerMiddleware) preCall(name string, start time.Time, r *http.Request) {
logger := m.opts.filterLog(m.logger)
level.Debug(logger).Log("http.start_time", start.String(), "http.method", fmt.Sprintf("%s %s", r.Method, r.URL), "http.request_id", r.Header.Get("X-Request-ID"), "thanos.method_name", name, "msg", "started call")
m.opts.levelledLogger(m.opts.filterLog(m.logger)).Log(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this logger could probably be configured once in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thinking about this some more I am also not sure that the filter is even needed considering we have setup a logger via cmd/main.go that is filtered based on --log.level and passed in to this middleware

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think if we use the leveledLogger here to achieve the intended effect (i.e. log the requests on the configured level), filterLog should not be necessary as you said - we should have that governed by the global flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure, totally agree - maybe @bwplotka could chime in and let us know if there were "bigger plans" for the logging package or do we really need to expose these functional options at all? Maybe we are missing some context


logger = m.opts.filterLog(logger)
m.opts.levelFunc(logger, status).Log("msg", "finished call")
m.opts.levelledLogger(m.opts.filterLog(m.logger)).Log(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as above

@@ -69,6 +70,12 @@ func WithFilter(f FilterLogging) Option {
}
}

func WithLevelledLogger(f LevelledLogging) Option {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering what is actually the reason to have WithLeveledLogger and WithFilter to be functional options and exported? It's not like we support users to provide their own leveled logger or filter, or do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, I am not sure that the functional options are required here. However I am not sure if there were other plans for the logging package to be used elsewhere or what the initial driver for that code was. It does seem excessive for the way the package is currently used.

To answer your question in regards to this LevelledLogger - the input in the config determines the level to log at, which is why I am using this currently. I believe it could all be done in the constructor as I had commented previously

@@ -125,6 +136,21 @@ func DefaultCodeToLevel(logger log.Logger, code int) log.Logger {
return level.Error(logger)
}

func DefaultLevelledLogger(logger log.Logger, lvl string) log.Logger {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we allow only Info and Debug in here as you mentioned in the PR description? Or perhaps we could validate level beforehand and only accept those two levels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #4956 (comment) but yes, or we could restrict the request logging config to only those two options while keeping the package more generic.

I didn't want to overhaul to much here without feedback from maintainers first :)

@@ -24,17 +24,17 @@ type HTTPServerMiddleware struct {
}

func (m *HTTPServerMiddleware) preCall(name string, start time.Time, r *http.Request) {
logger := m.opts.filterLog(m.logger)
level.Debug(logger).Log("http.start_time", start.String(), "http.method", fmt.Sprintf("%s %s", r.Method, r.URL), "http.request_id", r.Header.Get("X-Request-ID"), "thanos.method_name", name, "msg", "started call")
m.opts.levelledLogger(m.opts.filterLog(m.logger)).Log(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think if we use the leveledLogger here to achieve the intended effect (i.e. log the requests on the configured level), filterLog should not be necessary as you said - we should have that governed by the global flag.

@philipgough philipgough marked this pull request as draft December 20, 2021 17:20
@philipgough
Copy link
Contributor Author

closing based on the docs improvements in #4934

we can track improvements in #4961

@philipgough philipgough deleted the query-log-impl branch January 27, 2022 10:25
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.

None yet

2 participants