Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
add CreateExitSpanWithContext (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
kagaya85 authored Aug 8, 2021
1 parent ddc26b2 commit b16c9f0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 13 deletions.
29 changes: 16 additions & 13 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ func (t *Tracer) CreateEntrySpan(ctx context.Context, operationName string, extr
refSc = nil
}
s, nCtx, err = t.CreateLocalSpan(ctx, WithContext(refSc), WithSpanType(SpanTypeEntry), WithOperationName(operationName))
if err != nil {
return
}
return
}

Expand Down Expand Up @@ -138,27 +135,33 @@ func (t *Tracer) CreateLocalSpan(ctx context.Context, opts ...SpanOption) (s Spa
}

// CreateExitSpan creates and starts an exit span for client
func (t *Tracer) CreateExitSpan(ctx context.Context, operationName string, peer string, injector propagation.Injector) (Span, error) {
func (t *Tracer) CreateExitSpan(ctx context.Context, operationName string, peer string, injector propagation.Injector) (s Span, err error) {
s, _, err = t.CreateExitSpanWithContext(ctx, operationName, peer, injector)
return
}

// CreateExitSpanWithContext creates and starts an exit span for client with context
func (t *Tracer) CreateExitSpanWithContext(ctx context.Context, operationName string, peer string, injector propagation.Injector) (s Span, nCtx context.Context, err error) {
if ctx == nil || operationName == "" || peer == "" || injector == nil {
return nil, errParameter
return nil, nil, errParameter
}
if s, _ := t.createNoop(ctx); s != nil {
return s, nil
if s, nCtx = t.createNoop(ctx); s != nil {
return
}
s, _, err := t.CreateLocalSpan(ctx, WithSpanType(SpanTypeExit), WithOperationName(operationName))
s, nCtx, err = t.CreateLocalSpan(ctx, WithSpanType(SpanTypeExit), WithOperationName(operationName))
if err != nil {
return nil, err
return
}
noopSpan, ok := interface{}(s).(NoopSpan)
if ok {
// Ignored, there is no need to inject SW8 in the request header
return &noopSpan, nil
return &noopSpan, nCtx, nil
}
s.SetPeer(peer)
spanContext := &propagation.SpanContext{}
span, ok := s.(ReportedSpan)
if !ok {
return nil, errors.New("span type is wrong")
return nil, nil, errors.New("span type is wrong")
}

firstSpan := span.Context().FirstSpan
Expand All @@ -174,9 +177,9 @@ func (t *Tracer) CreateExitSpan(ctx context.Context, operationName string, peer

err = spanContext.Encode(injector)
if err != nil {
return nil, err
return nil, nil, err
}
return s, nil
return
}

func (t *Tracer) createNoop(ctx context.Context) (s Span, nCtx context.Context) {
Expand Down
81 changes: 81 additions & 0 deletions trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,84 @@ func TestTracer_CreateExitSpan_Parameter(t *testing.T) {
})
}
}

func TestTracer_CreateExitSpanWithContext_Parameter(t *testing.T) {
type args struct {
ctx context.Context
operationName string
peer string
injector propagation.Injector
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"context is nil",
args{
ctx: nil,
operationName: "query type",
peer: "localhost:8080",
injector: func(key, value string) error { return nil },
},
true,
},
{
"OperationName is nil",
args{
ctx: context.Background(),
operationName: "",
peer: "localhost:8080",
injector: func(key, value string) error { return nil },
},
true,
},
{
"Peer is nil",
args{
ctx: context.Background(),
operationName: "query type",
peer: "",
injector: func(key, value string) error { return nil },
},
true,
},
{
"injector is nil",
args{
ctx: context.Background(),
operationName: "",
peer: "localhost:8080",
injector: nil,
},
true,
},
{
"normal",
args{
ctx: context.Background(),
operationName: "query type",
peer: "localhost:8080",
injector: func(key, value string) error { return nil },
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tracer := Tracer{}
_, ctx, err := tracer.CreateExitSpanWithContext(tt.args.ctx, tt.args.operationName, tt.args.peer, tt.args.injector)
if (err != nil) != tt.wantErr {
t.Errorf("Tracer.CreateExitSpan() error = %v, wantErr %v", err, tt.wantErr)
return
}
if ctx != nil {
if id := SpanID(ctx); id != EmptySpanID {
t.Error("Span ID should not be Empty")
return
}
}
})
}
}

0 comments on commit b16c9f0

Please sign in to comment.