Remove usage of ocstatus package

This changes the tracing package to accept an error on SetStatus, which
is really what we always want anyway.
This also decouples the trace package from opencensus.
This commit is contained in:
Brian Goff
2019-05-20 14:33:37 -07:00
parent 02623170cc
commit d6b5ae3710
10 changed files with 59 additions and 47 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"github.com/virtual-kubelet/virtual-kubelet/log"
"go.opencensus.io/trace"
)
type nopTracer struct{}
@@ -15,9 +14,9 @@ func (nopTracer) StartSpan(ctx context.Context, _ string) (context.Context, Span
type nopSpan struct{}
func (nopSpan) End() {}
func (nopSpan) SetStatus(trace.Status) {}
func (nopSpan) Logger() log.Logger { return nil }
func (nopSpan) End() {}
func (nopSpan) SetStatus(error) {}
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 }

View File

@@ -9,6 +9,7 @@ import (
"fmt"
"sync"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/trace"
octrace "go.opencensus.io/trace"
@@ -46,7 +47,30 @@ func (s *span) End() {
s.s.End()
}
func (s *span) SetStatus(status trace.Status) {
func (s *span) SetStatus(err error) {
if !s.s.IsRecordingEvents() {
return
}
var status octrace.Status
if err == nil {
status.Code = octrace.StatusCodeOK
s.s.SetStatus(status)
return
}
switch {
case errdefs.IsNotFound(err):
status.Code = octrace.StatusCodeNotFound
case errdefs.IsInvalidInput(err):
status.Code = octrace.StatusCodeInvalidArgument
// TODO: other error types
default:
status.Code = octrace.StatusCodeUnknown
}
status.Message = err.Error()
s.s.SetStatus(status)
}

View File

@@ -9,15 +9,8 @@ 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
@@ -41,7 +34,13 @@ func StartSpan(ctx context.Context, name string) (context.Context, Span) {
// Span encapsulates a tracing event
type Span interface {
End()
SetStatus(Status)
// SetStatus sets the final status of the span.
// errors passed to this should use interfaces defined in
// github.com/virtual-kubelet/virtual-kubelet/errdefs
//
// If the error is nil, the span should be considered successful.
SetStatus(err error)
// WithField and WithFields adds attributes to an entire span
//