Skip to content

Commit

Permalink
trace: tee logs to runtime/trace output (#19994)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zejun Li authored Sep 23, 2020
1 parent 592b244 commit 92a04ef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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

0 comments on commit 92a04ef

Please sign in to comment.