Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v1' into alex.normand/sql-commenter-proof-of-concept
- Loading branch information
Showing
17 changed files
with
312 additions
and
214 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,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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.