This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
/
statsdreporter.go
158 lines (134 loc) · 5.38 KB
/
statsdreporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"regexp"
"strings"
"time"
"github.com/cactus/go-statsd-client/statsd"
)
const samplingRate = 1.0
//StatsdReporter is a implementation of Reporter interface with statsd
type StatsdReporter struct {
client statsd.Statter
addr string
tags map[string]string
}
// StatsdStopwatch is the struct which implements the Stopwatch interface to
// start and stop the timer
type StatsdStopwatch struct {
client statsd.Statter
startTime time.Time
name string
tags map[string]string
}
//NewStatsdReporter return a report which reports to statsd on the given addr
func NewStatsdReporter(HostPort string, Prefix string, FlushInterval time.Duration, FlushBytes int, tags map[string]string) Reporter {
client, err := statsd.NewBufferedClient(HostPort, Prefix, FlushInterval, FlushBytes)
if err != nil {
return nil
}
return &StatsdReporter{client: client, addr: HostPort, tags: tags}
}
// InitMetrics is used to initialize the metrics map with the respective type
func (r *StatsdReporter) InitMetrics(metricMap map[MetricName]MetricType) {
//pass
}
// GetChildReporter is used to get a child reporter from the parent
// this also makes sure we have all the tags from the parent in
// addition to the tags supplied here
func (r *StatsdReporter) GetChildReporter(tags map[string]string) Reporter {
client, err := statsd.NewBufferedClient(r.addr, "cherami", time.Second, 0)
if err != nil {
return nil
}
statsdReporter := &StatsdReporter{client: client, addr: r.addr, tags: tags}
mergeDictoRight(r.tags, statsdReporter.tags)
return statsdReporter
}
// GetTags gets the tags for this reporter object
func (r *StatsdReporter) GetTags() map[string]string {
return r.tags
}
// IncCounter should be used for Counter style metrics
func (r *StatsdReporter) IncCounter(name string, tags map[string]string, delta int64) {
reporterTags := mergeDicts(r.tags, tags)
r.client.Inc(metricstoPrefix(name, reporterTags), delta, samplingRate)
}
// UpdateGauge should be used for Gauge style metrics
func (r *StatsdReporter) UpdateGauge(name string, tags map[string]string, value int64) {
reporterTags := mergeDicts(r.tags, tags)
r.client.Gauge(metricstoPrefix(name, reporterTags), value, samplingRate)
}
func newStatsdStopWatch(client statsd.Statter, name string, tags map[string]string) Stopwatch {
return &StatsdStopwatch{client: client, name: name, startTime: time.Now(), tags: tags}
}
// StartTimer should be used for measuring latency.
// this returns a Stopwatch which can be used to stop the timer
func (r *StatsdReporter) StartTimer(name string, tags map[string]string) Stopwatch {
reporterTags := mergeDicts(r.tags, tags)
return newStatsdStopWatch(r.client, name, reporterTags)
}
// RecordTimer should be used for measuring latency when you cannot start the stop watch.
func (r *StatsdReporter) RecordTimer(name string, tags map[string]string, d time.Duration) {
reporterTags := mergeDicts(r.tags, tags)
r.client.TimingDuration(metricstoPrefix(name, reporterTags), d, samplingRate)
}
// Stop stops the stop watch and records the latency to M3
func (s *StatsdStopwatch) Stop() time.Duration {
d := time.Since(s.startTime)
s.client.TimingDuration(metricstoPrefix(s.name, s.tags), d, samplingRate)
return d
}
func mergeDictoRight(src map[string]string, dest map[string]string) {
for k, v := range src {
dest[k] = v
}
}
func mergeDicts(dic1 map[string]string, dic2 map[string]string) (resultDict map[string]string) {
resultDict = make(map[string]string)
mergeDictoRight(dic1, resultDict)
mergeDictoRight(dic2, resultDict)
return
}
// Cherami's metrics are tagged, but here we need to flatten
// it for statsd. We order tags by: Operation, Destination,
// ConsumerGroup, Hostname
var tagsFlattenOrder = []string{
OperationTagName,
DestinationTagName,
ConsumerGroupTagName,
HostnameTagName,
}
// MetricWithPrefix is the default mapping for metrics to statsd keys.
func metricstoPrefix(name string, tags map[string]string) string {
parts := []string{name}
for _, tag := range tagsFlattenOrder {
if v, ok := tags[tag]; ok {
parts = append(parts, clean(v))
}
}
return strings.Join(parts, ".")
}
var specialChars = regexp.MustCompile(`[{}/\\:\s.]`)
// clean replaces special characters [{}/\\:\s.] with '-'
func clean(keyPart string) string {
return specialChars.ReplaceAllString(keyPart, "-")
}