pkg: create package traceutil for tracing. mvcc: add tracing

steps:range from the in-memory index tree; range from boltdb.
etcdserver: add tracing steps: agreement among raft nodes before
linerized reading; authentication; filter and sort kv pairs; assemble
the response.
This commit is contained in:
yoyinzyc
2019-09-09 15:38:03 -07:00
parent 594005d7de
commit f4e7fc56a7
12 changed files with 149 additions and 28 deletions

60
pkg/traceutil/trace.go Normal file
View File

@@ -0,0 +1,60 @@
package traceutil
import (
"bytes"
"fmt"
"time"
"github.com/coreos/pkg/capnslog"
"go.uber.org/zap"
)
var (
plog = capnslog.NewPackageLogger("go.etcd.io/etcd", "trace")
)
type Trace struct {
operation string
startTime time.Time
steps []step
}
type step struct {
time time.Time
msg string
}
func New(op string) *Trace {
return &Trace{operation: op, startTime: time.Now()}
}
func (t *Trace) Step(msg string) {
t.steps = append(t.steps, step{time: time.Now(), msg: msg})
}
// Dump all steps in the Trace
func (t *Trace) Log(lg *zap.Logger) {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("The tracing of %v request:\n", t.operation))
buf.WriteString("Request started at:")
buf.WriteString(t.startTime.Format("2006-01-02 15:04:05"))
buf.WriteString(fmt.Sprintf(".%06d", t.startTime.Nanosecond()/1000))
buf.WriteString("\n")
lastStepTime := t.startTime
for i, step := range t.steps {
buf.WriteString(fmt.Sprintf("Step %d: %v Time cost: %v\n", i, step.msg, step.time.Sub(lastStepTime)))
//fmt.Println(step.msg, " costs: ", step.time.Sub(lastStepTime))
lastStepTime = step.time
}
buf.WriteString("Trace End\n")
s := buf.String()
if lg != nil {
lg.Info(s)
} else {
plog.Info(s)
}
}

View File

@@ -0,0 +1,28 @@
package traceutil
import (
"testing"
)
func TestTrace(t *testing.T) {
var (
op = "Test"
steps = []string{"Step1, Step2"}
)
trace := New(op)
if trace.operation != op {
t.Errorf("Expected %v, got %v\n", op, trace.operation)
}
for _, v := range steps {
trace.Step(v)
trace.Step(v)
}
for i, v := range steps {
if v != trace.steps[i].msg {
t.Errorf("Expected %v, got %v\n.", v, trace.steps[i].msg)
}
}
}