github.com/uber-go/tally/v4@v4.1.17/statsd/reporter.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 statsd 22 23 import ( 24 "fmt" 25 "math" 26 "strconv" 27 "time" 28 29 "github.com/cactus/go-statsd-client/v5/statsd" 30 tally "github.com/uber-go/tally/v4" 31 ) 32 33 const ( 34 // DefaultHistogramBucketNamePrecision is the default 35 // precision to use when formatting the metric name 36 // with the histogram bucket bound values. 37 DefaultHistogramBucketNamePrecision = uint(6) 38 ) 39 40 type cactusStatsReporter struct { 41 statter statsd.Statter 42 sampleRate float32 43 bucketFmt string 44 } 45 46 // Options is a set of options for the tally reporter. 47 type Options struct { 48 // SampleRate is the metrics emission sample rate. If you 49 // do not set this value it will be set to 1. 50 SampleRate float32 51 52 // HistogramBucketNamePrecision is the precision to use when 53 // formatting the metric name with the histogram bucket bound values. 54 // By default this will be set to the const DefaultHistogramBucketPrecision. 55 HistogramBucketNamePrecision uint 56 } 57 58 // NewReporter wraps a statsd.Statter for use with tally. Use either 59 // statsd.NewClient or statsd.NewBufferedClient. 60 func NewReporter(statsd statsd.Statter, opts Options) tally.StatsReporter { 61 var nilSampleRate float32 62 if opts.SampleRate == nilSampleRate { 63 opts.SampleRate = 1.0 64 } 65 if opts.HistogramBucketNamePrecision == 0 { 66 opts.HistogramBucketNamePrecision = DefaultHistogramBucketNamePrecision 67 } 68 return &cactusStatsReporter{ 69 statter: statsd, 70 sampleRate: opts.SampleRate, 71 bucketFmt: "%." + strconv.Itoa(int(opts.HistogramBucketNamePrecision)) + "f", 72 } 73 } 74 75 func (r *cactusStatsReporter) ReportCounter(name string, tags map[string]string, value int64) { 76 r.statter.Inc(name, value, r.sampleRate) 77 } 78 79 func (r *cactusStatsReporter) ReportGauge(name string, tags map[string]string, value float64) { 80 r.statter.Gauge(name, int64(value), r.sampleRate) 81 } 82 83 func (r *cactusStatsReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) { 84 r.statter.TimingDuration(name, interval, r.sampleRate) 85 } 86 87 func (r *cactusStatsReporter) ReportHistogramValueSamples( 88 name string, 89 tags map[string]string, 90 buckets tally.Buckets, 91 bucketLowerBound, 92 bucketUpperBound float64, 93 samples int64, 94 ) { 95 r.statter.Inc( 96 fmt.Sprintf("%s.%s-%s", name, 97 r.valueBucketString(bucketLowerBound), 98 r.valueBucketString(bucketUpperBound)), 99 samples, r.sampleRate) 100 } 101 102 func (r *cactusStatsReporter) ReportHistogramDurationSamples( 103 name string, 104 tags map[string]string, 105 buckets tally.Buckets, 106 bucketLowerBound, 107 bucketUpperBound time.Duration, 108 samples int64, 109 ) { 110 r.statter.Inc( 111 fmt.Sprintf("%s.%s-%s", name, 112 r.durationBucketString(bucketLowerBound), 113 r.durationBucketString(bucketUpperBound)), 114 samples, r.sampleRate) 115 } 116 117 func (r *cactusStatsReporter) valueBucketString( 118 upperBound float64, 119 ) string { 120 if upperBound == math.MaxFloat64 { 121 return "infinity" 122 } 123 if upperBound == -math.MaxFloat64 { 124 return "-infinity" 125 } 126 return fmt.Sprintf(r.bucketFmt, upperBound) 127 } 128 129 func (r *cactusStatsReporter) durationBucketString( 130 upperBound time.Duration, 131 ) string { 132 if upperBound == time.Duration(math.MaxInt64) { 133 return "infinity" 134 } 135 if upperBound == time.Duration(math.MinInt64) { 136 return "-infinity" 137 } 138 return upperBound.String() 139 } 140 141 func (r *cactusStatsReporter) Capabilities() tally.Capabilities { 142 return r 143 } 144 145 func (r *cactusStatsReporter) Reporting() bool { 146 return true 147 } 148 149 func (r *cactusStatsReporter) Tagging() bool { 150 return false 151 } 152 153 func (r *cactusStatsReporter) Flush() { 154 // no-op 155 }