Merge pull request #10341 from thaJeztah/cleanup_traces

pkg/tracing: remove direct use of logrus, and fix some linting issues
This commit is contained in:
Derek McGowan 2024-06-14 17:48:11 +00:00 committed by GitHub
commit 4d48204a3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 26 deletions

View File

@ -19,20 +19,11 @@ package tracing
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
) )
const ( func keyValue(k string, v any) attribute.KeyValue {
spanDelimiter = "."
)
func makeSpanName(names ...string) string {
return strings.Join(names, spanDelimiter)
}
func any(k string, v interface{}) attribute.KeyValue {
if v == nil { if v == nil {
return attribute.String(k, "<nil>") return attribute.String(k, "<nil>")
} }

View File

@ -17,33 +17,49 @@
package tracing package tracing
import ( import (
"github.com/sirupsen/logrus" "github.com/containerd/log"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace" "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 // NewLogrusHook creates a new logrus hook
func NewLogrusHook() *LogrusHook { func NewLogrusHook() *LogrusHook {
return &LogrusHook{} return &LogrusHook{}
} }
// LogrusHook is a logrus hook which adds logrus events to active spans. // 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. // 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{} type LogrusHook struct{}
// Levels returns the logrus levels that this hook is interested in. // Levels returns the logrus levels that this hook is interested in.
func (h *LogrusHook) Levels() []logrus.Level { func (h *LogrusHook) Levels() []log.Level {
return logrus.AllLevels return allLevels
} }
// Fire is called when a log event occurs. // 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) span := trace.SpanFromContext(entry.Context)
if span == nil { if span == nil {
return nil return nil
} }
if !span.SpanContext().IsValid() || !span.IsRecording() { if !span.IsRecording() || !span.SpanContext().IsValid() {
return nil return nil
} }
@ -57,10 +73,10 @@ func (h *LogrusHook) Fire(entry *logrus.Entry) error {
return nil return nil
} }
func logrusDataToAttrs(data logrus.Fields) []attribute.KeyValue { func logrusDataToAttrs(data map[string]any) []attribute.KeyValue {
attrs := make([]attribute.KeyValue, 0, len(data)) attrs := make([]attribute.KeyValue, 0, len(data))
for k, v := range data { for k, v := range data {
attrs = append(attrs, any(k, v)) attrs = append(attrs, keyValue(k, v))
} }
return attrs return attrs
} }

View File

@ -96,14 +96,14 @@ func init() {
return nil, err return nil, err
} }
//get TracingProcessorPlugin which is a dependency // get TracingProcessorPlugin which is a dependency
plugins, err := ic.GetByType(plugins.TracingProcessorPlugin) tracingProcessors, err := ic.GetByType(plugins.TracingProcessorPlugin)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get tracing processors: %w", err) return nil, fmt.Errorf("failed to get tracing processors: %w", err)
} }
procs := make([]trace.SpanProcessor, 0, len(plugins)) procs := make([]trace.SpanProcessor, 0, len(tracingProcessors))
for _, p := range plugins { for _, p := range tracingProcessors {
procs = append(procs, p.(trace.SpanProcessor)) procs = append(procs, p.(trace.SpanProcessor))
} }

View File

@ -19,6 +19,7 @@ package tracing
import ( import (
"context" "context"
"net/http" "net/http"
"strings"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
@ -99,14 +100,16 @@ func (s *Span) SetAttributes(kv ...attribute.KeyValue) {
s.otelSpan.SetAttributes(kv...) s.otelSpan.SetAttributes(kv...)
} }
const spanDelimiter = "."
// Name sets the span name by joining a list of strings in dot separated format. // Name sets the span name by joining a list of strings in dot separated format.
func Name(names ...string) string { func Name(names ...string) string {
return makeSpanName(names...) return strings.Join(names, spanDelimiter)
} }
// Attribute takes a key value pair and returns attribute.KeyValue type. // Attribute takes a key value pair and returns attribute.KeyValue type.
func Attribute(k string, v interface{}) attribute.KeyValue { func Attribute(k string, v any) attribute.KeyValue {
return any(k, v) return keyValue(k, v)
} }
// HTTPStatusCodeAttributes generates attributes of the HTTP namespace as specified by the OpenTelemetry // HTTPStatusCodeAttributes generates attributes of the HTTP namespace as specified by the OpenTelemetry