Skip to content

Commit 4d48204

Browse files
authored
Merge pull request containerd#10341 from thaJeztah/cleanup_traces
pkg/tracing: remove direct use of logrus, and fix some linting issues
2 parents 27de5fe + 587ee80 commit 4d48204

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

pkg/tracing/helpers.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,11 @@ package tracing
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"strings"
2322

2423
"go.opentelemetry.io/otel/attribute"
2524
)
2625

27-
const (
28-
spanDelimiter = "."
29-
)
30-
31-
func makeSpanName(names ...string) string {
32-
return strings.Join(names, spanDelimiter)
33-
}
34-
35-
func any(k string, v interface{}) attribute.KeyValue {
26+
func keyValue(k string, v any) attribute.KeyValue {
3627
if v == nil {
3728
return attribute.String(k, "<nil>")
3829
}

pkg/tracing/log.go

+25-9
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,49 @@
1717
package tracing
1818

1919
import (
20-
"github.com/sirupsen/logrus"
20+
"github.com/containerd/log"
2121
"go.opentelemetry.io/otel/attribute"
2222
"go.opentelemetry.io/otel/trace"
2323
)
2424

25+
// allLevels is the equivalent to [logrus.AllLevels].
26+
//
27+
// [logrus.AllLevels]: https://github.com/sirupsen/logrus/blob/v1.9.3/logrus.go#L80-L89
28+
var allLevels = []log.Level{
29+
log.PanicLevel,
30+
log.FatalLevel,
31+
log.ErrorLevel,
32+
log.WarnLevel,
33+
log.InfoLevel,
34+
log.DebugLevel,
35+
log.TraceLevel,
36+
}
37+
2538
// NewLogrusHook creates a new logrus hook
2639
func NewLogrusHook() *LogrusHook {
2740
return &LogrusHook{}
2841
}
2942

30-
// LogrusHook is a logrus hook which adds logrus events to active spans.
31-
// If the span is not recording or the span context is invalid, the hook is a no-op.
43+
// LogrusHook is a [logrus.Hook] which adds logrus events to active spans.
44+
// If the span is not recording or the span context is invalid, the hook
45+
// is a no-op.
46+
//
47+
// [logrus.Hook]: https://github.com/sirupsen/logrus/blob/v1.9.3/hooks.go#L3-L11
3248
type LogrusHook struct{}
3349

3450
// Levels returns the logrus levels that this hook is interested in.
35-
func (h *LogrusHook) Levels() []logrus.Level {
36-
return logrus.AllLevels
51+
func (h *LogrusHook) Levels() []log.Level {
52+
return allLevels
3753
}
3854

3955
// Fire is called when a log event occurs.
40-
func (h *LogrusHook) Fire(entry *logrus.Entry) error {
56+
func (h *LogrusHook) Fire(entry *log.Entry) error {
4157
span := trace.SpanFromContext(entry.Context)
4258
if span == nil {
4359
return nil
4460
}
4561

46-
if !span.SpanContext().IsValid() || !span.IsRecording() {
62+
if !span.IsRecording() || !span.SpanContext().IsValid() {
4763
return nil
4864
}
4965

@@ -57,10 +73,10 @@ func (h *LogrusHook) Fire(entry *logrus.Entry) error {
5773
return nil
5874
}
5975

60-
func logrusDataToAttrs(data logrus.Fields) []attribute.KeyValue {
76+
func logrusDataToAttrs(data map[string]any) []attribute.KeyValue {
6177
attrs := make([]attribute.KeyValue, 0, len(data))
6278
for k, v := range data {
63-
attrs = append(attrs, any(k, v))
79+
attrs = append(attrs, keyValue(k, v))
6480
}
6581
return attrs
6682
}

pkg/tracing/plugin/otlp.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ func init() {
9696
return nil, err
9797
}
9898

99-
//get TracingProcessorPlugin which is a dependency
100-
plugins, err := ic.GetByType(plugins.TracingProcessorPlugin)
99+
// get TracingProcessorPlugin which is a dependency
100+
tracingProcessors, err := ic.GetByType(plugins.TracingProcessorPlugin)
101101
if err != nil {
102102
return nil, fmt.Errorf("failed to get tracing processors: %w", err)
103103
}
104104

105-
procs := make([]trace.SpanProcessor, 0, len(plugins))
106-
for _, p := range plugins {
105+
procs := make([]trace.SpanProcessor, 0, len(tracingProcessors))
106+
for _, p := range tracingProcessors {
107107
procs = append(procs, p.(trace.SpanProcessor))
108108
}
109109

pkg/tracing/tracing.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package tracing
1919
import (
2020
"context"
2121
"net/http"
22+
"strings"
2223

2324
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
2425
"go.opentelemetry.io/otel"
@@ -99,14 +100,16 @@ func (s *Span) SetAttributes(kv ...attribute.KeyValue) {
99100
s.otelSpan.SetAttributes(kv...)
100101
}
101102

103+
const spanDelimiter = "."
104+
102105
// Name sets the span name by joining a list of strings in dot separated format.
103106
func Name(names ...string) string {
104-
return makeSpanName(names...)
107+
return strings.Join(names, spanDelimiter)
105108
}
106109

107110
// Attribute takes a key value pair and returns attribute.KeyValue type.
108-
func Attribute(k string, v interface{}) attribute.KeyValue {
109-
return any(k, v)
111+
func Attribute(k string, v any) attribute.KeyValue {
112+
return keyValue(k, v)
110113
}
111114

112115
// HTTPStatusCodeAttributes generates attributes of the HTTP namespace as specified by the OpenTelemetry

0 commit comments

Comments
 (0)