trace.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 Yunion
  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 trace
  15. import (
  16. "bytes"
  17. "fmt"
  18. "math/rand"
  19. "time"
  20. "yunion.io/x/log"
  21. )
  22. type traceStep struct {
  23. stepTime time.Time
  24. msg string
  25. }
  26. type Trace struct {
  27. name string
  28. startTime time.Time
  29. steps []traceStep
  30. }
  31. func New(name string) *Trace {
  32. return &Trace{name, time.Now(), nil}
  33. }
  34. func (t *Trace) Step(msg string) {
  35. if t.steps == nil {
  36. // traces almost always have less than 6 steps, do this to avoid more than a single allocation
  37. t.steps = make([]traceStep, 0, 6)
  38. }
  39. t.steps = append(t.steps, traceStep{time.Now(), msg})
  40. }
  41. func (t *Trace) Log() {
  42. // an explicit logging request should dump all the steps out at the higher level
  43. t.logWithStepThreshold(0)
  44. }
  45. func (t *Trace) logWithStepThreshold(stepThreshold time.Duration) {
  46. var buffer bytes.Buffer
  47. tracenum := rand.Int31()
  48. endTime := time.Now()
  49. totalTime := endTime.Sub(t.startTime)
  50. buffer.WriteString(fmt.Sprintf("Trace[%d]: %q (started: %v) (total time: %v):\n", tracenum, t.name, t.startTime, totalTime))
  51. lastStepTime := t.startTime
  52. for _, step := range t.steps {
  53. stepDuration := step.stepTime.Sub(lastStepTime)
  54. if stepThreshold == 0 || stepDuration > stepThreshold || log.V(4) {
  55. buffer.WriteString(fmt.Sprintf("Trace[%d]: [%v] [%v] %v\n", tracenum, step.stepTime.Sub(t.startTime), stepDuration, step.msg))
  56. }
  57. lastStepTime = step.stepTime
  58. }
  59. stepDuration := endTime.Sub(lastStepTime)
  60. if stepThreshold == 0 || stepDuration > stepThreshold || log.V(4) {
  61. buffer.WriteString(fmt.Sprintf("Trace[%d]: [%v] [%v] END\n", tracenum, endTime.Sub(t.startTime), stepDuration))
  62. }
  63. log.Infof(buffer.String())
  64. }
  65. func (t *Trace) LogIfLong(threshold time.Duration) {
  66. if time.Since(t.startTime) >= threshold {
  67. // if any step took more than it's share of the total allowed time, it deserves a higher log level
  68. stepThreshold := threshold / time.Duration(len(t.steps)+1)
  69. t.logWithStepThreshold(stepThreshold)
  70. }
  71. }
  72. func (t *Trace) TotalTime() time.Duration {
  73. return time.Since(t.startTime)
  74. }