Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: tee logs to runtime/trace output #19994

Merged
merged 9 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"math"
"runtime/trace"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -913,6 +914,9 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
if _, ok := a.StmtNode.(*ast.CommitStmt); ok {
slowItems.PrevStmt = sessVars.PrevStmt.String()
}
if trace.IsEnabled() {
trace.Log(a.GoCtx, "details", sessVars.SlowLogFormat(slowItems))
}
if costTime < threshold {
logutil.SlowQueryLogger.Debug(sessVars.SlowLogFormat(slowItems))
} else {
Expand Down
9 changes: 8 additions & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -928,8 +929,14 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
if len(sqlType) > 0 {
var task *trace.Task
ctx, task = trace.NewTask(ctx, sqlType)
trace.Log(ctx, "sql", lc.String())
defer task.End()

trace.Log(ctx, "sql", lc.String())
ctx = logutil.WithTraceLogger(ctx, cc.connectionID)

taskID := *(*uint64)(unsafe.Pointer(task))
ctx = pprof.WithLabels(ctx, pprof.Labels("trace", strconv.FormatUint(taskID, 10)))
pprof.SetGoroutineLabels(ctx)
}
}
token := cc.server.getToken()
Expand Down
38 changes: 38 additions & 0 deletions util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/trace"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -355,6 +356,43 @@ func WithConnID(ctx context.Context, connID uint32) context.Context {
return context.WithValue(ctx, ctxLogKey, logger.With(zap.Uint32("conn", connID)))
}

// WithTraceLogger attaches trace identifier to context
func WithTraceLogger(ctx context.Context, connID uint32) context.Context {
var logger *zap.Logger
if ctxLogger, ok := ctx.Value(ctxLogKey).(*zap.Logger); ok {
logger = ctxLogger
} else {
logger = zaplog.L()
}
return context.WithValue(ctx, ctxLogKey, wrapTraceLogger(ctx, connID, logger))
}

func wrapTraceLogger(ctx context.Context, connID uint32, logger *zap.Logger) *zap.Logger {
return logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
tl := &traceLog{ctx: ctx}
traceCore := zaplog.NewTextCore(zaplog.NewTextEncoder(&zaplog.Config{}), tl, tl).
With([]zapcore.Field{zap.Uint32("conn", connID)})
return zapcore.NewTee(traceCore, core)
}))
}

type traceLog struct {
ctx context.Context
}

func (t *traceLog) Enabled(_ zapcore.Level) bool {
return true
}

func (t *traceLog) Write(p []byte) (n int, err error) {
trace.Log(t.ctx, "log", string(p))
return len(p), nil
}

func (t *traceLog) Sync() error {
return nil
}

// WithKeyValue attaches key/value to context.
func WithKeyValue(ctx context.Context, key, value string) context.Context {
var logger *zap.Logger
Expand Down