You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/pkg/net/trace/dapper_test.go

137 lines
3.8 KiB

6 years ago
package trace
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
)
type mockReport struct {
6 years ago
sps []*Span
6 years ago
}
6 years ago
func (m *mockReport) WriteSpan(sp *Span) error {
6 years ago
m.sps = append(m.sps, sp)
return nil
}
func (m *mockReport) Close() error {
return nil
}
func TestDapperPropagation(t *testing.T) {
t.Run("test HTTP progagation", func(t *testing.T) {
report := &mockReport{}
6 years ago
t1 := NewTracer("service1", report, true)
t2 := NewTracer("service2", report, true)
6 years ago
sp1 := t1.New("opt_1")
sp2 := sp1.Fork("", "opt_client")
header := make(http.Header)
t1.Inject(sp2, HTTPFormat, header)
sp3, err := t2.Extract(HTTPFormat, header)
if err != nil {
t.Fatal(err)
}
sp3.Finish(nil)
sp2.Finish(nil)
sp1.Finish(nil)
assert.Len(t, report.sps, 3)
6 years ago
assert.Equal(t, report.sps[2].context.ParentID, uint64(0))
assert.Equal(t, report.sps[0].context.TraceID, report.sps[1].context.TraceID)
assert.Equal(t, report.sps[2].context.TraceID, report.sps[1].context.TraceID)
6 years ago
6 years ago
assert.Equal(t, report.sps[1].context.ParentID, report.sps[2].context.SpanID)
assert.Equal(t, report.sps[0].context.ParentID, report.sps[1].context.SpanID)
6 years ago
})
t.Run("test gRPC progagation", func(t *testing.T) {
report := &mockReport{}
6 years ago
t1 := NewTracer("service1", report, true)
t2 := NewTracer("service2", report, true)
6 years ago
sp1 := t1.New("opt_1")
sp2 := sp1.Fork("", "opt_client")
md := make(metadata.MD)
t1.Inject(sp2, GRPCFormat, md)
sp3, err := t2.Extract(GRPCFormat, md)
if err != nil {
t.Fatal(err)
}
sp3.Finish(nil)
sp2.Finish(nil)
sp1.Finish(nil)
assert.Len(t, report.sps, 3)
6 years ago
assert.Equal(t, report.sps[2].context.ParentID, uint64(0))
assert.Equal(t, report.sps[0].context.TraceID, report.sps[1].context.TraceID)
assert.Equal(t, report.sps[2].context.TraceID, report.sps[1].context.TraceID)
6 years ago
6 years ago
assert.Equal(t, report.sps[1].context.ParentID, report.sps[2].context.SpanID)
assert.Equal(t, report.sps[0].context.ParentID, report.sps[1].context.SpanID)
6 years ago
})
t.Run("test normal", func(t *testing.T) {
report := &mockReport{}
6 years ago
t1 := NewTracer("service1", report, true)
6 years ago
sp1 := t1.New("test123")
sp1.Finish(nil)
})
t.Run("test debug progagation", func(t *testing.T) {
report := &mockReport{}
6 years ago
t1 := NewTracer("service1", report, true)
t2 := NewTracer("service2", report, true)
6 years ago
sp1 := t1.New("opt_1", EnableDebug())
sp2 := sp1.Fork("", "opt_client")
header := make(http.Header)
t1.Inject(sp2, HTTPFormat, header)
sp3, err := t2.Extract(HTTPFormat, header)
if err != nil {
t.Fatal(err)
}
sp3.Finish(nil)
sp2.Finish(nil)
sp1.Finish(nil)
assert.Len(t, report.sps, 3)
6 years ago
assert.Equal(t, report.sps[2].context.ParentID, uint64(0))
assert.Equal(t, report.sps[0].context.TraceID, report.sps[1].context.TraceID)
assert.Equal(t, report.sps[2].context.TraceID, report.sps[1].context.TraceID)
6 years ago
6 years ago
assert.Equal(t, report.sps[1].context.ParentID, report.sps[2].context.SpanID)
assert.Equal(t, report.sps[0].context.ParentID, report.sps[1].context.SpanID)
6 years ago
})
}
func BenchmarkSample(b *testing.B) {
err := fmt.Errorf("test error")
report := &mockReport{}
6 years ago
t1 := NewTracer("service1", report, true)
6 years ago
for i := 0; i < b.N; i++ {
sp1 := t1.New("test_opt1")
sp1.SetTag(TagString("test", "123"))
sp2 := sp1.Fork("", "opt2")
sp3 := sp2.Fork("", "opt3")
sp3.SetTag(TagString("test", "123"))
sp3.Finish(nil)
sp2.Finish(&err)
sp1.Finish(nil)
}
}
func BenchmarkDisableSample(b *testing.B) {
err := fmt.Errorf("test error")
report := &mockReport{}
6 years ago
t1 := NewTracer("service1", report, true)
6 years ago
for i := 0; i < b.N; i++ {
sp1 := t1.New("test_opt1")
sp1.SetTag(TagString("test", "123"))
sp2 := sp1.Fork("", "opt2")
sp3 := sp2.Fork("", "opt3")
sp3.SetTag(TagString("test", "123"))
sp3.Finish(nil)
sp2.Finish(&err)
sp1.Finish(nil)
}
}