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