Make tracing interface to coalesce logging/tracing (#519)
* Define and use an interface for logging. This allows alternative implementations to use whatever logging package they want. Currently the interface just mimicks what logrus already implements, with minor modifications to not rely on logrus itself. I think the interface is pretty solid in terms of logging implementations being able to do what they need to. * Make tracing interface to coalesce logging/tracing Allows us to share data between the tracer and the logger so we can simplify log/trace handling wher we generally want data to go both places.
This commit is contained in:
23
trace/nop.go
Normal file
23
trace/nop.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
type nopTracer struct{}
|
||||
|
||||
func (nopTracer) StartSpan(ctx context.Context, _ string) (context.Context, Span) {
|
||||
return ctx, &nopSpan{}
|
||||
}
|
||||
|
||||
type nopSpan struct{}
|
||||
|
||||
func (nopSpan) End() {}
|
||||
func (nopSpan) SetStatus(trace.Status) {}
|
||||
func (nopSpan) Logger() log.Logger { return nil }
|
||||
|
||||
func (nopSpan) WithField(ctx context.Context, _ string, _ interface{}) context.Context { return ctx }
|
||||
func (nopSpan) WithFields(ctx context.Context, _ log.Fields) context.Context { return ctx }
|
||||
236
trace/opencensus/opencensus.go
Normal file
236
trace/opencensus/opencensus.go
Normal file
@@ -0,0 +1,236 @@
|
||||
// Package opencensus implements a github.com/virtual-kubelet/virtual-kubelet/trace.Tracer
|
||||
// using opencensus as a backend.
|
||||
//
|
||||
// Use this by setting `trace.T = Adapter{}`
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/trace"
|
||||
octrace "go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
lDebug = "DEBUG"
|
||||
lInfo = "INFO"
|
||||
lWarn = "WARN"
|
||||
lErr = "ERROR"
|
||||
lFatal = "FATAL"
|
||||
)
|
||||
|
||||
// Adapter implements the trace.Tracer interface for OpenCensus
|
||||
type Adapter struct{}
|
||||
|
||||
// StartSpan creates a new span from opencensus using the given name.
|
||||
func (Adapter) StartSpan(ctx context.Context, name string) (context.Context, trace.Span) {
|
||||
ctx, ocs := octrace.StartSpan(ctx, name)
|
||||
l := log.G(ctx).WithField("method", name)
|
||||
|
||||
s := &span{s: ocs, l: l}
|
||||
ctx = log.WithLogger(ctx, s.Logger())
|
||||
|
||||
return ctx, s
|
||||
}
|
||||
|
||||
type span struct {
|
||||
mu sync.Mutex
|
||||
s *octrace.Span
|
||||
l log.Logger
|
||||
}
|
||||
|
||||
func (s *span) End() {
|
||||
s.s.End()
|
||||
}
|
||||
|
||||
func (s *span) SetStatus(status trace.Status) {
|
||||
s.s.SetStatus(status)
|
||||
}
|
||||
|
||||
func (s *span) WithField(ctx context.Context, key string, val interface{}) context.Context {
|
||||
s.mu.Lock()
|
||||
s.l = s.l.WithField(key, val)
|
||||
ctx = log.WithLogger(ctx, &logger{s: s.s, l: s.l})
|
||||
s.mu.Unlock()
|
||||
|
||||
if s.s.IsRecordingEvents() {
|
||||
s.s.AddAttributes(makeAttribute(key, val))
|
||||
}
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (s *span) WithFields(ctx context.Context, f log.Fields) context.Context {
|
||||
s.mu.Lock()
|
||||
s.l = s.l.WithFields(f)
|
||||
ctx = log.WithLogger(ctx, &logger{s: s.s, l: s.l})
|
||||
s.mu.Unlock()
|
||||
|
||||
if s.s.IsRecordingEvents() {
|
||||
attrs := make([]octrace.Attribute, 0, len(f))
|
||||
for k, v := range f {
|
||||
attrs = append(attrs, makeAttribute(k, v))
|
||||
}
|
||||
s.s.AddAttributes(attrs...)
|
||||
}
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (s *span) Logger() log.Logger {
|
||||
return &logger{s: s.s, l: s.l}
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
s *octrace.Span
|
||||
l log.Logger
|
||||
a []octrace.Attribute
|
||||
}
|
||||
|
||||
func (l *logger) Debug(args ...interface{}) {
|
||||
if !l.s.IsRecordingEvents() {
|
||||
l.l.Debug(args...)
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprint(args...)
|
||||
l.l.Debug(msg)
|
||||
l.s.Annotate(withLevel(lDebug, l.a), msg)
|
||||
}
|
||||
|
||||
func (l *logger) Debugf(f string, args ...interface{}) {
|
||||
l.l.Debugf(f, args)
|
||||
l.s.Annotatef(withLevel(lDebug, l.a), f, args...)
|
||||
}
|
||||
|
||||
func (l *logger) Info(args ...interface{}) {
|
||||
if !l.s.IsRecordingEvents() {
|
||||
l.l.Info(args...)
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprint(args...)
|
||||
l.l.Info(msg)
|
||||
l.s.Annotate(withLevel(lInfo, l.a), msg)
|
||||
}
|
||||
|
||||
func (l *logger) Infof(f string, args ...interface{}) {
|
||||
l.l.Infof(f, args)
|
||||
l.s.Annotatef(withLevel(lInfo, l.a), f, args...)
|
||||
}
|
||||
|
||||
func (l *logger) Warn(args ...interface{}) {
|
||||
if !l.s.IsRecordingEvents() {
|
||||
l.l.Warn(args...)
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprint(args...)
|
||||
l.l.Warn(msg)
|
||||
l.s.Annotate(withLevel(lWarn, l.a), msg)
|
||||
}
|
||||
|
||||
func (l *logger) Warnf(f string, args ...interface{}) {
|
||||
l.l.Warnf(f, args)
|
||||
l.s.Annotatef(withLevel(lWarn, l.a), f, args...)
|
||||
}
|
||||
|
||||
func (l *logger) Error(args ...interface{}) {
|
||||
if !l.s.IsRecordingEvents() {
|
||||
l.l.Error(args...)
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprint(args...)
|
||||
l.l.Error(msg)
|
||||
l.s.Annotate(withLevel(lErr, l.a), msg)
|
||||
}
|
||||
|
||||
func (l *logger) Errorf(f string, args ...interface{}) {
|
||||
l.l.Errorf(f, args)
|
||||
l.s.Annotatef(withLevel(lErr, l.a), f, args...)
|
||||
}
|
||||
|
||||
func (l *logger) Fatal(args ...interface{}) {
|
||||
if !l.s.IsRecordingEvents() {
|
||||
l.l.Fatal(args...)
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprint(args...)
|
||||
l.s.Annotate(withLevel(lFatal, l.a), msg)
|
||||
l.l.Fatal(msg)
|
||||
}
|
||||
|
||||
func (l *logger) Fatalf(f string, args ...interface{}) {
|
||||
l.s.Annotatef(withLevel(lFatal, l.a), f, args...)
|
||||
l.l.Fatalf(f, args)
|
||||
}
|
||||
|
||||
func (l *logger) WithError(err error) log.Logger {
|
||||
log := l.l.WithError(err)
|
||||
|
||||
var a []octrace.Attribute
|
||||
if l.s.IsRecordingEvents() {
|
||||
a = make([]octrace.Attribute, len(l.a), len(l.a)+1)
|
||||
copy(a, l.a)
|
||||
a = append(l.a, makeAttribute("err", err))
|
||||
}
|
||||
|
||||
return &logger{s: l.s, l: log, a: a}
|
||||
}
|
||||
|
||||
func (l *logger) WithField(k string, value interface{}) log.Logger {
|
||||
log := l.l.WithField(k, value)
|
||||
|
||||
var a []octrace.Attribute
|
||||
|
||||
if l.s.IsRecordingEvents() {
|
||||
a = make([]octrace.Attribute, len(l.a), len(l.a)+1)
|
||||
copy(a, l.a)
|
||||
a = append(a, makeAttribute(k, value))
|
||||
}
|
||||
|
||||
return &logger{s: l.s, a: a, l: log}
|
||||
}
|
||||
|
||||
func (l *logger) WithFields(fields log.Fields) log.Logger {
|
||||
log := l.l.WithFields(fields)
|
||||
|
||||
var a []octrace.Attribute
|
||||
if l.s.IsRecordingEvents() {
|
||||
a = make([]octrace.Attribute, len(l.a), len(l.a)+len(fields))
|
||||
copy(a, l.a)
|
||||
for k, v := range fields {
|
||||
a = append(a, makeAttribute(k, v))
|
||||
}
|
||||
}
|
||||
|
||||
return &logger{s: l.s, a: a, l: log}
|
||||
}
|
||||
|
||||
func makeAttribute(key string, val interface{}) octrace.Attribute {
|
||||
var attr octrace.Attribute
|
||||
|
||||
switch v := val.(type) {
|
||||
case string:
|
||||
attr = octrace.StringAttribute(key, v)
|
||||
case int64:
|
||||
attr = octrace.Int64Attribute(key, v)
|
||||
case bool:
|
||||
attr = octrace.BoolAttribute(key, v)
|
||||
case error:
|
||||
attr = octrace.StringAttribute(key, v.Error())
|
||||
default:
|
||||
attr = octrace.StringAttribute(key, fmt.Sprintf("%+v", val))
|
||||
}
|
||||
|
||||
return attr
|
||||
}
|
||||
|
||||
func withLevel(l string, attrs []octrace.Attribute) []octrace.Attribute {
|
||||
return append(attrs, octrace.StringAttribute("level", l))
|
||||
}
|
||||
13
trace/opencensus/opencensus_test.go
Normal file
13
trace/opencensus/opencensus_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/trace"
|
||||
)
|
||||
|
||||
func TestTracerImplementsTracer(t *testing.T) {
|
||||
// ensure that Adapter implements trace.Tracer
|
||||
if tt := trace.Tracer(Adapter{}); tt == nil {
|
||||
}
|
||||
}
|
||||
58
trace/trace.go
Normal file
58
trace/trace.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Package trace abstracts virtual-kubelet's tracing capabilties into a set of
|
||||
// interfaces.
|
||||
// While this does allow consumers to use whatever tracing library they want,
|
||||
// the primary goal is to share logging data between the configured logger and
|
||||
// tracing spans instead of duplicating calls.
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Status is an alias to opencensus's trace status.
|
||||
// The main reason we use this instead of implementing our own is library re-use,
|
||||
// namely for converting an error to a tracing status.
|
||||
// In the future this may be defined completely in this package.
|
||||
type Status = trace.Status
|
||||
|
||||
// Tracer is the interface used for creating a tracing span
|
||||
type Tracer interface {
|
||||
// StartSpan starts a new span. The span details are emebedded into the returned
|
||||
// context
|
||||
StartSpan(context.Context, string) (context.Context, Span)
|
||||
}
|
||||
|
||||
var (
|
||||
// T is the Tracer to use this should be initialized before starting up
|
||||
// virtual-kubelet
|
||||
T Tracer = nopTracer{}
|
||||
)
|
||||
|
||||
// StartSpan starts a span from the configured default tracer
|
||||
func StartSpan(ctx context.Context, name string) (context.Context, Span) {
|
||||
ctx, span := T.StartSpan(ctx, name)
|
||||
ctx = log.WithLogger(ctx, span.Logger())
|
||||
return ctx, span
|
||||
}
|
||||
|
||||
// Span encapsulates a tracing event
|
||||
type Span interface {
|
||||
End()
|
||||
SetStatus(Status)
|
||||
|
||||
// WithField and WithFields adds attributes to an entire span
|
||||
//
|
||||
// This interface is a bit weird, but allows us to manage loggers in the context
|
||||
// It is expected that implementations set `log.WithLogger` so the logger stored
|
||||
// in the context is updated with the new fields.
|
||||
WithField(context.Context, string, interface{}) context.Context
|
||||
WithFields(context.Context, log.Fields) context.Context
|
||||
|
||||
// Logger is used to log individual entries.
|
||||
// Calls to functions like `WithField` and `WithFields` on the logger should
|
||||
// not affect the rest of the span but rather individual entries.
|
||||
Logger() log.Logger
|
||||
}
|
||||
Reference in New Issue
Block a user