github.com/imran-kn/cilium-fork@v1.6.9/pkg/spanstat/spanstat.go (about) 1 // Copyright 2018-2019 Authors of Cilium 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 15 package spanstat 16 17 import ( 18 "time" 19 20 "github.com/cilium/cilium/pkg/logging" 21 "github.com/cilium/cilium/pkg/logging/logfields" 22 "github.com/cilium/cilium/pkg/safetime" 23 ) 24 25 var ( 26 subSystem = "spanstat" 27 log = logging.DefaultLogger.WithField(logfields.LogSubsys, subSystem) 28 ) 29 30 // SpanStat measures the total duration of all time spent in between Start() 31 // and Stop() calls. 32 type SpanStat struct { 33 spanStart time.Time 34 successDuration time.Duration 35 failureDuration time.Duration 36 } 37 38 // Start creates a new SpanStat and starts it 39 func Start() *SpanStat { 40 s := &SpanStat{} 41 return s.Start() 42 } 43 44 // Start starts a new span 45 func (s *SpanStat) Start() *SpanStat { 46 s.spanStart = time.Now() 47 return s 48 } 49 50 // EndError calls End() based on the value of err 51 func (s *SpanStat) EndError(err error) *SpanStat { 52 return s.End(err == nil) 53 } 54 55 // End ends the current span and adds the measured duration to the total 56 // cumulated duration, and to the success or failure cumulated duration 57 // depending on the given success flag 58 func (s *SpanStat) End(success bool) *SpanStat { 59 if !s.spanStart.IsZero() { 60 d, _ := safetime.TimeSinceSafe(s.spanStart, log) 61 if success { 62 s.successDuration += d 63 } else { 64 s.failureDuration += d 65 } 66 } 67 s.spanStart = time.Time{} 68 return s 69 } 70 71 // Total returns the total duration of all spans measured, including both 72 // successes and failures 73 func (s *SpanStat) Total() time.Duration { 74 return s.successDuration + s.failureDuration 75 } 76 77 // SuccessTotal returns the total duration of all successful spans measured 78 func (s *SpanStat) SuccessTotal() time.Duration { 79 return s.successDuration 80 } 81 82 // FailureTotal returns the total duration of all unsuccessful spans measured 83 func (s *SpanStat) FailureTotal() time.Duration { 84 return s.failureDuration 85 } 86 87 // Reset rests the duration measurements 88 func (s *SpanStat) Reset() { 89 s.successDuration = 0 90 s.failureDuration = 0 91 } 92 93 // Seconds returns the number of seconds represents by the spanstat. If a span 94 // is still open, it is closed first. 95 func (s *SpanStat) Seconds() float64 { 96 if !s.spanStart.IsZero() { 97 s.End(true) 98 } 99 100 return s.Total().Seconds() 101 }