diff --git a/pkg/sql/flowinfra/BUILD.bazel b/pkg/sql/flowinfra/BUILD.bazel index 01e47711669b..ece70774f538 100644 --- a/pkg/sql/flowinfra/BUILD.bazel +++ b/pkg/sql/flowinfra/BUILD.bazel @@ -40,6 +40,7 @@ go_library( "//pkg/util/syncutil", "//pkg/util/timeutil", "//pkg/util/tracing", + "//pkg/util/tracing/tracingpb", "//pkg/util/uuid", "@com_github_cockroachdb_errors//:errors", "@com_github_cockroachdb_redact//:redact", diff --git a/pkg/sql/flowinfra/outbox.go b/pkg/sql/flowinfra/outbox.go index 8a117ecb73aa..b0384ddf2a53 100644 --- a/pkg/sql/flowinfra/outbox.go +++ b/pkg/sql/flowinfra/outbox.go @@ -26,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/timeutil" "github.com/cockroachdb/cockroach/pkg/util/tracing" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb" "go.opentelemetry.io/otel/attribute" ) @@ -210,10 +211,12 @@ func (m *Outbox) mainLoop(ctx context.Context) error { var span *tracing.Span ctx, span = execinfra.ProcessorSpan(ctx, "outbox") defer span.Finish() - if span != nil && span.IsVerbose() { - m.statsCollectionEnabled = true - span.SetTag(execinfrapb.FlowIDTagKey, attribute.StringValue(m.flowCtx.ID.String())) - span.SetTag(execinfrapb.StreamIDTagKey, attribute.IntValue(int(m.streamID))) + if span != nil { + m.statsCollectionEnabled = span.RecordingType() != tracingpb.RecordingOff + if span.IsVerbose() { + span.SetTag(execinfrapb.FlowIDTagKey, attribute.StringValue(m.flowCtx.ID.String())) + span.SetTag(execinfrapb.StreamIDTagKey, attribute.IntValue(int(m.streamID))) + } } if m.stream == nil { diff --git a/pkg/sql/instrumentation.go b/pkg/sql/instrumentation.go index 64a60cde88ea..a1051f3eed59 100644 --- a/pkg/sql/instrumentation.go +++ b/pkg/sql/instrumentation.go @@ -273,7 +273,17 @@ func (ih *instrumentationHelper) Setup( ih.collectExecStats = true ih.evalCtx = p.EvalContext() - newCtx, ih.sp = tracing.EnsureChildSpan(ctx, cfg.AmbientCtx.Tracer, "traced statement", tracing.WithRecording(tracing.RecordingVerbose)) + // Execution stats are propagated as structured metadata, so we definitely + // need to enable the tracing. We default to the RecordingStructured level + // in order to reduce the overhead of EXPLAIN ANALYZE. + recType := tracingpb.RecordingStructured + if ih.collectBundle || ih.withStatementTrace != nil { + // Use the verbose recording only if we're collecting the bundle (the + // verbose trace is very helpful during debugging) or if we have a + // testing callback. + recType = tracingpb.RecordingVerbose + } + newCtx, ih.sp = tracing.EnsureChildSpan(ctx, cfg.AmbientCtx.Tracer, "traced statement", tracing.WithRecording(recType)) ih.shouldFinishSpan = true return newCtx, true } diff --git a/pkg/sql/rowflow/BUILD.bazel b/pkg/sql/rowflow/BUILD.bazel index aff5c52aba7d..29d3aca41eec 100644 --- a/pkg/sql/rowflow/BUILD.bazel +++ b/pkg/sql/rowflow/BUILD.bazel @@ -24,6 +24,7 @@ go_library( "//pkg/util/mon", "//pkg/util/syncutil", "//pkg/util/tracing", + "//pkg/util/tracing/tracingpb", "@com_github_cockroachdb_errors//:errors", "@com_github_cockroachdb_redact//:redact", "@io_opentelemetry_go_otel//attribute", diff --git a/pkg/sql/rowflow/routers.go b/pkg/sql/rowflow/routers.go index 5851f0b604fe..370620200944 100644 --- a/pkg/sql/rowflow/routers.go +++ b/pkg/sql/rowflow/routers.go @@ -32,6 +32,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/mon" "github.com/cockroachdb/cockroach/pkg/util/syncutil" "github.com/cockroachdb/cockroach/pkg/util/tracing" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb" "github.com/cockroachdb/errors" "github.com/cockroachdb/redact" "go.opentelemetry.io/otel/attribute" @@ -262,7 +263,7 @@ func (rb *routerBase) setupStreams( // init must be called after setupStreams but before Start. func (rb *routerBase) init(ctx context.Context, flowCtx *execinfra.FlowCtx, types []*types.T) { // Check if we're recording stats. - if s := tracing.SpanFromContext(ctx); s != nil && s.IsVerbose() { + if s := tracing.SpanFromContext(ctx); s != nil && s.RecordingType() != tracingpb.RecordingOff { rb.statsCollectionEnabled = true } @@ -313,7 +314,9 @@ func (rb *routerBase) Start(ctx context.Context, wg *sync.WaitGroup, _ context.C if rb.statsCollectionEnabled { ctx, span = execinfra.ProcessorSpan(ctx, "router output") defer span.Finish() - span.SetTag(execinfrapb.StreamIDTagKey, attribute.IntValue(int(ro.streamID))) + if span.IsVerbose() { + span.SetTag(execinfrapb.StreamIDTagKey, attribute.IntValue(int(ro.streamID))) + } ro.stats.Inputs = make([]execinfrapb.InputStats, 1) }