-
Notifications
You must be signed in to change notification settings - Fork 10
/
statsd.go
155 lines (115 loc) · 3.87 KB
/
statsd.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
package main
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)
const (
networkDeviceStatsMetricPrefix = "net.dev."
netstatStatsMetricPrefix = "net.netstat."
sockstatStatsMetricPrefix = "net.sockstat."
)
type LoggedStat struct {
Name string `json:"name"`
Time time.Time `json:"time"`
Stat interface{} `json:"stat"`
}
func collectNetworkDeviceStats() {
stats, err := readNetworkDeviceStats()
if err != nil {
Log.WithField("error", err).Error("Error reading network device stats. Can't collect network device stats.")
}
for _, stat := range stats {
if ifaceRegExp == nil || !ifaceRegExp.MatchString(stat.Iface) {
elem := reflect.ValueOf(&stat).Elem()
typeOfElem := elem.Type()
for i := 0; i < elem.NumField(); i++ {
field := typeOfElem.Field(i)
if field.Name == "Iface" {
continue
}
value := elem.Field(i).Uint()
_, collect := StatsMap[field.Name]
if metricName := field.Tag.Get("json"); collect && metricName != "" {
if err := StatsdClient.Gauge(strings.Join([]string{networkDeviceStatsMetricPrefix, metricName}, ""), float64(value), []string{"iface:" + stat.Iface}, 1); err != nil {
Log.WithField("error", err).Error("Couldn't submit event to statsd.")
}
}
}
}
}
}
func logNetworkDeviceStats() {
stats, err := readNetworkDeviceStats()
if err != nil {
Log.WithField("error", err).Error("Error reading network device stats. Can't log network device stats.")
}
loggedStat := LoggedStat{NetworkStatPath, time.Now(), stats}
jsonBytes, err := json.Marshal(loggedStat)
if err != nil {
Log.WithField("error", err).Errorf("Error JSON marshalling: %+v.", stats)
}
fmt.Println(string(jsonBytes))
}
func collectNetstatStats() {
stats, err := readNetstatStats()
if err != nil {
Log.WithField("error", err).Error("Error reading network device stats. Can't collect netstat stats.")
}
elem := reflect.ValueOf(stats).Elem()
typeOfElem := elem.Type()
for i := 0; i < elem.NumField(); i++ {
field := typeOfElem.Field(i)
value := elem.Field(i).Uint()
_, collect := StatsMap[field.Name]
if metricName := field.Tag.Get("json"); collect && metricName != "" {
if err := StatsdClient.Gauge(strings.Join([]string{netstatStatsMetricPrefix, metricName}, ""), float64(value), []string{}, 1); err != nil {
Log.WithField("error", err).Error("Couldn't submit event to statsd.")
}
}
}
}
func logNetstatStats() {
stats, err := readNetstatStats()
if err != nil {
Log.WithField("error", err).Error("Error reading network device stats. Can't log netstat stats.")
}
loggedStat := LoggedStat{NetstatStatPath, time.Now(), stats}
jsonBytes, err := json.Marshal(loggedStat)
if err != nil {
Log.WithField("error", err).Errorf("Error JSON marshalling: %+v.", stats)
}
fmt.Println(string(jsonBytes))
}
func collectSockstatStats() {
stats, err := readSockstatStats()
if err != nil {
Log.WithField("error", err).Error("Error reading sockstat stats. Can't collect netstat stats.")
}
elem := reflect.ValueOf(stats).Elem()
typeOfElem := elem.Type()
for i := 0; i < elem.NumField(); i++ {
field := typeOfElem.Field(i)
value := elem.Field(i).Uint()
_, collect := StatsMap[field.Name]
if metricName := field.Tag.Get("json"); collect && metricName != "" {
if err := StatsdClient.Gauge(strings.Join([]string{sockstatStatsMetricPrefix, metricName}, ""), float64(value), []string{}, 1); err != nil {
Log.WithField("error", err).Error("Couldn't submit event to statsd.")
}
}
}
}
func logSockstatStats() {
stats, err := readSockstatStats()
if err != nil {
Log.WithField("error", err).Error("Error reading sockstat stats. Can't log netstat stats.")
}
loggedStat := LoggedStat{SockstatStatPath, time.Now(), stats}
jsonBytes, err := json.Marshal(loggedStat)
if err != nil {
Log.WithField("error", err).Errorf("Error JSON marshalling: %+v.", stats)
}
fmt.Println(string(jsonBytes))
}