Skip to content

Commit

Permalink
Fix got span type is wrong error when creating exit span with trace…
Browse files Browse the repository at this point in the history
… sampling
  • Loading branch information
mrproliu committed Aug 20, 2024
1 parent 8abb20f commit 7cccecf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Release Notes.
### Bug Fixes
* Fix panic error when root span finished.
* Fix when not route is found, the gin operation name is "http.Method:", example: "GET:".
* Fix got `span type is wrong` error when creating exit span with trace sampling.

0.4.0
------------------
Expand Down
22 changes: 14 additions & 8 deletions plugins/core/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package core

import (
"fmt"
"reflect"
"runtime/debug"

Expand Down Expand Up @@ -62,7 +63,8 @@ func (t *Tracer) CreateEntrySpan(operationName string, extractor interface{}, op
ref = nil
}

return t.createSpan0(ctx, tracingSpan, opts, withRef(ref), withSpanType(SpanTypeEntry), withOperationName(operationName))
span, _, err := t.createSpan0(ctx, tracingSpan, opts, withRef(ref), withSpanType(SpanTypeEntry), withOperationName(operationName))
return span, err
}

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

return t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeLocal), withOperationName(operationName))
span, _, err := t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeLocal), withOperationName(operationName))
return span, err
}

func (t *Tracer) CreateExitSpan(operationName, peer string, injector interface{}, opts ...interface{}) (s interface{}, err error) {
Expand All @@ -90,14 +93,17 @@ func (t *Tracer) CreateExitSpan(operationName, peer string, injector interface{}
if tracingSpan != nil && tracingSpan.IsExit() && reflect.ValueOf(tracingSpan).Type() != snapshotType {
return tracingSpan, nil
}
span, err := t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeExit), withOperationName(operationName), withPeer(peer))
span, noop, err := t.createSpan0(ctx, tracingSpan, opts, withSpanType(SpanTypeExit), withOperationName(operationName), withPeer(peer))
if err != nil {
return nil, err
}
if noop {
return span, nil
}
spanContext := &SpanContext{}
reportedSpan, ok := span.(SegmentSpan)
if !ok {
return nil, errors.New("span type is wrong")
return nil, errors.New(fmt.Sprintf("span type is wrong: %T", span))
}

firstSpan := reportedSpan.GetSegmentContext().FirstSpan
Expand Down Expand Up @@ -246,7 +252,7 @@ func (t *Tracer) createNoop(operationName string) (*TracingContext, TracingSpan,
return ctx, nil, false
}

func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts []interface{}, coreOpts ...interface{}) (s TracingSpan, err error) {
func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts []interface{}, coreOpts ...interface{}) (s TracingSpan, noop bool, err error) {
ds := NewDefaultSpan(t, parent)
var parentSpan SegmentSpan
if parent != nil {
Expand All @@ -262,7 +268,7 @@ func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts
sampled := t.Sampler.IsSampled(ds.OperationName)
if !sampled {
// Filter by sample just return noop span
return newNoopSpan(), nil
return newNoopSpan(), true, nil
}
}
// process the opts from agent core for prepare building segment span
Expand All @@ -271,13 +277,13 @@ func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts
}
s, err = NewSegmentSpan(ctx, ds, parentSpan)
if err != nil {
return nil, err
return nil, false, err
}
// process the opts from plugin, split opts because the DefaultSpan not contains the tracing context information(AdaptSpan)
for _, opt := range pluginOpts {
opt.(tracing.SpanOption).Apply(s)
}
return s, nil
return s, false, nil
}

func withSpanType(spanType SpanType) tracing.SpanOption {
Expand Down

0 comments on commit 7cccecf

Please sign in to comment.