-
Notifications
You must be signed in to change notification settings - Fork 491
/
metadata.go
319 lines (300 loc) · 8.18 KB
/
metadata.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Package metadata provides metadata information between bosun and OpenTSDB.
package metadata // import "bosun.org/metadata"
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strings"
"sync"
"time"
"bosun.org/opentsdb"
"bosun.org/slog"
"bosun.org/util"
)
var (
// AuthToken is an optional string that sets the X-Access-Token HTTP header
// which is used to authenticate against Bosun
AuthToken string
)
// RateType is the type of rate for a metric: gauge, counter, or rate.
type RateType string
const (
// Unknown is a not-yet documented rate type.
Unknown RateType = ""
// Gauge rate type.
Gauge = "gauge"
// Counter rate type.
Counter = "counter"
// Rate rate type.
Rate = "rate"
)
// Unit is the unit for a metric.
type Unit string
const (
// None is a not-yet documented unit.
None Unit = ""
A = "A" // Amps
ActiveUsers = "active users" // Google Analytics
Alert = "alerts"
Abort = "aborts"
Bool = "bool"
BitsPerSecond = "bits per second"
Bytes = "bytes"
BytesPerSecond = "bytes per second"
C = "C" // Celsius
CacheHit = "cache hits"
CacheMiss = "cache misses"
Change = "changes"
Channel = "channels"
Check = "checks"
CHz = "CentiHertz"
Client = "clients"
Command = "commands"
Connection = "connections"
Consumer = "consumers"
Context = "contexts"
ContextSwitch = "context switches"
Count = ""
Document = "documents"
Enabled = "enabled"
Entropy = "entropy"
Error = "errors"
Event = ""
Eviction = "evictions"
Exchange = "exchanges"
Fault = "faults"
Flush = "flushes"
Files = "files"
Frame = "frames"
Fraction = "fraction"
Get = "gets"
GetExists = "get exists"
Group = "groups"
Incident = "incidents"
Interupt = "interupts"
InProgress = "in progress"
Item = "items"
KBytes = "kbytes"
Key = "keys"
Load = "load"
EMail = "emails"
MHz = "MHz" // MegaHertz
Megabit = "Mbit"
Merge = "merges"
Message = "messages"
MilliSecond = "milliseconds"
Nanosecond = "nanoseconds"
Node = "nodes"
Ok = "ok" // "OK" or not status, 0 = ok, 1 = not ok
Operation = "Operations"
Packet = "packets"
Page = "pages"
Pct = "percent" // Range of 0-100.
PerSecond = "per second"
Pool = "pools"
Process = "processes"
Priority = "priority"
Query = "queries"
Queue = "queues"
Ratio = "ratio"
Redispatch = "redispatches"
Refresh = "refreshes"
Replica = "replicas"
Retry = "retries"
Response = "responses"
Request = "requests"
RPM = "RPM" // Rotations per minute.
Scheduled = "scheduled"
Score = "score"
Second = "seconds"
Sector = "sectors"
Segment = "segments"
Server = "servers"
Session = "sessions"
Shard = "shards"
Slave = "slaves"
Socket = "sockets"
Suggest = "suggests"
StatusCode = "status code"
Resync = "resynchronizations"
Syscall = "system calls"
Thread = "threads"
Timestamp = "timestamp"
Transition = "transitions"
USD = "US dollars"
V = "V" // Volts
V10 = "tenth-Volts"
Vulnerabilities = "vulnerabilities"
Watt = "Watts"
Weight = "weight"
Yield = "yields"
)
// Metakey uniquely identifies a metadata entry.
type Metakey struct {
Metric string
Tags string
Name string
}
// TagSet returns m's tags.
func (m Metakey) TagSet() opentsdb.TagSet {
tags, err := opentsdb.ParseTags(m.Tags)
if err != nil {
return nil
}
return tags
}
var (
metadata = make(map[Metakey]interface{})
metalock sync.Mutex
metahost string
metafuncs []func()
metadebug bool
)
// AddMeta adds a metadata entry to memory, which is queued for later sending.
func AddMeta(metric string, tags opentsdb.TagSet, name string, value interface{}, setHost bool) {
if tags == nil {
tags = make(opentsdb.TagSet)
}
if _, present := tags["host"]; setHost && !present {
tags["host"] = util.GetHostManager().GetHostName()
}
if err := tags.Clean(); err != nil {
slog.Error(err)
return
}
ts := tags.Tags()
metalock.Lock()
defer metalock.Unlock()
prev, present := metadata[Metakey{metric, ts, name}]
if present && !reflect.DeepEqual(prev, value) {
slog.Infof("metadata changed for %s/%s/%s: %v to %v", metric, ts, name, prev, value)
go sendMetadata([]Metasend{{
Metric: metric,
Tags: tags,
Name: name,
Value: value,
}})
} else if metadebug {
slog.Infof("AddMeta for %s/%s/%s: %v", metric, ts, name, value)
}
metadata[Metakey{metric, ts, name}] = value
}
// AddMetricMeta is a convenience function to set the main metadata fields for a
// metric. Those fields are rate, unit, and description. If you need to document
// tag keys then use AddMeta.
func AddMetricMeta(metric string, rate RateType, unit Unit, desc string) {
AddMeta(metric, nil, "rate", rate, false)
AddMeta(metric, nil, "unit", unit, false)
AddMeta(metric, nil, "desc", desc, false)
}
// Init initializes the metadata send queue.
func Init(u *url.URL, debug bool) error {
mh, err := u.Parse("/api/metadata/put")
if err != nil {
return err
}
if strings.HasPrefix(mh.Host, ":") {
mh.Host = "localhost" + mh.Host
}
metahost = mh.String()
metadebug = debug
go collectMetadata()
return nil
}
var putFunction func(k Metakey, v interface{}) error
func InitF(debug bool, f func(k Metakey, v interface{}) error) error {
putFunction = f
metadebug = debug
go collectMetadata()
return nil
}
func collectMetadata() {
// Wait a bit so hopefully our collectors have run once and populated the
// metadata.
time.Sleep(time.Minute)
for {
FlushMetadata()
time.Sleep(time.Hour)
}
}
func FlushMetadata() {
for _, f := range metafuncs {
f()
}
if len(metadata) == 0 {
return
}
metalock.Lock()
ms := make([]Metasend, len(metadata))
i := 0
for k, v := range metadata {
ms[i] = Metasend{
Metric: k.Metric,
Tags: k.TagSet(),
Name: k.Name,
Value: v,
}
i++
}
metalock.Unlock()
sendMetadata(ms)
}
// Metasend is the struct for sending metadata to bosun.
type Metasend struct {
Metric string `json:",omitempty"`
Tags opentsdb.TagSet `json:",omitempty"`
Name string `json:",omitempty"`
Value interface{}
Time *time.Time `json:",omitempty"`
}
func sendMetadata(ms []Metasend) {
if putFunction != nil {
for _, m := range ms {
key := Metakey{
Metric: m.Metric,
Name: m.Name,
Tags: m.Tags.Tags(),
}
err := putFunction(key, m.Value)
if err != nil {
slog.Error(err)
continue
}
}
} else {
postMetadata(ms)
}
}
func postMetadata(ms []Metasend) {
b, err := json.Marshal(&ms)
if err != nil {
slog.Error(err)
return
}
req, err := http.NewRequest(http.MethodPost, metahost, bytes.NewBuffer(b))
if err != nil {
slog.Error(err)
return
}
req.Header.Set("Content-Type", "application/json")
if AuthToken != "" {
req.Header.Set("X-Access-Token", AuthToken)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
slog.Error(err)
return
}
defer resp.Body.Close()
// Drain up to 512 bytes and close the body to let the Transport reuse the connection
io.CopyN(ioutil.Discard, resp.Body, 512)
if resp.StatusCode != 204 {
slog.Errorln("bad metadata return:", resp.Status)
return
}
}