Skip to content

Commit

Permalink
Call otel.Handle with non-nil errors (#1384)
Browse files Browse the repository at this point in the history
* Call otel.Handle with non-nil errors

That's what normally happens in other call sites. Those two didn't
check it, but passed the "error" to Handle. The default, delegating
implementation of ErrorHandler was printing the error unconditionally,
which resulted in pointless lines like `2020/12/07 19:51:28 <nil>` in
demos, for example.

* Add tests

Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
krnowak and MrAlias authored Dec 11, 2020
1 parent c3c4273 commit af114ba
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
8 changes: 6 additions & 2 deletions sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func (p *TracerProvider) UnregisterSpanProcessor(s SpanProcessor) {
}
if stopOnce != nil {
stopOnce.state.Do(func() {
otel.Handle(s.Shutdown(context.Background()))
if err := s.Shutdown(context.Background()); err != nil {
otel.Handle(err)
}
})
}
if len(new) > 1 {
Expand Down Expand Up @@ -192,7 +194,9 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error {

for _, sps := range spss {
sps.state.Do(func() {
otel.Handle(sps.sp.Shutdown(ctx))
if err := sps.sp.Shutdown(ctx); err != nil {
otel.Handle(err)
}
})
}
return nil
Expand Down
41 changes: 39 additions & 2 deletions sdk/trace/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ package trace

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

type basicSpanProcesor struct {
running bool
running bool
injectShutdownError error
}

func (t *basicSpanProcesor) Shutdown(context.Context) error {
t.running = false
return nil
return t.injectShutdownError
}

func (t *basicSpanProcesor) OnStart(parent context.Context, s ReadWriteSpan) {}
Expand All @@ -45,3 +49,36 @@ func TestShutdownTraceProvider(t *testing.T) {
t.Errorf("Error shutdown basicSpanProcesor\n")
}
}

func TestFailedProcessorShutdown(t *testing.T) {
handler.Reset()
stp := NewTracerProvider()
spErr := errors.New("basic span processor shutdown failure")
sp := &basicSpanProcesor{
running: true,
injectShutdownError: spErr,
}
stp.RegisterSpanProcessor(sp)

_ = stp.Shutdown(context.Background())

assert.Contains(t, handler.errs, spErr)
}

func TestFailedProcessorShutdownInUnregister(t *testing.T) {
handler.Reset()
stp := NewTracerProvider()
spErr := errors.New("basic span processor shutdown failure")
sp := &basicSpanProcesor{
running: true,
injectShutdownError: spErr,
}
stp.RegisterSpanProcessor(sp)
stp.UnregisterSpanProcessor(sp)

assert.Contains(t, handler.errs, spErr)

handler.errs = nil
_ = stp.Shutdown(context.Background())
assert.Empty(t, handler.errs)
}
20 changes: 15 additions & 5 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,30 @@ import (
"go.opentelemetry.io/otel/sdk/resource"
)

type storingHandler struct {
errs []error
}

func (s *storingHandler) Handle(err error) {
s.errs = append(s.errs, err)
}

func (s *storingHandler) Reset() {
s.errs = nil
}

var (
tid trace.TraceID
sid trace.SpanID
)

type discardHandler struct{}

func (*discardHandler) Handle(_ error) {}
handler *storingHandler = &storingHandler{}
)

func init() {
tid, _ = trace.TraceIDFromHex("01020304050607080102040810203040")
sid, _ = trace.SpanIDFromHex("0102040810203040")

otel.SetErrorHandler(new(discardHandler))
otel.SetErrorHandler(handler)
}

func TestTracerFollowsExpectedAPIBehaviour(t *testing.T) {
Expand Down

0 comments on commit af114ba

Please sign in to comment.