trace.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package traceutil implements tracing utilities using "context".
  15. package traceutil
  16. import (
  17. "bytes"
  18. "context"
  19. "fmt"
  20. "math/rand"
  21. "time"
  22. "go.uber.org/zap"
  23. )
  24. const (
  25. TraceKey = "trace"
  26. StartTimeKey = "startTime"
  27. )
  28. // Field is a kv pair to record additional details of the trace.
  29. type Field struct {
  30. Key string
  31. Value interface{}
  32. }
  33. func (f *Field) format() string {
  34. return fmt.Sprintf("%s:%v; ", f.Key, f.Value)
  35. }
  36. func writeFields(fields []Field) string {
  37. if len(fields) == 0 {
  38. return ""
  39. }
  40. var buf bytes.Buffer
  41. buf.WriteString("{")
  42. for _, f := range fields {
  43. buf.WriteString(f.format())
  44. }
  45. buf.WriteString("}")
  46. return buf.String()
  47. }
  48. type Trace struct {
  49. operation string
  50. lg *zap.Logger
  51. fields []Field
  52. startTime time.Time
  53. steps []step
  54. stepDisabled bool
  55. }
  56. type step struct {
  57. time time.Time
  58. msg string
  59. fields []Field
  60. }
  61. func New(op string, lg *zap.Logger, fields ...Field) *Trace {
  62. return &Trace{operation: op, lg: lg, startTime: time.Now(), fields: fields}
  63. }
  64. // TODO returns a non-nil, empty Trace
  65. func TODO() *Trace {
  66. return &Trace{}
  67. }
  68. func Get(ctx context.Context) *Trace {
  69. if trace, ok := ctx.Value(TraceKey).(*Trace); ok && trace != nil {
  70. return trace
  71. }
  72. return TODO()
  73. }
  74. func (t *Trace) GetStartTime() time.Time {
  75. return t.startTime
  76. }
  77. func (t *Trace) SetStartTime(time time.Time) {
  78. t.startTime = time
  79. }
  80. func (t *Trace) InsertStep(at int, time time.Time, msg string, fields ...Field) {
  81. newStep := step{time, msg, fields}
  82. if at < len(t.steps) {
  83. t.steps = append(t.steps[:at+1], t.steps[at:]...)
  84. t.steps[at] = newStep
  85. } else {
  86. t.steps = append(t.steps, newStep)
  87. }
  88. }
  89. // Step adds step to trace
  90. func (t *Trace) Step(msg string, fields ...Field) {
  91. if !t.stepDisabled {
  92. t.steps = append(t.steps, step{time: time.Now(), msg: msg, fields: fields})
  93. }
  94. }
  95. // DisableStep sets the flag to prevent the trace from adding steps
  96. func (t *Trace) DisableStep() {
  97. t.stepDisabled = true
  98. }
  99. // EnableStep re-enable the trace to add steps
  100. func (t *Trace) EnableStep() {
  101. t.stepDisabled = false
  102. }
  103. func (t *Trace) AddField(fields ...Field) {
  104. for _, f := range fields {
  105. t.fields = append(t.fields, f)
  106. }
  107. }
  108. // Log dumps all steps in the Trace
  109. func (t *Trace) Log() {
  110. t.LogWithStepThreshold(0)
  111. }
  112. // LogIfLong dumps logs if the duration is longer than threshold
  113. func (t *Trace) LogIfLong(threshold time.Duration) {
  114. if time.Since(t.startTime) > threshold {
  115. stepThreshold := threshold / time.Duration(len(t.steps)+1)
  116. t.LogWithStepThreshold(stepThreshold)
  117. }
  118. }
  119. // LogWithStepThreshold only dumps step whose duration is longer than step threshold
  120. func (t *Trace) LogWithStepThreshold(threshold time.Duration) {
  121. msg, fs := t.logInfo(threshold)
  122. if t.lg != nil {
  123. t.lg.Info(msg, fs...)
  124. }
  125. }
  126. func (t *Trace) logInfo(threshold time.Duration) (string, []zap.Field) {
  127. endTime := time.Now()
  128. totalDuration := endTime.Sub(t.startTime)
  129. traceNum := rand.Int31()
  130. msg := fmt.Sprintf("trace[%d] %s", traceNum, t.operation)
  131. var steps []string
  132. lastStepTime := t.startTime
  133. for _, step := range t.steps {
  134. stepDuration := step.time.Sub(lastStepTime)
  135. if stepDuration > threshold {
  136. steps = append(steps, fmt.Sprintf("trace[%d] '%v' %s (duration: %v)",
  137. traceNum, step.msg, writeFields(step.fields), stepDuration))
  138. }
  139. lastStepTime = step.time
  140. }
  141. fs := []zap.Field{zap.String("detail", writeFields(t.fields)),
  142. zap.Duration("duration", totalDuration),
  143. zap.Time("start", t.startTime),
  144. zap.Time("end", endTime),
  145. zap.Strings("steps", steps)}
  146. return msg, fs
  147. }