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

profiler: add WithLogger option, respect DD_TRACE_STARTUP_LOGS #1334

Closed
wants to merge 6 commits into from

Conversation

nsrip-dd
Copy link
Contributor

@nsrip-dd nsrip-dd commented Jun 9, 2022

Resolves #1331

@nsrip-dd nsrip-dd requested a review from a team as a code owner June 9, 2022 17:16
@nsrip-dd nsrip-dd added this to the 1.39.0 milestone Jun 9, 2022
// WithLogger sets logger as the profiler's error printer.
// Note that setting the logger for the profiler sets the same logger for the
// tracer, and vice-versa.
func WithLogger(logger ddtrace.Logger) Option {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if this is a good approach. In hindsight, it was a bad choice for the tracer too. This setting is not app-specific, it is repo wide. Someone could come later and ask the same thing from AppSec or some other component that may come up. Or even worse, they could expect to be able to set different logger for different apps, which is wrong.

I suggest considering to make the logger exported instead...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean make internal/log.UseLogger exported, as requested in #1331? Alternatively, I could keep this interface and refactor the profiler to do its own logging internally. Or we could refactor internal/log to provide a new Logger type with all the same Error, Warn, etc. methods, so that each package can use its own logger or fall back to a default, shared logger.

@nsrip-dd nsrip-dd changed the title profiler: add WithLogger option to match the tracer profiler: add WithLogger option, respect DD_TRACE_STARTUP_LOGS Jun 10, 2022
@nsrip-dd nsrip-dd modified the milestones: 1.39.0, 1.40.0 Jun 30, 2022
Copy link
Member

@felixge felixge left a comment

Choose a reason for hiding this comment

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

LGTM for the most parts, but got a few questions.

profiler/log.go Outdated

func (l *logger) Info(format string, a ...interface{}) { l.msg("", format, a...) }
func (l *logger) Error(format string, a ...interface{}) { l.msg("", format, a...) }
func (l *logger) Warn(format string, a ...interface{}) { l.msg("", format, a...) }
Copy link
Member

Choose a reason for hiding this comment

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

Why is the lvl = "" for all of the msg() calls above?

Also: Wouldn't it be nicer to put this logger implementation into internal/log and reuse it here? Or is there a specific reason for the code duplication? (It's not a lot of duplication, so I'm not too worried, just curious).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RE: lvl, that was an oversight, I'll fix it.

RE: duplication, I'm not sure what the best approach is. The first version of this PR set the logger using internal/log.UseLogger, which would set the global logger for every dd-trace-go package that uses internal/log. If we're okay with that, maybe making UseLogger public is better than adding a WithLogger option? Having two WithLogger options creates the impression that logging can be configured independently for different packages, as Gabriel points out.

If we do want a logger struct like this so that packages can do their own, independent logging, we could certainly move this code into internal/log, but unless we also make the tracer use this struct then there will still be some duplicated code.

Any thoughts on what's better?

profiler/options.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

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

Thanks Felix! See my followup comments on the WithLogger option. I'm not 100% on whether to keep this code or do it a different way.

P.S. I was thinking of moving the DD_TRACE_STARTUP_LOGS change into a separate PR since I think we'll want to support that environment variable regardless of how we configure logging. Should I go ahead and do that, or just keep the change in this PR?

profiler/options.go Outdated Show resolved Hide resolved
profiler/log.go Outdated

func (l *logger) Info(format string, a ...interface{}) { l.msg("", format, a...) }
func (l *logger) Error(format string, a ...interface{}) { l.msg("", format, a...) }
func (l *logger) Warn(format string, a ...interface{}) { l.msg("", format, a...) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

RE: lvl, that was an oversight, I'll fix it.

RE: duplication, I'm not sure what the best approach is. The first version of this PR set the logger using internal/log.UseLogger, which would set the global logger for every dd-trace-go package that uses internal/log. If we're okay with that, maybe making UseLogger public is better than adding a WithLogger option? Having two WithLogger options creates the impression that logging can be configured independently for different packages, as Gabriel points out.

If we do want a logger struct like this so that packages can do their own, independent logging, we could certainly move this code into internal/log, but unless we also make the tracer use this struct then there will still be some duplicated code.

Any thoughts on what's better?

@Hellzy Hellzy modified the milestones: 1.40.0, 1.41.0 Jul 15, 2022
@nsrip-dd
Copy link
Contributor Author

I'm going to close this in favor of two separate PRs:

  • One to check for DD_TRACE_STARTUP_LOGS
  • One to handle configurable logging, which might be better done through publicly exporting internal.UseLogger.

@nsrip-dd nsrip-dd closed this Jul 15, 2022
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.

question: can you make log.UseLogger public API?
4 participants