go.uber.org/cadence@v1.2.9/internal/common/metrics/capturingStatsReporter.go (about) 1 // Copyright (c) 2017 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 metrics 22 23 import ( 24 "io" 25 "time" 26 27 "github.com/uber-go/tally" 28 ) 29 30 // NewMetricsScope returns a new metric scope 31 func NewMetricsScope(isReplay *bool) (tally.Scope, io.Closer, *CapturingStatsReporter) { 32 reporter := &CapturingStatsReporter{} 33 opts := tally.ScopeOptions{Reporter: reporter} 34 scope, closer := tally.NewRootScope(opts, time.Second) 35 return WrapScope(isReplay, scope, &realClock{}), closer, reporter 36 } 37 38 // NewTaggedMetricsScope return NewTaggedMetricsScope 39 func NewTaggedMetricsScope() (*TaggedScope, io.Closer, *CapturingStatsReporter) { 40 isReplay := false 41 scope, closer, reporter := NewMetricsScope(&isReplay) 42 return &TaggedScope{Scope: scope}, closer, reporter 43 } 44 45 type realClock struct { 46 } 47 48 func (c *realClock) Now() time.Time { 49 return time.Now() 50 } 51 52 // CapturingStatsReporter is a reporter used by tests to capture the metric so we can verify our tests. 53 type CapturingStatsReporter struct { 54 counts []CapturedCount 55 gauges []CapturedGauge 56 timers []CapturedTimer 57 histogramValueSamples []CapturedHistogramValueSamples 58 histogramDurationSamples []CapturedHistogramDurationSamples 59 capabilities int 60 flush int 61 } 62 63 // HistogramDurationSamples return HistogramDurationSamples 64 func (c *CapturingStatsReporter) HistogramDurationSamples() []CapturedHistogramDurationSamples { 65 return c.histogramDurationSamples 66 } 67 68 // HistogramValueSamples return HistogramValueSamples 69 func (c *CapturingStatsReporter) HistogramValueSamples() []CapturedHistogramValueSamples { 70 return c.histogramValueSamples 71 } 72 73 // Timers return Timers 74 func (c *CapturingStatsReporter) Timers() []CapturedTimer { 75 return c.timers 76 } 77 78 // Gauges return Gauges 79 func (c *CapturingStatsReporter) Gauges() []CapturedGauge { 80 return c.gauges 81 } 82 83 // Counts return Counts 84 func (c *CapturingStatsReporter) Counts() []CapturedCount { 85 return c.counts 86 } 87 88 // CapturedCount has associated name, tags and value 89 type CapturedCount struct { 90 name string 91 tags map[string]string 92 value int64 93 } 94 95 // Value return the value of CapturedCount 96 func (c *CapturedCount) Value() int64 { 97 return c.value 98 } 99 100 // Tags return CapturedCount tags 101 func (c *CapturedCount) Tags() map[string]string { 102 return c.tags 103 } 104 105 // Name return the name of CapturedCount 106 func (c *CapturedCount) Name() string { 107 return c.name 108 } 109 110 // CapturedGauge has CapturedGauge name, tag and values 111 type CapturedGauge struct { 112 name string 113 tags map[string]string 114 value float64 115 } 116 117 // Value return the value of CapturedGauge 118 func (c *CapturedGauge) Value() float64 { 119 return c.value 120 } 121 122 // Tags return the tags of CapturedGauge 123 func (c *CapturedGauge) Tags() map[string]string { 124 return c.tags 125 } 126 127 // Name return the name of CapturedGauge 128 func (c *CapturedGauge) Name() string { 129 return c.name 130 } 131 132 // CapturedTimer has related name , tags and value 133 type CapturedTimer struct { 134 name string 135 tags map[string]string 136 value time.Duration 137 } 138 139 // Value return the value of CapturedTimer 140 func (c *CapturedTimer) Value() time.Duration { 141 return c.value 142 } 143 144 // Tags return the tag of CapturedTimer 145 func (c *CapturedTimer) Tags() map[string]string { 146 return c.tags 147 } 148 149 // Name return the name of CapturedTimer 150 func (c *CapturedTimer) Name() string { 151 return c.name 152 } 153 154 // CapturedHistogramValueSamples has related information for CapturedHistogramValueSamples 155 type CapturedHistogramValueSamples struct { 156 name string 157 tags map[string]string 158 bucketLowerBound float64 159 bucketUpperBound float64 160 samples int64 161 } 162 163 // CapturedHistogramDurationSamples has related information for CapturedHistogramDurationSamples 164 type CapturedHistogramDurationSamples struct { 165 name string 166 tags map[string]string 167 bucketLowerBound time.Duration 168 bucketUpperBound time.Duration 169 samples int64 170 } 171 172 // ReportCounter reports the counts 173 func (c *CapturingStatsReporter) ReportCounter( 174 name string, 175 tags map[string]string, 176 value int64, 177 ) { 178 c.counts = append(c.counts, CapturedCount{name, tags, value}) 179 } 180 181 // ReportGauge reports the gauges 182 func (c *CapturingStatsReporter) ReportGauge( 183 name string, 184 tags map[string]string, 185 value float64, 186 ) { 187 c.gauges = append(c.gauges, CapturedGauge{name, tags, value}) 188 } 189 190 // ReportTimer reports timers 191 func (c *CapturingStatsReporter) ReportTimer( 192 name string, 193 tags map[string]string, 194 value time.Duration, 195 ) { 196 c.timers = append(c.timers, CapturedTimer{name, tags, value}) 197 } 198 199 // ReportHistogramValueSamples reports histogramValueSamples 200 func (c *CapturingStatsReporter) ReportHistogramValueSamples( 201 name string, 202 tags map[string]string, 203 buckets tally.Buckets, 204 bucketLowerBound, 205 bucketUpperBound float64, 206 samples int64, 207 ) { 208 elem := CapturedHistogramValueSamples{name, tags, 209 bucketLowerBound, bucketUpperBound, samples} 210 c.histogramValueSamples = append(c.histogramValueSamples, elem) 211 } 212 213 // ReportHistogramDurationSamples reports ReportHistogramDurationSamples 214 func (c *CapturingStatsReporter) ReportHistogramDurationSamples( 215 name string, 216 tags map[string]string, 217 buckets tally.Buckets, 218 bucketLowerBound, 219 bucketUpperBound time.Duration, 220 samples int64, 221 ) { 222 elem := CapturedHistogramDurationSamples{name, tags, 223 bucketLowerBound, bucketUpperBound, samples} 224 c.histogramDurationSamples = append(c.histogramDurationSamples, elem) 225 } 226 227 // Capabilities return tally.Capabilities 228 func (c *CapturingStatsReporter) Capabilities() tally.Capabilities { 229 c.capabilities++ 230 return c 231 } 232 233 // Reporting will always return true 234 func (c *CapturingStatsReporter) Reporting() bool { 235 return true 236 } 237 238 // Tagging will always return true 239 func (c *CapturingStatsReporter) Tagging() bool { 240 return true 241 } 242 243 // Flush will add one to flush 244 func (c *CapturingStatsReporter) Flush() { 245 c.flush++ 246 }