trace.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package trace
  14. import (
  15. "bytes"
  16. "fmt"
  17. "math/rand"
  18. "time"
  19. "k8s.io/klog"
  20. )
  21. type traceStep struct {
  22. stepTime time.Time
  23. msg string
  24. }
  25. // Trace keeps track of a set of "steps" and allows us to log a specific
  26. // step if it took longer than its share of the total allowed time
  27. type Trace struct {
  28. name string
  29. startTime time.Time
  30. steps []traceStep
  31. }
  32. // New creates a Trace with the specified name
  33. func New(name string) *Trace {
  34. return &Trace{name, time.Now(), nil}
  35. }
  36. // Step adds a new step with a specific message
  37. func (t *Trace) Step(msg string) {
  38. if t.steps == nil {
  39. // traces almost always have less than 6 steps, do this to avoid more than a single allocation
  40. t.steps = make([]traceStep, 0, 6)
  41. }
  42. t.steps = append(t.steps, traceStep{time.Now(), msg})
  43. }
  44. // Log is used to dump all the steps in the Trace
  45. func (t *Trace) Log() {
  46. // an explicit logging request should dump all the steps out at the higher level
  47. t.logWithStepThreshold(0)
  48. }
  49. func (t *Trace) logWithStepThreshold(stepThreshold time.Duration) {
  50. var buffer bytes.Buffer
  51. tracenum := rand.Int31()
  52. endTime := time.Now()
  53. totalTime := endTime.Sub(t.startTime)
  54. buffer.WriteString(fmt.Sprintf("Trace[%d]: %q (started: %v) (total time: %v):\n", tracenum, t.name, t.startTime, totalTime))
  55. lastStepTime := t.startTime
  56. for _, step := range t.steps {
  57. stepDuration := step.stepTime.Sub(lastStepTime)
  58. if stepThreshold == 0 || stepDuration > stepThreshold || klog.V(4) {
  59. buffer.WriteString(fmt.Sprintf("Trace[%d]: [%v] [%v] %v\n", tracenum, step.stepTime.Sub(t.startTime), stepDuration, step.msg))
  60. }
  61. lastStepTime = step.stepTime
  62. }
  63. stepDuration := endTime.Sub(lastStepTime)
  64. if stepThreshold == 0 || stepDuration > stepThreshold || klog.V(4) {
  65. buffer.WriteString(fmt.Sprintf("Trace[%d]: [%v] [%v] END\n", tracenum, endTime.Sub(t.startTime), stepDuration))
  66. }
  67. klog.Info(buffer.String())
  68. }
  69. // LogIfLong is used to dump steps that took longer than its share
  70. func (t *Trace) LogIfLong(threshold time.Duration) {
  71. if time.Since(t.startTime) >= threshold {
  72. // if any step took more than it's share of the total allowed time, it deserves a higher log level
  73. stepThreshold := threshold / time.Duration(len(t.steps)+1)
  74. t.logWithStepThreshold(stepThreshold)
  75. }
  76. }
  77. // TotalTime can be used to figure out how long it took since the Trace was created
  78. func (t *Trace) TotalTime() time.Duration {
  79. return time.Since(t.startTime)
  80. }