Skip to content

Commit

Permalink
contrib/sirupsen/logrus: Add context logging hook (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgajg1134 committed May 4, 2022
1 parent ededfe7 commit 8affd29
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 46 deletions.
34 changes: 34 additions & 0 deletions contrib/sirupsen/logrus/example_test.go
@@ -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"}
}
32 changes: 32 additions & 0 deletions contrib/sirupsen/logrus/logrus.go
@@ -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
}
31 changes: 31 additions & 0 deletions contrib/sirupsen/logrus/logrus_test.go
@@ -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"])
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -74,7 +74,7 @@ require (
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pierrec/lz4 v2.5.2+incompatible // indirect
github.com/segmentio/kafka-go v0.4.29
github.com/stretchr/objx v0.2.0 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.0
github.com/tidwall/btree v1.1.0 // indirect
Expand Down

0 comments on commit 8affd29

Please sign in to comment.