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

contrib/sirupsen/logrus: Add context logging hook #1240

Merged
merged 11 commits into from May 4, 2022

Conversation

ajgajg1134
Copy link
Contributor

This adds a hook for the logrus logging package. This makes it a bit easier to connect logs to their related span (as brought up in issue #936.

It doesn't seem like a new external API is needed, and due to the nature of context / golang in general it doesn't seem like there's any way to make it more ergonomic.

I tested this in the staging environment and was able to connect logs to their related spans.

@ajgajg1134 ajgajg1134 added this to the Triage milestone Apr 11, 2022
@ajgajg1134 ajgajg1134 self-assigned this Apr 11, 2022
@ajgajg1134 ajgajg1134 requested a review from a team April 11, 2022 14:49
@ajgajg1134 ajgajg1134 requested a review from a team as a code owner April 11, 2022 14:49
contrib/sirupsen/logrus/example_test.go Outdated Show resolved Hide resolved
contrib/sirupsen/logrus/example_test.go Outdated Show resolved Hide resolved
//Pass the current context to the logger
cLog := logrus.WithContext(sctx)
//Log as desired using the context-aware logger
cLog.Info("Completed some work!")
Copy link
Contributor

@gbbr gbbr Apr 12, 2022

Choose a reason for hiding this comment

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

Can we use // Output: here? (https://go.dev/blog/examples)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh neat! I've never used // Output: before.

It seems to make the example a bit more clunky as it means that the output has to be set to stdout and the time overridden to a fixed value. I've pushed up the change with the Output validation, let me know if you think it's not worth the extra couple lines of complexity. I'm leaning towards it's not worth it given there's a unit test that validates the Hook method already and my hope for the example was it's mostly copy/paste for users.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is great actually. But frankly I was expecting the comment to be just below the log call that causes it. But perhaps I don't fully understand how Output: works. Disclaimer: I've never used it.

contrib/sirupsen/logrus/logrus.go Outdated Show resolved Hide resolved
contrib/sirupsen/logrus/logrus_test.go Outdated Show resolved Hide resolved
contrib/sirupsen/logrus/example_test.go Outdated Show resolved Hide resolved
@ajgajg1134 ajgajg1134 requested a review from gbbr April 12, 2022 16:53
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

I agree this is a long example, even more so with the suggested tracer start & stop, but I think it's fine. It's mostly boilerplate and with actual usage its less. Nice work.

//Pass the current context to the logger
cLog := logrus.WithContext(sctx)
//Log as desired using the context-aware logger
cLog.Info("Completed some work!")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is great actually. But frankly I was expecting the comment to be just below the log call that causes it. But perhaps I don't fully understand how Output: works. Disclaimer: I've never used it.

contrib/sirupsen/logrus/example_test.go Outdated Show resolved Hide resolved
contrib/sirupsen/logrus/example_test.go Outdated Show resolved Hide resolved
@ajgajg1134 ajgajg1134 modified the milestones: Triage, 1.39.0 Apr 21, 2022
@ajgajg1134 ajgajg1134 requested a review from gbbr May 2, 2022 14:02
Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

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

Nice!

@ajgajg1134 ajgajg1134 merged commit 8affd29 into v1 May 4, 2022
@ajgajg1134 ajgajg1134 deleted the ajgajg1134/AddLogrusHook branch May 4, 2022 15:08
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.

None yet

3 participants