Skip to content

Commit

Permalink
Add integration tests for span processor ordering (#1208)
Browse files Browse the repository at this point in the history
- Test by span attributes added by processors in the order
  they were registered.

Signed-off-by: Hui Kang <kangh@us.ibm.com>

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
huikang and MrAlias authored Sep 28, 2020
1 parent 3a9f5fe commit a69f8fb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Renamed `SamplingDecision` values to comply with OpenTelemetry specification change. (#1192)
- Renamed Zipkin attribute names from `ot.status_code & ot.status_description` to `otel.status_code & otel.status_description`. (#1201)
- The default SDK now invokes registered `SpanProcessor`s in the order they were registered with the `TracerProvider`. (#1195)
- Add test of spans being processed by the `SpanProcessor`s in the order they were registered. (#1203)

### Removed

Expand Down
72 changes: 59 additions & 13 deletions sdk/trace/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,32 @@ import (
"context"
"testing"

"go.opentelemetry.io/otel/label"
export "go.opentelemetry.io/otel/sdk/export/trace"
)

type testSpanProcesor struct {
name string
spansStarted []*export.SpanData
spansEnded []*export.SpanData
shutdownCount int
}

func (t *testSpanProcesor) OnStart(s *export.SpanData) {
kv := label.KeyValue{
Key: "OnStart",
Value: label.StringValue(t.name),
}
s.Attributes = append(s.Attributes, kv)
t.spansStarted = append(t.spansStarted, s)
}

func (t *testSpanProcesor) OnEnd(s *export.SpanData) {
kv := label.KeyValue{
Key: "OnEnd",
Value: label.StringValue(t.name),
}
s.Attributes = append(s.Attributes, kv)
t.spansEnded = append(t.spansEnded, s)
}

Expand All @@ -45,10 +57,8 @@ func (t *testSpanProcesor) ForceFlush() {
func TestRegisterSpanProcessort(t *testing.T) {
name := "Register span processor before span starts"
tp := basicTracerProvider(t)
sps := []*testSpanProcesor{
NewTestSpanProcessor(),
NewTestSpanProcessor(),
}
spNames := []string{"sp1", "sp2", "sp3"}
sps := NewNamedTestSpanProcessors(spNames)

for _, sp := range sps {
tp.RegisterSpanProcessor(sp)
Expand All @@ -68,16 +78,29 @@ func TestRegisterSpanProcessort(t *testing.T) {
if gotCount != wantCount {
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount)
}

c := 0
for _, kv := range sp.spansStarted[0].Attributes {
if kv.Key != "OnStart" {
continue
}
gotValue := kv.Value.AsString()
if gotValue != spNames[c] {
t.Errorf("%s: ordered attributes: got %s, want %s\n", name, gotValue, spNames[c])
}
c++
}
if c != len(spNames) {
t.Errorf("%s: expected attributes(OnStart): got %d, want %d\n", name, c, len(spNames))
}
}
}

func TestUnregisterSpanProcessor(t *testing.T) {
name := "Start span after unregistering span processor"
tp := basicTracerProvider(t)
sps := []*testSpanProcesor{
NewTestSpanProcessor(),
NewTestSpanProcessor(),
}
spNames := []string{"sp1", "sp2", "sp3"}
sps := NewNamedTestSpanProcessors(spNames)

for _, sp := range sps {
tp.RegisterSpanProcessor(sp)
Expand Down Expand Up @@ -105,13 +128,28 @@ func TestUnregisterSpanProcessor(t *testing.T) {
if gotCount != wantCount {
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount)
}

c := 0
for _, kv := range sp.spansEnded[0].Attributes {
if kv.Key != "OnEnd" {
continue
}
gotValue := kv.Value.AsString()
if gotValue != spNames[c] {
t.Errorf("%s: ordered attributes: got %s, want %s\n", name, gotValue, spNames[c])
}
c++
}
if c != len(spNames) {
t.Errorf("%s: expected attributes(OnEnd): got %d, want %d\n", name, c, len(spNames))
}
}
}

func TestUnregisterSpanProcessorWhileSpanIsActive(t *testing.T) {
name := "Unregister span processor while span is active"
tp := basicTracerProvider(t)
sp := NewTestSpanProcessor()
sp := NewTestSpanProcessor("sp")
tp.RegisterSpanProcessor(sp)

tr := tp.Tracer("SpanProcessor")
Expand All @@ -136,7 +174,7 @@ func TestUnregisterSpanProcessorWhileSpanIsActive(t *testing.T) {
func TestSpanProcessorShutdown(t *testing.T) {
name := "Increment shutdown counter of a span processor"
tp := basicTracerProvider(t)
sp := NewTestSpanProcessor()
sp := NewTestSpanProcessor("sp")
if sp == nil {
t.Fatalf("Error creating new instance of TestSpanProcessor\n")
}
Expand All @@ -154,7 +192,7 @@ func TestSpanProcessorShutdown(t *testing.T) {
func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) {
name := "Increment shutdown counter after first UnregisterSpanProcessor call"
tp := basicTracerProvider(t)
sp := NewTestSpanProcessor()
sp := NewTestSpanProcessor("sp")
if sp == nil {
t.Fatalf("Error creating new instance of TestSpanProcessor\n")
}
Expand All @@ -178,6 +216,14 @@ func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) {
}
}

func NewTestSpanProcessor() *testSpanProcesor {
return &testSpanProcesor{}
func NewTestSpanProcessor(name string) *testSpanProcesor {
return &testSpanProcesor{name: name}
}

func NewNamedTestSpanProcessors(names []string) []*testSpanProcesor {
tsp := []*testSpanProcesor{}
for _, n := range names {
tsp = append(tsp, NewTestSpanProcessor(n))
}
return tsp
}

0 comments on commit a69f8fb

Please sign in to comment.