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
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
be52bb7
contrib/sirupsen/logrus: Add context logging hook
ajgajg1134 d1467cf
Merge branch 'v1' into ajgajg1134/AddLogrusHook
ajgajg1134 915e70d
contrib/sirupsen/logrus: fix goimports
ajgajg1134 8dabe97
Merge branch 'ajgajg1134/AddLogrusHook' of github.com:DataDog/dd-trac…
ajgajg1134 8e1b122
Add comment for exported methods
ajgajg1134 7004aac
PR Comments, fix formatting etc
ajgajg1134 06c54d9
Address PR comments
ajgajg1134 c2bdde8
Merge branch 'v1' into ajgajg1134/AddLogrusHook
ajgajg1134 49ce27b
Merge branch 'v1' into ajgajg1134/AddLogrusHook
ajgajg1134 3ddb9ba
Merge branch 'v1' into ajgajg1134/AddLogrusHook
knusbaum e15aec0
Merge branch 'v1' into ajgajg1134/AddLogrusHook
ajgajg1134 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
package logrus | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"time" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func ExampleHook() { | ||
// Ensure your tracer is started and stopped | ||
// Setup logrus, do this once at the beginning of your program | ||
logrus.SetFormatter(&logrus.JSONFormatter{}) | ||
logrus.AddHook(&DDContextLogHook{}) | ||
logrus.SetOutput(os.Stdout) | ||
|
||
span, sctx := tracer.StartSpanFromContext(context.Background(), "mySpan") | ||
defer span.Finish() | ||
|
||
// Pass the current span context to the logger (Time is set for consistency in output here) | ||
cLog := logrus.WithContext(sctx).WithTime(time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC)) | ||
// Log as desired using the context-aware logger | ||
cLog.Info("Completed some work!") | ||
// Output: | ||
// {"dd.span_id":0,"dd.trace_id":0,"level":"info","msg":"Completed some work!","time":"2000-01-01T01:01:01Z"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
// Package logrus provides a log/span correlation hook for the sirupsen/logrus package (https://github.com/sirupsen/logrus). | ||
package logrus | ||
|
||
import ( | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// DDContextLogHook ensures that any span in the log context is correlated to log output. | ||
type DDContextLogHook struct{} | ||
|
||
// Levels implements logrus.Hook interface, this hook applies to all defined levels | ||
func (d *DDContextLogHook) Levels() []logrus.Level { | ||
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel, logrus.InfoLevel, logrus.DebugLevel, logrus.TraceLevel} | ||
} | ||
|
||
// Fire implements logrus.Hook interface, attaches trace and span details found in entry context | ||
func (d *DDContextLogHook) Fire(e *logrus.Entry) error { | ||
span, found := tracer.SpanFromContext(e.Context) | ||
if !found { | ||
return nil | ||
} | ||
e.Data["dd.trace_id"] = span.Context().TraceID() | ||
e.Data["dd.span_id"] = span.Context().SpanID() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
package logrus | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFire(t *testing.T) { | ||
tracer.Start() | ||
defer tracer.Stop() | ||
_, sctx := tracer.StartSpanFromContext(context.Background(), "testSpan", tracer.WithSpanID(1234)) | ||
|
||
hook := &DDContextLogHook{} | ||
e := logrus.NewEntry(logrus.New()) | ||
e.Context = sctx | ||
err := hook.Fire(e) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, uint64(1234), e.Data["dd.trace_id"]) | ||
assert.Equal(t, uint64(1234), e.Data["dd.span_id"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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 theHook
method already and my hope for the example was it's mostly copy/paste for users.There was a problem hiding this comment.
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.