1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-04 09:43:23 +02:00

Add integration tests for span processor ordering (#1208)

- 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>
This commit is contained in:
huikang 2020-09-28 12:29:14 -04:00 committed by GitHub
parent 3a9f5fe15f
commit a69f8fbe7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 13 deletions

View File

@ -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

View File

@ -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)
}
@ -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)
@ -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)
@ -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")
@ -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")
}
@ -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")
}
@ -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
}