-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Datadog hook for go-logger lib (#239)
* feat: Add Datadog hook for go-logger lib If the hook is added to the go-logger lib, and then a log-message is logged with `logger.WithContext(ctx).Info("my-message")`, then any datadog trace-information in the context will be added to the log-message's fields. Note: This commit will fail until go-logger has released a new version, with the HookEntry-struct. * chore: go mod tidy after go-logger was published
- Loading branch information
1 parent
39d1a31
commit 6b1e7e7
Showing
3 changed files
with
34 additions
and
3 deletions.
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
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
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 @@ | ||
package tracelogger | ||
|
||
import ( | ||
"github.com/coopnorge/go-logger" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// DDContextLogHook ensures that any span in the log context is correlated to log output. | ||
type DDContextLogHook struct{} | ||
|
||
// Fire implements logger.Hook interface, attaches trace and span details found in entry context | ||
func (d *DDContextLogHook) Fire(he *logger.HookEntry) (bool, error) { | ||
ctx := he.Context | ||
if ctx == nil { | ||
return false, nil | ||
} | ||
span, found := tracer.SpanFromContext(he.Context) | ||
if !found { | ||
return false, nil | ||
} | ||
he.Data["dd.trace_id"] = span.Context().TraceID() | ||
he.Data["dd.span_id"] = span.Context().SpanID() | ||
return true, nil | ||
} | ||
|
||
// NewHook will create a new Hook compatible with go-logger, | ||
// to automatically extract Span/Trace information from the Log-entry's context.Context, | ||
// and add them as fields to the log-message. | ||
func NewHook() logger.Hook { | ||
return &DDContextLogHook{} | ||
} |