From 43a1231da1e01f94916688fdc4c067dfc26215fa Mon Sep 17 00:00:00 2001 From: Katie Hockman Date: Fri, 12 Jan 2024 17:33:47 -0500 Subject: [PATCH] share single client/server for all OTel tests, to lower execution time and chance of time out --- ddtrace/opentelemetry/otel_test.go | 2 +- ddtrace/opentelemetry/span_test.go | 62 +++++++++++++++++----------- ddtrace/opentelemetry/tracer_test.go | 8 ++-- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/ddtrace/opentelemetry/otel_test.go b/ddtrace/opentelemetry/otel_test.go index 78c17670f9..036762dfc1 100644 --- a/ddtrace/opentelemetry/otel_test.go +++ b/ddtrace/opentelemetry/otel_test.go @@ -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{}) diff --git a/ddtrace/opentelemetry/span_test.go b/ddtrace/opentelemetry/span_test.go index 243ef5f662..01b4f43bb9 100644 --- a/ddtrace/opentelemetry/span_test.go +++ b/ddtrace/opentelemetry/span_test.go @@ -13,6 +13,7 @@ import ( "fmt" "io" "net/http" + "os" "strings" "testing" "time" @@ -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" { @@ -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: @@ -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() } } @@ -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() @@ -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() @@ -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() @@ -210,7 +224,7 @@ func TestSpanSetStatus(t *testing.T) { ignoredMsg: "unset_description", }, } - _, payloads, cleanup := mockTracerProvider(t) + _, cleanup := mockTracerProvider(t) tr := otel.Tracer("") defer cleanup() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/ddtrace/opentelemetry/tracer_test.go b/ddtrace/opentelemetry/tracer_test.go index aa4108cc0f..6c8420ec73 100644 --- a/ddtrace/opentelemetry/tracer_test.go +++ b/ddtrace/opentelemetry/tracer_test.go @@ -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 @@ -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) { @@ -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)()