forked from atlassian/gostatsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
88 lines (79 loc) · 2.17 KB
/
metrics.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
package gostatsd
import (
"bytes"
"fmt"
"time"
)
// MetricType is an enumeration of all the possible types of Metric.
type MetricType byte
const (
_ = iota
// COUNTER is statsd counter type
COUNTER MetricType = iota
// TIMER is statsd timer type
TIMER
// GAUGE is statsd gauge type
GAUGE
// SET is statsd set type
SET
)
func (m MetricType) String() string {
switch m {
case SET:
return "set"
case GAUGE:
return "gauge"
case TIMER:
return "timer"
case COUNTER:
return "counter"
}
return "unknown"
}
// Metric represents a single data collected datapoint.
type Metric struct {
Name string // The name of the metric
Value float64 // The numeric value of the metric
Tags Tags // The tags for the metric
StringValue string // The string value for some metrics e.g. Set
Hostname string // Hostname of the source of the metric
SourceIP IP // IP of the source of the metric
Type MetricType // The type of metric
}
func (m *Metric) String() string {
return fmt.Sprintf("{%s, %s, %f, %s, %v}", m.Type, m.Name, m.Value, m.StringValue, m.Tags)
}
// AggregatedMetrics is an interface for aggregated metrics.
type AggregatedMetrics interface {
MetricsName() string
Delete(string)
DeleteChild(string, string)
HasChildren(string) bool
}
// MetricMap is used for storing aggregated Metric values.
// The keys of each map are metric names.
type MetricMap struct {
FlushInterval time.Duration
Counters Counters
Timers Timers
Gauges Gauges
Sets Sets
}
func (m *MetricMap) String() string {
buf := new(bytes.Buffer)
m.Counters.Each(func(k, tags string, counter Counter) {
fmt.Fprintf(buf, "stats.counter.%s: %d tags=%s\n", k, counter.Value, tags)
})
m.Timers.Each(func(k, tags string, timer Timer) {
for _, value := range timer.Values {
fmt.Fprintf(buf, "stats.timer.%s: %f tags=%s\n", k, value, tags)
}
})
m.Gauges.Each(func(k, tags string, gauge Gauge) {
fmt.Fprintf(buf, "stats.gauge.%s: %f tags=%s\n", k, gauge.Value, tags)
})
m.Sets.Each(func(k, tags string, set Set) {
fmt.Fprintf(buf, "stats.set.%s: %d tags=%s\n", k, len(set.Values), tags)
})
return buf.String()
}