Skip to content

Commit

Permalink
release-22.1: backport distsql profiler labels for remote flows
Browse files Browse the repository at this point in the history
Backport of cockroachdb#92775

Fixes: cockroachdb#82464

Release note: SQL queries running on remote notes now show up in cpu
profiles with "distsql.stmt" label.
  • Loading branch information
cucaroach committed Feb 13, 2023
1 parent 96481c6 commit cdd538a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/sql/distsql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ go_library(
"//pkg/util/envutil",
"//pkg/util/log",
"//pkg/util/mon",
"//pkg/util/pprofutil",
"//pkg/util/timeutil",
"//pkg/util/tracing",
"@com_github_cockroachdb_errors//:errors",
Expand Down
19 changes: 18 additions & 1 deletion pkg/sql/distsql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/pprofutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -408,6 +409,16 @@ func (ds *ServerImpl) setupFlow(

if !f.IsLocal() {
flowCtx.AmbientContext.AddLogTag("f", f.GetFlowCtx().ID.Short())
if req.StatementSQL != "" {
flowCtx.AmbientContext.AddLogTag("distsql.stmt", req.StatementSQL)
}
flowCtx.AmbientContext.AddLogTag("distsql.gateway", req.Flow.Gateway)
if req.EvalContext.SessionData.ApplicationName != "" {
flowCtx.AmbientContext.AddLogTag("distsql.appname", req.EvalContext.SessionData.ApplicationName)
}
if leafTxn != nil {
flowCtx.AmbientContext.AddLogTag("distsql.txn", leafTxn.ID())
}
ctx = flowCtx.AmbientContext.AnnotateCtx(ctx)
telemetry.Inc(sqltelemetry.DistSQLExecCounter)
}
Expand Down Expand Up @@ -644,6 +655,9 @@ func (ds *ServerImpl) SetupFlow(
nil /* batchSyncFlowConsumer */, LocalState{},
)
if err == nil {
var undo func()
ctx, undo = pprofutil.SetProfilerLabelsFromCtxTags(ctx)
defer undo()
err = ds.flowScheduler.ScheduleFlow(ctx, f)
}
if err != nil {
Expand Down Expand Up @@ -690,7 +704,10 @@ func (ds *ServerImpl) flowStreamInt(
}
defer cleanup()
log.VEventf(ctx, 1, "connected inbound stream %s/%d", flowID.Short(), streamID)
return streamStrategy.Run(f.AmbientContext.AnnotateCtx(ctx), stream, msg, f)
ctx = f.AmbientContext.AnnotateCtx(ctx)
ctx, undo := pprofutil.SetProfilerLabelsFromCtxTags(ctx)
defer undo()
return streamStrategy.Run(ctx, stream, msg, f)
}

// FlowStream is part of the execinfrapb.DistSQLServer interface.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/pprofutil/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func SetProfilerLabels(ctx context.Context, labels ...string) (_ context.Context
func SetProfilerLabelsFromCtxTags(ctx context.Context) (_ context.Context, undo func()) {
tags := logtags.FromContext(ctx)
if tags == nil || len(tags.Get()) == 0 {
return
return ctx, func() {}
}
var labels []string
for _, tag := range tags.Get() {
Expand Down

0 comments on commit cdd538a

Please sign in to comment.