| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 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)
- }
- }
|