Skip to content

Commit

Permalink
ddtrace/opentracer: translate Datadog errors to OpenTracing errors
Browse files Browse the repository at this point in the history
This change fixes so that the OpenTracing implementation returns standard errors
defined in opentracing-go, as opposed to returning errors defined in ddtrace/tracer.

Fixes #998
  • Loading branch information
Kristoffer Rosén committed Sep 16, 2021
1 parent f86a82b commit e2c2612
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ddtrace/opentracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (t *opentracer) Inject(ctx opentracing.SpanContext, format interface{}, car
}
switch format {
case opentracing.TextMap, opentracing.HTTPHeaders:
return t.Tracer.Inject(sctx, carrier)
return translateError(t.Tracer.Inject(sctx, carrier))
default:
return opentracing.ErrUnsupportedFormat
}
Expand All @@ -83,7 +83,8 @@ func (t *opentracer) Inject(ctx opentracing.SpanContext, format interface{}, car
func (t *opentracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error) {
switch format {
case opentracing.TextMap, opentracing.HTTPHeaders:
return t.Tracer.Extract(carrier)
sctx, err := t.Tracer.Extract(carrier)
return sctx, translateError(err)
default:
return nil, opentracing.ErrUnsupportedFormat
}
Expand All @@ -99,3 +100,18 @@ func (t *opentracer) ContextWithSpanHook(ctx context.Context, openSpan opentraci
}
return tracer.ContextWithSpan(ctx, ddSpan.Span)
}

func translateError(err error) error {
switch err {
case tracer.ErrSpanContextNotFound:
return opentracing.ErrSpanContextNotFound
case tracer.ErrInvalidCarrier:
return opentracing.ErrInvalidCarrier
case tracer.ErrInvalidSpanContext:
return opentracing.ErrInvalidSpanContext
case tracer.ErrSpanContextCorrupted:
return opentracing.ErrSpanContextCorrupted
default:
return err
}
}
18 changes: 18 additions & 0 deletions ddtrace/opentracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package opentracer

import (
"context"
"errors"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
Expand Down Expand Up @@ -37,3 +38,20 @@ func TestSpanWithContext(t *testing.T) {
assert.True(ok)
assert.Equal(got, want.(*span).Span)
}

func TestTranslateError(t *testing.T) {
for name, tt := range map[string]struct {
in, out error
}{
"nil": {in: nil, out: nil},
"unrecognized": {in: errors.New("unrecognized"), out: errors.New("unrecognized")},
"ErrSpanContextNotFound": {in: tracer.ErrSpanContextNotFound, out: opentracing.ErrSpanContextNotFound},
"ErrInvalidCarrier": {in: tracer.ErrInvalidCarrier, out: opentracing.ErrInvalidCarrier},
"ErrInvalidSpanContext": {in: tracer.ErrInvalidSpanContext, out: opentracing.ErrInvalidSpanContext},
"ErrSpanContextCorrupted": {in: tracer.ErrSpanContextCorrupted, out: opentracing.ErrSpanContextCorrupted},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tt.out, translateError(tt.in))
})
}
}

0 comments on commit e2c2612

Please sign in to comment.