From d3a6549ef1b995a490bcb55488472871c9689948 Mon Sep 17 00:00:00 2001 From: Nick Ripley Date: Tue, 16 Jul 2024 06:32:37 -0700 Subject: [PATCH] contrib/database/sql: remove broken execution trace tests (#2789) The execution tracer tests for contrib/database/sql use a parser which does not support Go 1.21 or newer traces. The tests should be ported to use golang.org/x/exp/trace and moved to internal/exectracetest, but for now they are effectively broken since we no longer run Go 1.20 in CI. Remove them to fix CI on main, until we port them to the new parser. --- contrib/database/sql/exec_trace_test.go | 146 ------------------------ go.mod | 1 - go.sum | 2 - 3 files changed, 149 deletions(-) delete mode 100644 contrib/database/sql/exec_trace_test.go diff --git a/contrib/database/sql/exec_trace_test.go b/contrib/database/sql/exec_trace_test.go deleted file mode 100644 index 00df3bac70..0000000000 --- a/contrib/database/sql/exec_trace_test.go +++ /dev/null @@ -1,146 +0,0 @@ -// Unless explicitly stated otherwise all files in this repository are licensed -// under the Apache License Version 2.0. -// This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2023 Datadog, Inc. - -package sql - -import ( - "bytes" - "context" - "io" - "net/http" - "runtime/trace" - "testing" - "time" - - "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql/internal" - "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" - "gopkg.in/DataDog/dd-trace-go.v1/internal/httpmem" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - gotraceui "honnef.co/go/gotraceui/trace" -) - -func TestExecutionTraceAnnotations(t *testing.T) { - if trace.IsEnabled() { - t.Skip("execution tracing is already enabled") - } - - // In-memory server & client which discards everything, to avoid - // slowness from unnecessary network I/O - s, c := httpmem.ServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) - defer s.Close() - tracer.Start(tracer.WithHTTPClient(c), tracer.WithLogStartup(false)) - defer tracer.Stop() - - buf := new(bytes.Buffer) - require.NoError(t, trace.Start(buf), "starting execution tracing") - - // sleepDuration is the amount of time our mock DB operations should - // take. We are going to assert that the execution trace tasks - // corresponding with the duration are at least as long as this. In - // reality they could be longer than this due to slow CI, scheduling - // jitter, etc., but we know that they should be at least this long. - const sleepDuration = 10 * time.Millisecond - - Register("mock", &internal.MockDriver{ - Hook: func() { - time.Sleep(sleepDuration) - }, - }) - db, err := Open("mock", "") - require.NoError(t, err, "opening mock db") - - span, ctx := tracer.StartSpanFromContext(context.Background(), "parent") - _, err = db.ExecContext(ctx, "foobar") - require.NoError(t, err, "executing mock statement") - conn, err := db.Conn(ctx) - require.NoError(t, err, "connecting to DB") - rows, err := conn.QueryContext(ctx, "foobar") - require.NoError(t, err, "executing mock query") - rows.Close() - stmt, err := conn.PrepareContext(ctx, "prepared") - require.NoError(t, err, "preparing mock statement") - _, err = stmt.ExecContext(ctx) - require.NoError(t, err, "executing mock perpared statement") - rows, err = stmt.QueryContext(ctx) - require.NoError(t, err, "executing mock perpared query") - rows.Close() - tx, err := conn.BeginTx(ctx, nil) - require.NoError(t, err, "beginning mock transaction") - _, err = tx.ExecContext(ctx, "foobar") - require.NoError(t, err, "executing query in mock transaction") - require.NoError(t, tx.Commit(), "commiting mock transaction") - require.NoError(t, conn.Close()) - - span.Finish() - - trace.Stop() - - tasks, err := tasksFromTrace(buf) - require.NoError(t, err, "getting tasks from trace") - - expectedParentChildTasks := []string{"Connect", "Exec", "Query", "Prepare", "Exec", "Query", "Begin", "Exec"} - var foundParent, foundPrepared bool - for id, task := range tasks { - t.Logf("task %d: %+v", id, task) - switch task.name { - case "parent": - foundParent = true - var gotParentChildTasks []string - for _, id := range tasks[id].children { - gotParentChildTasks = append(gotParentChildTasks, tasks[id].name) - } - assert.ElementsMatch(t, expectedParentChildTasks, gotParentChildTasks) - case "Prepare": - foundPrepared = true - assert.GreaterOrEqual(t, task.duration, sleepDuration, "task %s", task.name) - case "Connect", "Exec", "Begin", "Commit", "Query": - assert.GreaterOrEqual(t, task.duration, sleepDuration, "task %s", task.name) - default: - continue - } - } - assert.True(t, foundParent, "need parent task") - assert.True(t, foundPrepared, "need prepared statement task") -} - -type traceTask struct { - name string - duration time.Duration - parent int - children []int -} - -func tasksFromTrace(r io.Reader) (map[int]traceTask, error) { - execTrace, err := gotraceui.Parse(r, nil) - if err != nil { - return nil, err - } - - tasks := make(map[int]traceTask) - for _, ev := range execTrace.Events { - switch ev.Type { - case gotraceui.EvUserTaskCreate: - if ev.Link == -1 { - continue - } - id := int(ev.Args[0]) - parent := int(ev.Args[1]) - if parent != 0 { - t := tasks[parent] - t.children = append(t.children, id) - tasks[parent] = t - } - name := execTrace.Strings[ev.Args[2]] - tasks[id] = traceTask{ - name: name, - parent: parent, - duration: time.Duration(execTrace.Events[ev.Link].Ts - ev.Ts), - } - } - } - return tasks, nil -} diff --git a/go.mod b/go.mod index af26485d97..33809dd6cb 100644 --- a/go.mod +++ b/go.mod @@ -112,7 +112,6 @@ require ( gorm.io/driver/postgres v1.4.6 gorm.io/driver/sqlserver v1.4.2 gorm.io/gorm v1.25.3 - honnef.co/go/gotraceui v0.4.0 k8s.io/apimachinery v0.23.17 k8s.io/client-go v0.23.17 modernc.org/sqlite v1.28.0 diff --git a/go.sum b/go.sum index d2d2e753ef..32f99bb03f 100644 --- a/go.sum +++ b/go.sum @@ -3058,8 +3058,6 @@ gotest.tools/gotestsum v1.8.2/go.mod h1:6JHCiN6TEjA7Kaz23q1bH0e2Dc3YJjDUZ0DmctFZ gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= -honnef.co/go/gotraceui v0.4.0 h1:6CioQQRvVEWTQ9fXcGY3LXSuKL6cgavln5ZMKVbF5yE= -honnef.co/go/gotraceui v0.4.0/go.mod h1:hcoo31yypIJuS1FXdFWjKu1PVJrdpIL9ShdQvMvm9qw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=