Skip to content

Commit 91d8895

Browse files
authored
Fix got span type is wrong error when creating exit span with trace sampling (#193)
1 parent 8abb20f commit 91d8895

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Release Notes.
2222
### Bug Fixes
2323
* Fix panic error when root span finished.
2424
* Fix when not route is found, the gin operation name is "http.Method:", example: "GET:".
25+
* Fix got `span type is wrong` error when creating exit span with trace sampling.
2526

2627
0.4.0
2728
------------------

plugins/core/tracing.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package core
1919

2020
import (
21+
"fmt"
2122
"reflect"
2223
"runtime/debug"
2324

@@ -55,14 +56,15 @@ func (t *Tracer) CreateEntrySpan(operationName string, extractor interface{}, op
5556
return tracingSpan, nil
5657
}
5758
var ref = &SpanContext{}
58-
if err := ref.Decode(extractor.(tracing.ExtractorWrapper).Fun()); err != nil {
59-
return nil, err
59+
if err1 := ref.Decode(extractor.(tracing.ExtractorWrapper).Fun()); err1 != nil {
60+
return nil, err1
6061
}
6162
if !ref.Valid {
6263
ref = nil
6364
}
6465

65-
return t.createSpan0(ctx, tracingSpan, opts, withRef(ref), withSpanType(SpanTypeEntry), withOperationName(operationName))
66+
span, _, err := t.createSpan0(ctx, tracingSpan, opts, withRef(ref), withSpanType(SpanTypeEntry), withOperationName(operationName))
67+
return span, err
6668
}
6769

6870
func (t *Tracer) CreateLocalSpan(operationName string, opts ...interface{}) (s interface{}, err error) {
@@ -74,7 +76,8 @@ func (t *Tracer) CreateLocalSpan(operationName string, opts ...interface{}) (s i
7476
saveSpanToActiveIfNotError(ctx, s, err)
7577
}()
7678

77-
return t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeLocal), withOperationName(operationName))
79+
span, _, err := t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeLocal), withOperationName(operationName))
80+
return span, err
7881
}
7982

8083
func (t *Tracer) CreateExitSpan(operationName, peer string, injector interface{}, opts ...interface{}) (s interface{}, err error) {
@@ -90,14 +93,17 @@ func (t *Tracer) CreateExitSpan(operationName, peer string, injector interface{}
9093
if tracingSpan != nil && tracingSpan.IsExit() && reflect.ValueOf(tracingSpan).Type() != snapshotType {
9194
return tracingSpan, nil
9295
}
93-
span, err := t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeExit), withOperationName(operationName), withPeer(peer))
96+
span, noop, err := t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeExit), withOperationName(operationName), withPeer(peer))
9497
if err != nil {
9598
return nil, err
9699
}
100+
if noop {
101+
return span, nil
102+
}
97103
spanContext := &SpanContext{}
98104
reportedSpan, ok := span.(SegmentSpan)
99105
if !ok {
100-
return nil, errors.New("span type is wrong")
106+
return nil, errors.New(fmt.Sprintf("span type is wrong: %T", span))
101107
}
102108

103109
firstSpan := reportedSpan.GetSegmentContext().FirstSpan
@@ -246,7 +252,8 @@ func (t *Tracer) createNoop(operationName string) (*TracingContext, TracingSpan,
246252
return ctx, nil, false
247253
}
248254

249-
func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts []interface{}, coreOpts ...interface{}) (s TracingSpan, err error) {
255+
func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts []interface{},
256+
coreOpts ...interface{}) (s TracingSpan, noop bool, err error) {
250257
ds := NewDefaultSpan(t, parent)
251258
var parentSpan SegmentSpan
252259
if parent != nil {
@@ -262,7 +269,7 @@ func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts
262269
sampled := t.Sampler.IsSampled(ds.OperationName)
263270
if !sampled {
264271
// Filter by sample just return noop span
265-
return newNoopSpan(), nil
272+
return newNoopSpan(), true, nil
266273
}
267274
}
268275
// process the opts from agent core for prepare building segment span
@@ -271,13 +278,13 @@ func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts
271278
}
272279
s, err = NewSegmentSpan(ctx, ds, parentSpan)
273280
if err != nil {
274-
return nil, err
281+
return nil, false, err
275282
}
276283
// process the opts from plugin, split opts because the DefaultSpan not contains the tracing context information(AdaptSpan)
277284
for _, opt := range pluginOpts {
278285
opt.(tracing.SpanOption).Apply(s)
279286
}
280-
return s, nil
287+
return s, false, nil
281288
}
282289

283290
func withSpanType(spanType SpanType) tracing.SpanOption {

0 commit comments

Comments
 (0)