| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
"go.opentelemetry.io/otel/trace" |
| 12 |
) |
| 13 |
|
| 14 |
type testSpanProcessor struct { |
| 15 |
name string |
| 16 |
spansStarted []ReadWriteSpan |
| 17 |
spansEnded []ReadOnlySpan |
| 18 |
shutdownCount int |
| 19 |
} |
| 20 |
|
| 21 |
func (t *testSpanProcessor) OnStart(parent context.Context, s ReadWriteSpan) { |
| 22 |
if t == nil { |
| 23 |
return |
| 24 |
} |
| 25 |
psc := trace.SpanContextFromContext(parent) |
| 26 |
kv := []attribute.KeyValue{ |
| 27 |
{ |
| 28 |
Key: "SpanProcessorName", |
| 29 |
Value: attribute.StringValue(t.name), |
| 30 |
}, |
| 31 |
// Store parent trace ID and span ID as attributes to be read later in |
| 32 |
// tests so that we "do something" with the parent argument. Real |
| 33 |
// SpanProcessor implementations will likely use the parent argument in |
| 34 |
// a more meaningful way. |
| 35 |
{ |
| 36 |
Key: "ParentTraceID", |
| 37 |
Value: attribute.StringValue(psc.TraceID().String()), |
| 38 |
}, |
| 39 |
{ |
| 40 |
Key: "ParentSpanID", |
| 41 |
Value: attribute.StringValue(psc.SpanID().String()), |
| 42 |
}, |
| 43 |
} |
| 44 |
s.AddEvent("OnStart", trace.WithAttributes(kv...)) |
| 45 |
t.spansStarted = append(t.spansStarted, s) |
| 46 |
} |
| 47 |
|
| 48 |
func (t *testSpanProcessor) OnEnd(s ReadOnlySpan) { |
| 49 |
if t == nil { |
| 50 |
return |
| 51 |
} |
| 52 |
t.spansEnded = append(t.spansEnded, s) |
| 53 |
} |
| 54 |
|
| 55 |
func (t *testSpanProcessor) Shutdown(context.Context) error { |
| 56 |
if t == nil { |
| 57 |
return nil |
| 58 |
} |
| 59 |
t.shutdownCount++ |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
func (t *testSpanProcessor) ForceFlush(context.Context) error { |
| 64 |
if t == nil { |
| 65 |
return nil |
| 66 |
} |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
func TestRegisterSpanProcessor(t *testing.T) { |
| 71 |
name := "Register span processor before span starts" |
| 72 |
tp := basicTracerProvider(t) |
| 73 |
spNames := []string{"sp1", "sp2", "sp3"} |
| 74 |
sps := NewNamedTestSpanProcessors(spNames) |
| 75 |
|
| 76 |
for _, sp := range sps { |
| 77 |
tp.RegisterSpanProcessor(sp) |
| 78 |
} |
| 79 |
|
| 80 |
tid, _ := trace.TraceIDFromHex("01020304050607080102040810203040") |
| 81 |
sid, _ := trace.SpanIDFromHex("0102040810203040") |
| 82 |
parent := trace.NewSpanContext(trace.SpanContextConfig{ |
| 83 |
TraceID: tid, |
| 84 |
SpanID: sid, |
| 85 |
}) |
| 86 |
ctx := trace.ContextWithRemoteSpanContext(t.Context(), parent) |
| 87 |
|
| 88 |
tr := tp.Tracer("SpanProcessor") |
| 89 |
_, span := tr.Start(ctx, "OnStart") |
| 90 |
span.End() |
| 91 |
wantCount := 1 |
| 92 |
|
| 93 |
for _, sp := range sps { |
| 94 |
gotCount := len(sp.spansStarted) |
| 95 |
if gotCount != wantCount { |
| 96 |
t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount) |
| 97 |
} |
| 98 |
gotCount = len(sp.spansEnded) |
| 99 |
if gotCount != wantCount { |
| 100 |
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount) |
| 101 |
} |
| 102 |
|
| 103 |
c := 0 |
| 104 |
tidOK := false |
| 105 |
sidOK := false |
| 106 |
for _, e := range sp.spansStarted[0].Events() { |
| 107 |
for _, kv := range e.Attributes { |
| 108 |
switch kv.Key { |
| 109 |
case "SpanProcessorName": |
| 110 |
gotValue := kv.Value.AsString() |
| 111 |
if gotValue != spNames[c] { |
| 112 |
t.Errorf("%s: attributes: got %s, want %s\n", name, gotValue, spNames[c]) |
| 113 |
} |
| 114 |
c++ |
| 115 |
case "ParentTraceID": |
| 116 |
gotValue := kv.Value.AsString() |
| 117 |
if gotValue != parent.TraceID().String() { |
| 118 |
t.Errorf("%s: attributes: got %s, want %s\n", name, gotValue, parent.TraceID()) |
| 119 |
} |
| 120 |
tidOK = true |
| 121 |
case "ParentSpanID": |
| 122 |
gotValue := kv.Value.AsString() |
| 123 |
if gotValue != parent.SpanID().String() { |
| 124 |
t.Errorf("%s: attributes: got %s, want %s\n", name, gotValue, parent.SpanID()) |
| 125 |
} |
| 126 |
sidOK = true |
| 127 |
default: |
| 128 |
continue |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
if c != len(spNames) { |
| 133 |
t.Errorf("%s: expected attributes(SpanProcessorName): got %d, want %d\n", name, c, len(spNames)) |
| 134 |
} |
| 135 |
if !tidOK { |
| 136 |
t.Errorf("%s: expected attributes(ParentTraceID)\n", name) |
| 137 |
} |
| 138 |
if !sidOK { |
| 139 |
t.Errorf("%s: expected attributes(ParentSpanID)\n", name) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func TestUnregisterSpanProcessor(t *testing.T) { |
| 145 |
name := "Start span after unregistering span processor" |
| 146 |
tp := basicTracerProvider(t) |
| 147 |
spNames := []string{"sp1", "sp2", "sp3"} |
| 148 |
sps := NewNamedTestSpanProcessors(spNames) |
| 149 |
|
| 150 |
for _, sp := range sps { |
| 151 |
tp.RegisterSpanProcessor(sp) |
| 152 |
} |
| 153 |
|
| 154 |
tr := tp.Tracer("SpanProcessor") |
| 155 |
_, span := tr.Start(t.Context(), "OnStart") |
| 156 |
span.End() |
| 157 |
for _, sp := range sps { |
| 158 |
tp.UnregisterSpanProcessor(sp) |
| 159 |
} |
| 160 |
|
| 161 |
// start another span after unregistering span processor. |
| 162 |
_, span = tr.Start(t.Context(), "Start span after unregister") |
| 163 |
span.End() |
| 164 |
|
| 165 |
for _, sp := range sps { |
| 166 |
wantCount := 1 |
| 167 |
gotCount := len(sp.spansStarted) |
| 168 |
if gotCount != wantCount { |
| 169 |
t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount) |
| 170 |
} |
| 171 |
|
| 172 |
gotCount = len(sp.spansEnded) |
| 173 |
if gotCount != wantCount { |
| 174 |
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount) |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestUnregisterSpanProcessorWhileSpanIsActive(t *testing.T) { |
| 180 |
name := "Unregister span processor while span is active" |
| 181 |
tp := basicTracerProvider(t) |
| 182 |
sp := NewTestSpanProcessor("sp") |
| 183 |
tp.RegisterSpanProcessor(sp) |
| 184 |
|
| 185 |
tr := tp.Tracer("SpanProcessor") |
| 186 |
_, span := tr.Start(t.Context(), "OnStart") |
| 187 |
tp.UnregisterSpanProcessor(sp) |
| 188 |
|
| 189 |
span.End() |
| 190 |
|
| 191 |
wantCount := 1 |
| 192 |
gotCount := len(sp.spansStarted) |
| 193 |
if gotCount != wantCount { |
| 194 |
t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount) |
| 195 |
} |
| 196 |
|
| 197 |
wantCount = 0 |
| 198 |
gotCount = len(sp.spansEnded) |
| 199 |
if gotCount != wantCount { |
| 200 |
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func TestSpanProcessorShutdown(t *testing.T) { |
| 205 |
name := "Increment shutdown counter of a span processor" |
| 206 |
tp := basicTracerProvider(t) |
| 207 |
sp := NewTestSpanProcessor("sp") |
| 208 |
tp.RegisterSpanProcessor(sp) |
| 209 |
|
| 210 |
wantCount := 1 |
| 211 |
err := sp.Shutdown(t.Context()) |
| 212 |
if err != nil { |
| 213 |
t.Error("Error shutting the testSpanProcessor down\n") |
| 214 |
} |
| 215 |
|
| 216 |
gotCount := sp.shutdownCount |
| 217 |
if wantCount != gotCount { |
| 218 |
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) { |
| 223 |
name := "Increment shutdown counter after first UnregisterSpanProcessor call" |
| 224 |
tp := basicTracerProvider(t) |
| 225 |
sp := NewTestSpanProcessor("sp") |
| 226 |
|
| 227 |
wantCount := 1 |
| 228 |
|
| 229 |
tp.RegisterSpanProcessor(sp) |
| 230 |
tp.UnregisterSpanProcessor(sp) |
| 231 |
|
| 232 |
gotCount := sp.shutdownCount |
| 233 |
if wantCount != gotCount { |
| 234 |
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount) |
| 235 |
} |
| 236 |
|
| 237 |
// Multiple UnregisterSpanProcessor should not trigger multiple Shutdown calls. |
| 238 |
tp.UnregisterSpanProcessor(sp) |
| 239 |
|
| 240 |
gotCount = sp.shutdownCount |
| 241 |
if wantCount != gotCount { |
| 242 |
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func NewTestSpanProcessor(name string) *testSpanProcessor { |
| 247 |
return &testSpanProcessor{name: name} |
| 248 |
} |
| 249 |
|
| 250 |
func NewNamedTestSpanProcessors(names []string) []*testSpanProcessor { |
| 251 |
tsp := []*testSpanProcessor{} |
| 252 |
for _, n := range names { |
| 253 |
tsp = append(tsp, NewTestSpanProcessor(n)) |
| 254 |
} |
| 255 |
return tsp |
| 256 |
} |