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

pkg/promtail: propagate a logger rather than using util.Logger globally #2438

Merged
merged 2 commits into from
Jul 28, 2020

Conversation

rfratto
Copy link
Member

@rfratto rfratto commented Jul 28, 2020

This PR allows for creating Promtail with a custom log.Logger instance which will be propagated and used consistently throughout the Promtail package. This allows for clients to provide a Promtail-specific logger.

The options pattern is now used for instantiating a Promtail instance; the only option to apply as of this PR is WithLogger. If unspecified, the default behavior is unchanged from how Promtail behaves today (i.e., using the global util.Logger).

The downside of this PR is having pass through the logger to more functions now, leaving the code a bit messier.

Address a portion of #2405.

This commit allows for creating promtail with a custom log.Logger
instance which will be propagated and used consistently throughout the
Promtail package. This allows for clients to provide a Promtail-specific
logger.

Address a portion of grafana#2405.
@pull-request-size pull-request-size bot added size/M and removed size/L labels Jul 28, 2020
@rfratto rfratto requested a review from owen-d July 28, 2020 16:23
@codecov-commenter
Copy link

Codecov Report

Merging #2438 into master will decrease coverage by 0.02%.
The diff coverage is 76.19%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2438      +/-   ##
==========================================
- Coverage   61.55%   61.53%   -0.03%     
==========================================
  Files         160      160              
  Lines       13612    13616       +4     
==========================================
- Hits         8379     8378       -1     
- Misses       4607     4611       +4     
- Partials      626      627       +1     
Impacted Files Coverage Δ
pkg/promtail/promtail.go 68.88% <58.33%> (-9.16%) ⬇️
pkg/promtail/client/logger.go 80.64% <100.00%> (ø)
pkg/promtail/targets/stdin/stdin_target_manager.go 73.33% <100.00%> (ø)

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

The WithLogger felt a bit unnecessarily complex to me, but LGTM.

type Option func(p *Promtail)

// WithLogger overrides the default logger for Promtail.
func WithLogger(log log.Logger) Option {
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a bit of a weird way to expose this. Since we're expecting to modify this externally, couldn't we just make the field public? Perhaps you wanted to make it only modifiable via this new function. I'm not really opinionated here though.

Copy link
Member Author

Choose a reason for hiding this comment

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

It'd have to be a public field in the Config struct for this to work since the logger is already passed around when New is called. I'm open to this, but I think it'd be a little strange to do that.

I do agree that the Option pattern is a big heavy handed for this specific PR, but my justification is that there's going to be a second With<X> function in my final PR for #2405, where you give it a custom mux router, where having two option functions would feel more justified.

I don't feel that strongly about this either though, I'm happy to make it a public field of Config if you think that'd be a better approach here.

@owen-d owen-d merged commit b5f0c75 into grafana:master Jul 28, 2020
@rfratto rfratto deleted the promtail-custom-logger branch September 18, 2024 12:54
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.

3 participants