Add open telemetry logging hook for logrus
This adds valuable logging data to the open telemetry traces. When the trace is not recording we don't bother doing anything as it is relatively expensive to convert logrus data to otel just due to the nature of how logrus works. The way this works is that we now set a context on the logrus.Entry that gets passed around which the hook then uses to determine if there is an active span to forward the logs to. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
@@ -52,7 +52,8 @@ const (
|
||||
// WithLogger returns a new context with the provided logger. Use in
|
||||
// combination with logger.WithField(s) for great effect.
|
||||
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
|
||||
return context.WithValue(ctx, loggerKey{}, logger)
|
||||
e := logger.WithContext(ctx)
|
||||
return context.WithValue(ctx, loggerKey{}, e)
|
||||
}
|
||||
|
||||
// GetLogger retrieves the current logger from the context. If no logger is
|
||||
@@ -61,7 +62,7 @@ func GetLogger(ctx context.Context) *logrus.Entry {
|
||||
logger := ctx.Value(loggerKey{})
|
||||
|
||||
if logger == nil {
|
||||
return L
|
||||
return L.WithContext(ctx)
|
||||
}
|
||||
|
||||
return logger.(*logrus.Entry)
|
||||
|
@@ -25,8 +25,6 @@ import (
|
||||
|
||||
func TestLoggerContext(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
assert.Equal(t, GetLogger(ctx), L) // should be same as L variable
|
||||
assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same.
|
||||
|
||||
ctx = WithLogger(ctx, G(ctx).WithField("test", "one"))
|
||||
assert.Equal(t, GetLogger(ctx).Data["test"], "one")
|
||||
|
Reference in New Issue
Block a user