github.com/uber-go/tally/v4@v4.1.17/types.go (about) 1 // Copyright (c) 2021 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package tally 22 23 import ( 24 "fmt" 25 "sort" 26 "time" 27 ) 28 29 // Scope is a namespace wrapper around a stats reporter, ensuring that 30 // all emitted values have a given prefix or set of tags. 31 // 32 // IMPORTANT: When using Prometheus reporters, users must take care to 33 // not create metrics from both parent scopes and subscopes 34 // that have the same metric name but different tag keys, 35 // as metric allocation will panic. 36 type Scope interface { 37 // Counter returns the Counter object corresponding to the name. 38 Counter(name string) Counter 39 40 // Gauge returns the Gauge object corresponding to the name. 41 Gauge(name string) Gauge 42 43 // Timer returns the Timer object corresponding to the name. 44 Timer(name string) Timer 45 46 // Histogram returns the Histogram object corresponding to the name. 47 // To use default value and duration buckets configured for the scope 48 // simply pass tally.DefaultBuckets or nil. 49 // You can use tally.ValueBuckets{x, y, ...} for value buckets. 50 // You can use tally.DurationBuckets{x, y, ...} for duration buckets. 51 // You can use tally.MustMakeLinearValueBuckets(start, width, count) for linear values. 52 // You can use tally.MustMakeLinearDurationBuckets(start, width, count) for linear durations. 53 // You can use tally.MustMakeExponentialValueBuckets(start, factor, count) for exponential values. 54 // You can use tally.MustMakeExponentialDurationBuckets(start, factor, count) for exponential durations. 55 Histogram(name string, buckets Buckets) Histogram 56 57 // Tagged returns a new child scope with the given tags and current tags. 58 Tagged(tags map[string]string) Scope 59 60 // SubScope returns a new child scope appending a further name prefix. 61 SubScope(name string) Scope 62 63 // Capabilities returns a description of metrics reporting capabilities. 64 Capabilities() Capabilities 65 } 66 67 // Counter is the interface for emitting counter type metrics. 68 type Counter interface { 69 // Inc increments the counter by a delta. 70 Inc(delta int64) 71 } 72 73 // Gauge is the interface for emitting gauge metrics. 74 type Gauge interface { 75 // Update sets the gauges absolute value. 76 Update(value float64) 77 } 78 79 // Timer is the interface for emitting timer metrics. 80 type Timer interface { 81 // Record a specific duration directly. 82 Record(value time.Duration) 83 84 // Start gives you back a specific point in time to report via Stop. 85 Start() Stopwatch 86 } 87 88 // Histogram is the interface for emitting histogram metrics 89 type Histogram interface { 90 // RecordValue records a specific value directly. 91 // Will use the configured value buckets for the histogram. 92 RecordValue(value float64) 93 94 // RecordDuration records a specific duration directly. 95 // Will use the configured duration buckets for the histogram. 96 RecordDuration(value time.Duration) 97 98 // Start gives you a specific point in time to then record a duration. 99 // Will use the configured duration buckets for the histogram. 100 Start() Stopwatch 101 } 102 103 // Stopwatch is a helper for simpler tracking of elapsed time, use the 104 // Stop() method to report time elapsed since its created back to the 105 // timer or histogram. 106 type Stopwatch struct { 107 start time.Time 108 recorder StopwatchRecorder 109 } 110 111 // NewStopwatch creates a new immutable stopwatch for recording the start 112 // time to a stopwatch reporter. 113 func NewStopwatch(start time.Time, r StopwatchRecorder) Stopwatch { 114 return Stopwatch{start: start, recorder: r} 115 } 116 117 // Stop reports time elapsed since the stopwatch start to the recorder. 118 func (sw Stopwatch) Stop() { 119 sw.recorder.RecordStopwatch(sw.start) 120 } 121 122 // StopwatchRecorder is a recorder that is called when a stopwatch is 123 // stopped with Stop(). 124 type StopwatchRecorder interface { 125 RecordStopwatch(stopwatchStart time.Time) 126 } 127 128 // Buckets is an interface that can represent a set of buckets 129 // either as float64s or as durations. 130 type Buckets interface { 131 fmt.Stringer 132 sort.Interface 133 134 // AsValues returns a representation of the buckets as float64s 135 AsValues() []float64 136 137 // AsDurations returns a representation of the buckets as time.Durations 138 AsDurations() []time.Duration 139 } 140 141 // BucketPair describes the lower and upper bounds 142 // for a derived bucket from a buckets set. 143 type BucketPair interface { 144 LowerBoundValue() float64 145 UpperBoundValue() float64 146 LowerBoundDuration() time.Duration 147 UpperBoundDuration() time.Duration 148 } 149 150 // Capabilities is a description of metrics reporting capabilities. 151 type Capabilities interface { 152 // Reporting returns whether the reporter has the ability to actively report. 153 Reporting() bool 154 155 // Tagging returns whether the reporter has the capability for tagged metrics. 156 Tagging() bool 157 }