2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 08:05:28 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
package trace_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-12-11 07:15:44 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2020-11-16 18:45:49 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-09-16 22:58:15 +02:00
|
|
|
)
|
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
type testSpanProcessor struct {
|
2020-09-28 18:29:14 +02:00
|
|
|
name string
|
2020-12-11 07:15:44 +02:00
|
|
|
spansStarted []sdktrace.ReadWriteSpan
|
|
|
|
spansEnded []sdktrace.ReadOnlySpan
|
2019-09-16 22:58:15 +02:00
|
|
|
shutdownCount int
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
func (t *testSpanProcessor) OnStart(parent context.Context, s sdktrace.ReadWriteSpan) {
|
2021-11-16 18:57:10 +02:00
|
|
|
if t == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-29 17:55:44 +02:00
|
|
|
psc := trace.SpanContextFromContext(parent)
|
2021-02-18 19:59:37 +02:00
|
|
|
kv := []attribute.KeyValue{
|
2020-11-16 18:45:49 +02:00
|
|
|
{
|
|
|
|
Key: "SpanProcessorName",
|
2021-02-18 19:59:37 +02:00
|
|
|
Value: attribute.StringValue(t.name),
|
2020-11-16 18:45:49 +02:00
|
|
|
},
|
|
|
|
// Store parent trace ID and span ID as attributes to be read later in
|
|
|
|
// tests so that we "do something" with the parent argument. Real
|
|
|
|
// SpanProcessor implementations will likely use the parent argument in
|
|
|
|
// a more meaningful way.
|
|
|
|
{
|
|
|
|
Key: "ParentTraceID",
|
2021-03-09 18:17:29 +02:00
|
|
|
Value: attribute.StringValue(psc.TraceID().String()),
|
2020-11-16 18:45:49 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "ParentSpanID",
|
2021-03-09 18:17:29 +02:00
|
|
|
Value: attribute.StringValue(psc.SpanID().String()),
|
2020-11-16 18:45:49 +02:00
|
|
|
},
|
2020-09-28 18:29:14 +02:00
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
s.AddEvent("OnStart", trace.WithAttributes(kv...))
|
2019-09-16 22:58:15 +02:00
|
|
|
t.spansStarted = append(t.spansStarted, s)
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
func (t *testSpanProcessor) OnEnd(s sdktrace.ReadOnlySpan) {
|
2021-11-16 18:57:10 +02:00
|
|
|
if t == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
t.spansEnded = append(t.spansEnded, s)
|
|
|
|
}
|
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
func (t *testSpanProcessor) Shutdown(_ context.Context) error {
|
2021-11-16 18:57:10 +02:00
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
t.shutdownCount++
|
2020-10-27 04:06:55 +02:00
|
|
|
return nil
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 21:12:13 +02:00
|
|
|
func (t *testSpanProcessor) ForceFlush(context.Context) error {
|
2021-11-16 18:57:10 +02:00
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-08 21:12:13 +02:00
|
|
|
return nil
|
2020-09-20 19:35:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-16 18:45:49 +02:00
|
|
|
func TestRegisterSpanProcessor(t *testing.T) {
|
2019-09-16 22:58:15 +02:00
|
|
|
name := "Register span processor before span starts"
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-28 18:29:14 +02:00
|
|
|
spNames := []string{"sp1", "sp2", "sp3"}
|
|
|
|
sps := NewNamedTestSpanProcessors(spNames)
|
2020-09-24 20:43:23 +02:00
|
|
|
|
|
|
|
for _, sp := range sps {
|
|
|
|
tp.RegisterSpanProcessor(sp)
|
|
|
|
}
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2020-11-16 18:45:49 +02:00
|
|
|
tid, _ := trace.TraceIDFromHex("01020304050607080102040810203040")
|
|
|
|
sid, _ := trace.SpanIDFromHex("0102040810203040")
|
2021-03-09 18:17:29 +02:00
|
|
|
parent := trace.NewSpanContext(trace.SpanContextConfig{
|
2020-11-16 18:45:49 +02:00
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-11-16 18:45:49 +02:00
|
|
|
ctx := trace.ContextWithRemoteSpanContext(context.Background(), parent)
|
|
|
|
|
2019-11-25 19:46:07 +02:00
|
|
|
tr := tp.Tracer("SpanProcessor")
|
2020-11-16 18:45:49 +02:00
|
|
|
_, span := tr.Start(ctx, "OnStart")
|
2019-09-27 19:48:10 +02:00
|
|
|
span.End()
|
2019-09-16 22:58:15 +02:00
|
|
|
wantCount := 1
|
2020-09-24 20:43:23 +02:00
|
|
|
|
|
|
|
for _, sp := range sps {
|
|
|
|
gotCount := len(sp.spansStarted)
|
|
|
|
if gotCount != wantCount {
|
|
|
|
t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
|
|
|
gotCount = len(sp.spansEnded)
|
|
|
|
if gotCount != wantCount {
|
|
|
|
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
2020-09-28 18:29:14 +02:00
|
|
|
|
|
|
|
c := 0
|
2020-11-16 18:45:49 +02:00
|
|
|
tidOK := false
|
|
|
|
sidOK := false
|
2020-12-11 07:15:44 +02:00
|
|
|
for _, e := range sp.spansStarted[0].Events() {
|
|
|
|
for _, kv := range e.Attributes {
|
|
|
|
switch kv.Key {
|
|
|
|
case "SpanProcessorName":
|
|
|
|
gotValue := kv.Value.AsString()
|
|
|
|
if gotValue != spNames[c] {
|
|
|
|
t.Errorf("%s: attributes: got %s, want %s\n", name, gotValue, spNames[c])
|
|
|
|
}
|
|
|
|
c++
|
|
|
|
case "ParentTraceID":
|
|
|
|
gotValue := kv.Value.AsString()
|
2021-03-09 18:17:29 +02:00
|
|
|
if gotValue != parent.TraceID().String() {
|
|
|
|
t.Errorf("%s: attributes: got %s, want %s\n", name, gotValue, parent.TraceID())
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
|
|
|
tidOK = true
|
|
|
|
case "ParentSpanID":
|
|
|
|
gotValue := kv.Value.AsString()
|
2021-03-09 18:17:29 +02:00
|
|
|
if gotValue != parent.SpanID().String() {
|
|
|
|
t.Errorf("%s: attributes: got %s, want %s\n", name, gotValue, parent.SpanID())
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
|
|
|
sidOK = true
|
|
|
|
default:
|
|
|
|
continue
|
2020-11-16 18:45:49 +02:00
|
|
|
}
|
2020-09-28 18:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c != len(spNames) {
|
2020-11-16 18:45:49 +02:00
|
|
|
t.Errorf("%s: expected attributes(SpanProcessorName): got %d, want %d\n", name, c, len(spNames))
|
|
|
|
}
|
|
|
|
if !tidOK {
|
|
|
|
t.Errorf("%s: expected attributes(ParentTraceID)\n", name)
|
|
|
|
}
|
|
|
|
if !sidOK {
|
|
|
|
t.Errorf("%s: expected attributes(ParentSpanID)\n", name)
|
2020-09-28 18:29:14 +02:00
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnregisterSpanProcessor(t *testing.T) {
|
|
|
|
name := "Start span after unregistering span processor"
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-28 18:29:14 +02:00
|
|
|
spNames := []string{"sp1", "sp2", "sp3"}
|
|
|
|
sps := NewNamedTestSpanProcessors(spNames)
|
2020-09-24 20:43:23 +02:00
|
|
|
|
|
|
|
for _, sp := range sps {
|
|
|
|
tp.RegisterSpanProcessor(sp)
|
|
|
|
}
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2019-11-25 19:46:07 +02:00
|
|
|
tr := tp.Tracer("SpanProcessor")
|
2019-10-22 22:19:11 +02:00
|
|
|
_, span := tr.Start(context.Background(), "OnStart")
|
2019-09-27 19:48:10 +02:00
|
|
|
span.End()
|
2020-09-24 20:43:23 +02:00
|
|
|
for _, sp := range sps {
|
|
|
|
tp.UnregisterSpanProcessor(sp)
|
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
// start another span after unregistering span processor.
|
2019-10-22 22:19:11 +02:00
|
|
|
_, span = tr.Start(context.Background(), "Start span after unregister")
|
2019-09-27 19:48:10 +02:00
|
|
|
span.End()
|
2019-09-16 22:58:15 +02:00
|
|
|
|
2020-09-24 20:43:23 +02:00
|
|
|
for _, sp := range sps {
|
|
|
|
wantCount := 1
|
|
|
|
gotCount := len(sp.spansStarted)
|
|
|
|
if gotCount != wantCount {
|
|
|
|
t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotCount = len(sp.spansEnded)
|
|
|
|
if gotCount != wantCount {
|
|
|
|
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnregisterSpanProcessorWhileSpanIsActive(t *testing.T) {
|
|
|
|
name := "Unregister span processor while span is active"
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-28 18:29:14 +02:00
|
|
|
sp := NewTestSpanProcessor("sp")
|
2019-10-22 22:19:11 +02:00
|
|
|
tp.RegisterSpanProcessor(sp)
|
|
|
|
|
2019-11-25 19:46:07 +02:00
|
|
|
tr := tp.Tracer("SpanProcessor")
|
2019-10-22 22:19:11 +02:00
|
|
|
_, span := tr.Start(context.Background(), "OnStart")
|
|
|
|
tp.UnregisterSpanProcessor(sp)
|
2019-09-16 22:58:15 +02:00
|
|
|
|
2019-09-27 19:48:10 +02:00
|
|
|
span.End()
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
wantCount := 1
|
|
|
|
gotCount := len(sp.spansStarted)
|
|
|
|
if gotCount != wantCount {
|
|
|
|
t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
wantCount = 0
|
|
|
|
gotCount = len(sp.spansEnded)
|
|
|
|
if gotCount != wantCount {
|
|
|
|
t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:34:19 +02:00
|
|
|
func TestSpanProcessorShutdown(t *testing.T) {
|
|
|
|
name := "Increment shutdown counter of a span processor"
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-28 18:29:14 +02:00
|
|
|
sp := NewTestSpanProcessor("sp")
|
2019-10-22 22:19:11 +02:00
|
|
|
tp.RegisterSpanProcessor(sp)
|
2019-10-08 19:34:19 +02:00
|
|
|
|
2019-10-09 08:31:29 +02:00
|
|
|
wantCount := 1
|
2020-10-27 04:06:55 +02:00
|
|
|
err := sp.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error shutting the testSpanProcessor down\n")
|
|
|
|
}
|
2019-10-08 19:34:19 +02:00
|
|
|
|
|
|
|
gotCount := sp.shutdownCount
|
|
|
|
if wantCount != gotCount {
|
|
|
|
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) {
|
2019-10-09 08:31:29 +02:00
|
|
|
name := "Increment shutdown counter after first UnregisterSpanProcessor call"
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-28 18:29:14 +02:00
|
|
|
sp := NewTestSpanProcessor("sp")
|
2019-10-08 19:34:19 +02:00
|
|
|
|
2019-10-09 08:31:29 +02:00
|
|
|
wantCount := 1
|
2019-10-08 19:34:19 +02:00
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
tp.RegisterSpanProcessor(sp)
|
|
|
|
tp.UnregisterSpanProcessor(sp)
|
2019-10-08 19:34:19 +02:00
|
|
|
|
|
|
|
gotCount := sp.shutdownCount
|
|
|
|
if wantCount != gotCount {
|
|
|
|
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:31:29 +02:00
|
|
|
// Multiple UnregisterSpanProcessor should not trigger multiple Shutdown calls.
|
2019-10-22 22:19:11 +02:00
|
|
|
tp.UnregisterSpanProcessor(sp)
|
2019-10-08 19:34:19 +02:00
|
|
|
|
|
|
|
gotCount = sp.shutdownCount
|
|
|
|
if wantCount != gotCount {
|
|
|
|
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount)
|
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
func NewTestSpanProcessor(name string) *testSpanProcessor {
|
|
|
|
return &testSpanProcessor{name: name}
|
2020-09-28 18:29:14 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
func NewNamedTestSpanProcessors(names []string) []*testSpanProcessor {
|
|
|
|
tsp := []*testSpanProcessor{}
|
2020-09-28 18:29:14 +02:00
|
|
|
for _, n := range names {
|
|
|
|
tsp = append(tsp, NewTestSpanProcessor(n))
|
|
|
|
}
|
|
|
|
return tsp
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|