You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/pkg/cache/redis/trace.go

145 lines
3.4 KiB

6 years ago
package redis
import (
"context"
"fmt"
"time"
"github.com/bilibili/kratos/pkg/log"
"github.com/bilibili/kratos/pkg/net/trace"
6 years ago
)
const (
_traceComponentName = "library/cache/redis"
6 years ago
_tracePeerService = "redis"
_traceSpanKind = "client"
)
var _internalTags = []trace.Tag{
trace.TagString(trace.TagSpanKind, _traceSpanKind),
trace.TagString(trace.TagComponent, _traceComponentName),
trace.TagString(trace.TagPeerService, _tracePeerService),
}
type traceConn struct {
// tr for pipeline, if tr != nil meaning on pipeline
tr trace.Trace
6 years ago
// connTag include e.g. ip,port
connTags []trace.Tag
ctx context.Context
6 years ago
// origin redis conn
Conn
pending int
// TODO: split slow log from trace.
slowLogThreshold time.Duration
6 years ago
}
func (t *traceConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
statement := getStatement(commandName, args...)
defer t.slowLog(statement, time.Now())
6 years ago
// NOTE: ignored empty commandName
// current sdk will Do empty command after pipeline finished
if t.tr == nil || commandName == "" {
6 years ago
return t.Conn.Do(commandName, args...)
}
tr := t.tr.Fork("", "Redis:"+commandName)
6 years ago
tr.SetTag(_internalTags...)
tr.SetTag(t.connTags...)
tr.SetTag(trace.TagString(trace.TagDBStatement, statement))
reply, err = t.Conn.Do(commandName, args...)
tr.Finish(&err)
return
}
func (t *traceConn) Send(commandName string, args ...interface{}) (err error) {
6 years ago
statement := getStatement(commandName, args...)
defer t.slowLog(statement, time.Now())
6 years ago
t.pending++
if t.tr == nil {
6 years ago
return t.Conn.Send(commandName, args...)
}
if t.pending == 1 {
t.tr = t.tr.Fork("", "Redis:Pipeline")
6 years ago
t.tr.SetTag(_internalTags...)
t.tr.SetTag(t.connTags...)
}
t.tr.SetLog(
trace.Log(trace.LogEvent, "Send"),
trace.Log("db.statement", statement),
)
if err = t.Conn.Send(commandName, args...); err != nil {
6 years ago
t.tr.SetTag(trace.TagBool(trace.TagError, true))
t.tr.SetLog(
trace.Log(trace.LogEvent, "Send Fail"),
trace.Log(trace.LogMessage, err.Error()),
)
}
return err
}
func (t *traceConn) Flush() error {
defer t.slowLog("Flush", time.Now())
6 years ago
if t.tr == nil {
return t.Conn.Flush()
}
t.tr.SetLog(trace.Log(trace.LogEvent, "Flush"))
err := t.Conn.Flush()
if err != nil {
t.tr.SetTag(trace.TagBool(trace.TagError, true))
t.tr.SetLog(
trace.Log(trace.LogEvent, "Flush Fail"),
trace.Log(trace.LogMessage, err.Error()),
)
}
return err
}
func (t *traceConn) Receive() (reply interface{}, err error) {
defer t.slowLog("Receive", time.Now())
6 years ago
if t.tr == nil {
return t.Conn.Receive()
}
t.tr.SetLog(trace.Log(trace.LogEvent, "Receive"))
reply, err = t.Conn.Receive()
if err != nil {
t.tr.SetTag(trace.TagBool(trace.TagError, true))
t.tr.SetLog(
trace.Log(trace.LogEvent, "Receive Fail"),
trace.Log(trace.LogMessage, err.Error()),
)
}
if t.pending > 0 {
t.pending--
}
if t.pending == 0 {
t.tr.Finish(nil)
t.tr = nil
}
return reply, err
}
func (t *traceConn) WithContext(ctx context.Context) Conn {
t.Conn = t.Conn.WithContext(ctx)
if root, ok := trace.FromContext(ctx); ok {
t.tr = root
}
6 years ago
return t
}
func (t *traceConn) slowLog(statement string, now time.Time) {
6 years ago
du := time.Since(now)
if du > t.slowLogThreshold {
6 years ago
log.Warn("%s slow log statement: %s time: %v", _tracePeerService, statement, du)
}
}
func getStatement(commandName string, args ...interface{}) (res string) {
res = commandName
if len(args) > 0 {
res = fmt.Sprintf("%s %v", commandName, args[0])
}
return
}