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:
commit
4d48204a3d
@ -19,20 +19,11 @@ package tracing
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
)
|
||||
|
||||
const (
|
||||
spanDelimiter = "."
|
||||
)
|
||||
|
||||
func makeSpanName(names ...string) string {
|
||||
return strings.Join(names, spanDelimiter)
|
||||
}
|
||||
|
||||
func any(k string, v interface{}) attribute.KeyValue {
|
||||
func keyValue(k string, v any) attribute.KeyValue {
|
||||
if v == nil {
|
||||
return attribute.String(k, "<nil>")
|
||||
}
|
||||
|
@ -17,33 +17,49 @@
|
||||
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
|
||||
}
|
||||
|
||||
if !span.SpanContext().IsValid() || !span.IsRecording() {
|
||||
if !span.IsRecording() || !span.SpanContext().IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -57,10 +73,10 @@ 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, any(k, v))
|
||||
attrs = append(attrs, keyValue(k, v))
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
@ -96,14 +96,14 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//get TracingProcessorPlugin which is a dependency
|
||||
plugins, err := ic.GetByType(plugins.TracingProcessorPlugin)
|
||||
// get TracingProcessorPlugin which is a dependency
|
||||
tracingProcessors, err := ic.GetByType(plugins.TracingProcessorPlugin)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get tracing processors: %w", err)
|
||||
}
|
||||
|
||||
procs := make([]trace.SpanProcessor, 0, len(plugins))
|
||||
for _, p := range plugins {
|
||||
procs := make([]trace.SpanProcessor, 0, len(tracingProcessors))
|
||||
for _, p := range tracingProcessors {
|
||||
procs = append(procs, p.(trace.SpanProcessor))
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ package tracing
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"go.opentelemetry.io/otel"
|
||||
@ -99,14 +100,16 @@ func (s *Span) SetAttributes(kv ...attribute.KeyValue) {
|
||||
s.otelSpan.SetAttributes(kv...)
|
||||
}
|
||||
|
||||
const spanDelimiter = "."
|
||||
|
||||
// Name sets the span name by joining a list of strings in dot separated format.
|
||||
func Name(names ...string) string {
|
||||
return makeSpanName(names...)
|
||||
return strings.Join(names, spanDelimiter)
|
||||
}
|
||||
|
||||
// Attribute takes a key value pair and returns attribute.KeyValue type.
|
||||
func Attribute(k string, v interface{}) attribute.KeyValue {
|
||||
return any(k, v)
|
||||
func Attribute(k string, v any) attribute.KeyValue {
|
||||
return keyValue(k, v)
|
||||
}
|
||||
|
||||
// HTTPStatusCodeAttributes generates attributes of the HTTP namespace as specified by the OpenTelemetry
|
||||
|
Loading…
Reference in New Issue
Block a user