Skip to content

Commit

Permalink
share single client/server for all OTel tests, to lower execution tim…
Browse files Browse the repository at this point in the history
…e and chance of time out
  • Loading branch information
katiehockman committed Jan 12, 2024
1 parent 07b2344 commit 43a1231
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ddtrace/opentelemetry/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

func TestHttpDistributedTrace(t *testing.T) {
assert := assert.New(t)
tp, payloads, cleanup := mockTracerProvider(t)
tp, cleanup := mockTracerProvider(t)
defer cleanup()
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.TraceContext{})
Expand Down
62 changes: 38 additions & 24 deletions ddtrace/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"
"time"
Expand All @@ -29,11 +30,10 @@ import (
oteltrace "go.opentelemetry.io/otel/trace"
)

type traces [][]map[string]interface{}

func mockTracerProvider(t *testing.T, opts ...tracer.StartOption) (tp *TracerProvider, payloads chan traces, cleanup func()) {
func TestMain(m *testing.M) {
payloads = make(chan traces)
s, c := httpmem.ServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var server *http.Server
server, client = httpmem.ServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v0.4/traces":
if h := r.Header.Get("X-Datadog-Trace-Count"); h == "0" {
Expand All @@ -43,17 +43,17 @@ func mockTracerProvider(t *testing.T, opts ...tracer.StartOption) (tp *TracerPro
defer req.Body.Close()
buf, err := io.ReadAll(req.Body)
if err != nil || len(buf) == 0 {
t.Fatalf("Test agent: Error receiving traces: %v", err)
panic(fmt.Sprintf("Test agent: Error receiving traces: %v", err))
}
var payload bytes.Buffer
_, err = msgp.UnmarshalAsJSON(&payload, buf)
if err != nil {
t.Fatalf("Failed to unmarshal payload bytes as JSON: %v", err)
panic(fmt.Sprintf("Failed to unmarshal payload bytes as JSON: %v", err))
}
var tr [][]map[string]interface{}
err = json.Unmarshal(payload.Bytes(), &tr)
if err != nil || len(tr) == 0 {
t.Fatalf("Failed to unmarshal payload bytes as trace: %v", err)
panic(fmt.Sprintf("Failed to unmarshal payload bytes as trace: %v", err))
}
payloads <- tr
default:
Expand All @@ -67,11 +67,25 @@ func mockTracerProvider(t *testing.T, opts ...tracer.StartOption) (tp *TracerPro
}
w.WriteHeader(200)
}))
opts = append(opts, tracer.WithHTTPClient(c))

errCode := m.Run()
server.Close()
close(payloads)
os.Exit(errCode)
}

var (
payloads chan traces
client *http.Client
)

type traces [][]map[string]interface{}

func mockTracerProvider(t *testing.T, opts ...tracer.StartOption) (tp *TracerProvider, cleanup func()) {
opts = append(opts, tracer.WithHTTPClient(client))
tp = NewTracerProvider(opts...)
otel.SetTracerProvider(tp)
return tp, payloads, func() {
s.Close()
return tp, func() {
tp.Shutdown()
}
}
Expand All @@ -90,7 +104,7 @@ func TestSpanResourceNameDefault(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand All @@ -112,7 +126,7 @@ func TestSpanSetName(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand All @@ -133,7 +147,7 @@ func TestSpanEnd(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -210,7 +224,7 @@ func TestSpanSetStatus(t *testing.T) {
ignoredMsg: "unset_description",
},
}
_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -256,7 +270,7 @@ func TestSpanContextWithStartOptions(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -310,7 +324,7 @@ func TestSpanContextWithStartOptionsPriorityOrder(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -342,7 +356,7 @@ func TestSpanEndOptionsPriorityOrder(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -378,7 +392,7 @@ func TestSpanEndOptions(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -416,7 +430,7 @@ func TestSpanSetAttributes(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -477,7 +491,7 @@ func TestSpanSetAttributesWithRemapping(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand All @@ -500,7 +514,7 @@ func TestTracerStartOptions(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
_, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
tr := otel.Tracer("")
defer cleanup()

Expand All @@ -522,7 +536,7 @@ func TestOperationNameRemapping(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
_, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

Expand Down Expand Up @@ -650,7 +664,7 @@ func TestRemapName(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
_, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
tr := otel.Tracer("")
defer cleanup()

Expand All @@ -676,7 +690,7 @@ func TestRemapWithMultipleSetAttributes(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
_, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
tr := otel.Tracer("")
defer cleanup()

Expand Down
8 changes: 3 additions & 5 deletions ddtrace/opentelemetry/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func TestSpanContext(t *testing.T) {
}

func TestForceFlush(t *testing.T) {
tp, cleanup := mockTracerProvider(t)
defer cleanup()
assert := assert.New(t)
const (
UNSET = iota
Expand All @@ -139,10 +141,8 @@ func TestForceFlush(t *testing.T) {
}
for _, tc := range testData {
t.Run(fmt.Sprintf("Flush success: %t", tc.flushed), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
tp, payloads, cleanup := mockTracerProvider(t)
defer cleanup()

flushStatus := UNSET
setFlushStatus := func(ok bool) {
Expand All @@ -168,8 +168,6 @@ func TestForceFlush(t *testing.T) {
}

t.Run("Flush after shutdown", func(t *testing.T) {
tp := NewTracerProvider()
otel.SetTracerProvider(tp)
testLog := new(log.RecordLogger)
defer log.UseLogger(testLog)()

Expand Down

0 comments on commit 43a1231

Please sign in to comment.