From ccf7938126e0d7a52b3ef8a1d45468732ba866de Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 14 Jun 2024 12:08:51 +0200 Subject: [PATCH] pkg/tracing: remove direct use of github.com/sirupsen/logrus While the hook is intended to be used with logrus, we don't need to have the direct import; use the aliases provided by the containerd/log module instead. Signed-off-by: Sebastiaan van Stijn --- pkg/tracing/log.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/tracing/log.go b/pkg/tracing/log.go index 5834d03b0..f2604cd60 100644 --- a/pkg/tracing/log.go +++ b/pkg/tracing/log.go @@ -17,27 +17,43 @@ package tracing import ( - "github.com/sirupsen/logrus" + "github.com/containerd/log" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" ) +// allLevels is the equivalent to [logrus.AllLevels]. +// +// [logrus.AllLevels]: https://github.com/sirupsen/logrus/blob/v1.9.3/logrus.go#L80-L89 +var allLevels = []log.Level{ + log.PanicLevel, + log.FatalLevel, + log.ErrorLevel, + log.WarnLevel, + log.InfoLevel, + log.DebugLevel, + log.TraceLevel, +} + // NewLogrusHook creates a new logrus hook func NewLogrusHook() *LogrusHook { return &LogrusHook{} } -// LogrusHook is a logrus hook which adds logrus events to active spans. -// If the span is not recording or the span context is invalid, the hook is a no-op. +// LogrusHook is a [logrus.Hook] which adds logrus events to active spans. +// If the span is not recording or the span context is invalid, the hook +// is a no-op. +// +// [logrus.Hook]: https://github.com/sirupsen/logrus/blob/v1.9.3/hooks.go#L3-L11 type LogrusHook struct{} // Levels returns the logrus levels that this hook is interested in. -func (h *LogrusHook) Levels() []logrus.Level { - return logrus.AllLevels +func (h *LogrusHook) Levels() []log.Level { + return allLevels } // Fire is called when a log event occurs. -func (h *LogrusHook) Fire(entry *logrus.Entry) error { +func (h *LogrusHook) Fire(entry *log.Entry) error { span := trace.SpanFromContext(entry.Context) if span == nil { return nil @@ -57,7 +73,7 @@ func (h *LogrusHook) Fire(entry *logrus.Entry) error { return nil } -func logrusDataToAttrs(data logrus.Fields) []attribute.KeyValue { +func logrusDataToAttrs(data map[string]any) []attribute.KeyValue { attrs := make([]attribute.KeyValue, 0, len(data)) for k, v := range data { attrs = append(attrs, keyValue(k, v))