forked from juju/juju
/
metrics.go
516 lines (460 loc) · 13.7 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/juju/charm/v9"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/mgo/v2"
"github.com/juju/mgo/v2/bson"
"github.com/juju/mgo/v2/txn"
"github.com/juju/names/v4"
)
var metricsLogger = loggo.GetLogger("juju.state.metrics")
const (
CleanupAge = time.Hour * 24
)
// MetricBatch represents a batch of metrics reported from a unit.
// These will be received from the unit in batches.
// The main contents of the metric (key, value) is defined
// by the charm author and sent from the unit via a call to
// add-metric
type MetricBatch struct {
st *State
doc metricBatchDoc
}
type metricBatchDoc struct {
UUID string `bson:"_id"`
ModelUUID string `bson:"model-uuid"`
Unit string `bson:"unit"`
CharmURL string `bson:"charmurl"`
Sent bool `bson:"sent"`
DeleteTime time.Time `bson:"delete-time"`
Created time.Time `bson:"created"`
Metrics []Metric `bson:"metrics"`
Credentials []byte `bson:"credentials"`
SLACredentials []byte `bson:"sla-credentials,omitempty"`
}
// Metric represents a single Metric.
type Metric struct {
Key string `bson:"key"`
Value string `bson:"value"`
Time time.Time `bson:"time"`
Labels map[string]string `bson:"labels,omitempty"`
}
type byTime []Metric
// Len implements sort.Interface.
func (t byTime) Len() int { return len(t) }
// Swap implements sort.Interface.
func (t byTime) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
// Less implements sort.Interface.
func (t byTime) Less(i, j int) bool {
return t[i].Time.Before(t[j].Time)
}
type byKey []Metric
// Len implements sort.Interface.
func (t byKey) Len() int { return len(t) }
// Swap implements sort.Interface.
func (t byKey) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
// Less implements sort.Interface.
func (t byKey) Less(i, j int) bool {
if t[i].Key == t[j].Key {
return labelsKey(t[i].Labels) < labelsKey(t[j].Labels)
}
return t[i].Key < t[j].Key
}
func labelsKey(m map[string]string) string {
var result []string
for k, v := range m {
result = append(result, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(result)
return strings.Join(result, ",")
}
// validate checks that the MetricBatch contains valid metrics.
func (m *MetricBatch) validate() error {
charmURL, err := charm.ParseURL(m.doc.CharmURL)
if err != nil {
return errors.Trace(err)
}
chrm, err := m.st.Charm(charmURL)
if err != nil {
return errors.Trace(err)
}
chrmMetrics := chrm.Metrics()
if chrmMetrics == nil {
return errors.Errorf("charm doesn't implement metrics")
}
for _, m := range m.doc.Metrics {
if err := chrmMetrics.ValidateMetric(m.Key, m.Value); err != nil {
return errors.Trace(err)
}
}
return nil
}
// BatchParam contains the properties of the metrics batch used when creating a metrics
// batch.
type BatchParam struct {
UUID string
CharmURL string
Created time.Time
Metrics []Metric
Unit names.UnitTag
}
// ModelBatchParam contains the properties of a metric batch for a model
// The model uuid will be attenuated in the call to AddModelMetrics.
type ModelBatchParam struct {
UUID string
Created time.Time
Metrics []Metric
}
// AddMetrics adds a new batch of metrics to the database.
func (st *State) AddMetrics(batch BatchParam) (*MetricBatch, error) {
if len(batch.Metrics) == 0 {
return nil, errors.New("cannot add a batch of 0 metrics")
}
charmURL, err := charm.ParseURL(batch.CharmURL)
if err != nil {
return nil, errors.NewNotValid(err, "could not parse charm URL")
}
unit, err := st.Unit(batch.Unit.Id())
if err != nil {
return nil, errors.Trace(err)
}
application, err := unit.Application()
if err != nil {
return nil, errors.Trace(err)
}
slaCreds, err := st.SLACredential()
if err != nil {
return nil, errors.Trace(err)
}
metric := &MetricBatch{
st: st,
doc: metricBatchDoc{
UUID: batch.UUID,
ModelUUID: st.ModelUUID(),
Unit: batch.Unit.Id(),
CharmURL: charmURL.String(),
Sent: false,
Created: batch.Created,
Metrics: batch.Metrics,
Credentials: application.MetricCredentials(),
SLACredentials: slaCreds,
},
}
if err := metric.validate(); err != nil {
return nil, err
}
buildTxn := func(attempt int) ([]txn.Op, error) {
if attempt > 0 {
notDead, err := isNotDead(st, unitsC, batch.Unit.Id())
if err != nil || !notDead {
return nil, errors.NotFoundf(batch.Unit.Id())
}
exists, err := st.MetricBatch(batch.UUID)
if exists != nil && err == nil {
return nil, errors.AlreadyExistsf("metrics batch UUID %q", batch.UUID)
}
if !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
}
ops := []txn.Op{{
C: unitsC,
Id: st.docID(batch.Unit.Id()),
Assert: notDeadDoc,
}, {
C: metricsC,
Id: metric.UUID(),
Assert: txn.DocMissing,
Insert: &metric.doc,
}}
return ops, nil
}
err = st.db().Run(buildTxn)
if err != nil {
return nil, errors.Trace(err)
}
return metric, nil
}
// AddModelMetrics adds a new model-centric batch of metrics to the database.
func (st *State) AddModelMetrics(batch ModelBatchParam) (*MetricBatch, error) {
if len(batch.Metrics) == 0 {
return nil, errors.New("cannot add a batch of 0 metrics")
}
slaCreds, err := st.SLACredential()
if err != nil {
return nil, errors.Trace(err)
}
metric := &MetricBatch{
st: st,
doc: metricBatchDoc{
UUID: batch.UUID,
ModelUUID: st.ModelUUID(),
Sent: false,
Created: batch.Created,
Metrics: batch.Metrics,
SLACredentials: slaCreds,
},
}
buildTxn := func(attempt int) ([]txn.Op, error) {
if attempt > 0 {
exists, err := st.MetricBatch(batch.UUID)
if exists != nil && err == nil {
return nil, errors.AlreadyExistsf("metrics batch UUID %q", batch.UUID)
}
if !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
}
ops := []txn.Op{{
C: metricsC,
Id: metric.UUID(),
Assert: txn.DocMissing,
Insert: &metric.doc,
}}
return ops, nil
}
err = st.db().Run(buildTxn)
if err != nil {
return nil, errors.Trace(err)
}
return metric, nil
}
// AllMetricBatches returns all metric batches currently stored in state.
// TODO (tasdomas): this method is currently only used in the uniter worker test -
// it needs to be modified to restrict the scope of the values it
// returns if it is to be used outside of tests.
func (st *State) AllMetricBatches() ([]MetricBatch, error) {
c, closer := st.db().GetCollection(metricsC)
defer closer()
docs := []metricBatchDoc{}
err := c.Find(nil).All(&docs)
if err != nil {
return nil, errors.Trace(err)
}
results := make([]MetricBatch, len(docs))
for i, doc := range docs {
results[i] = MetricBatch{st: st, doc: doc}
}
return results, nil
}
func (st *State) queryMetricBatches(query bson.M) ([]MetricBatch, error) {
c, closer := st.db().GetCollection(metricsC)
defer closer()
docs := []metricBatchDoc{}
err := c.Find(query).Sort("created").All(&docs)
if err != nil {
return nil, errors.Trace(err)
}
results := make([]MetricBatch, len(docs))
for i, doc := range docs {
results[i] = MetricBatch{st: st, doc: doc}
}
return results, nil
}
// MetricBatchesForUnit returns metric batches for the given unit.
func (st *State) MetricBatchesForUnit(unit string) ([]MetricBatch, error) {
_, err := st.Unit(unit)
if err != nil {
return nil, errors.Trace(err)
}
return st.queryMetricBatches(bson.M{"unit": unit})
}
// MetricBatchesForModel returns metric batches for all the units in the model.
func (st *State) MetricBatchesForModel() ([]MetricBatch, error) {
return st.queryMetricBatches(bson.M{"model-uuid": st.ModelUUID()})
}
// MetricBatchesForApplication returns metric batches for the given application.
func (st *State) MetricBatchesForApplication(application string) ([]MetricBatch, error) {
app, err := st.Application(application)
if err != nil {
return nil, errors.Trace(err)
}
units, err := app.AllUnits()
if err != nil {
return nil, errors.Trace(err)
}
unitNames := make([]bson.M, len(units))
for i, u := range units {
unitNames[i] = bson.M{"unit": u.Name()}
}
return st.queryMetricBatches(bson.M{"$or": unitNames})
}
// MetricBatch returns the metric batch with the given id.
func (st *State) MetricBatch(id string) (*MetricBatch, error) {
c, closer := st.db().GetCollection(metricsC)
defer closer()
doc := metricBatchDoc{}
err := c.Find(bson.M{"_id": id}).One(&doc)
if err == mgo.ErrNotFound {
return nil, errors.NotFoundf("metric %v", id)
}
if err != nil {
return nil, err
}
return &MetricBatch{st: st, doc: doc}, nil
}
// CleanupOldMetrics looks for metrics that are 24 hours old (or older)
// and have been sent. Any metrics it finds are deleted.
func (st *State) CleanupOldMetrics() error {
now := st.clock().Now()
metrics, closer := st.db().GetCollection(metricsC)
defer closer()
// Nothing else in the system will interact with sent metrics, and nothing needs
// to watch them either; so in this instance it's safe to do an end run around the
// mgo/txn package. See State.cleanupRelationSettings for a similar situation.
metricsW := metrics.Writeable()
// TODO (mattyw) iter over this.
info, err := metricsW.RemoveAll(bson.M{
"model-uuid": st.ModelUUID(),
"sent": true,
"delete-time": bson.M{"$lte": now},
})
if err == nil {
metricsLogger.Tracef("cleanup removed %d metrics", info.Removed)
}
return errors.Trace(err)
}
// MetricsToSend returns batchSize metrics that need to be sent
// to the collector
func (st *State) MetricsToSend(batchSize int) ([]*MetricBatch, error) {
var docs []metricBatchDoc
c, closer := st.db().GetCollection(metricsC)
defer closer()
q := bson.M{
"model-uuid": st.ModelUUID(),
"sent": false,
}
err := c.Find(q).Limit(batchSize).All(&docs)
if err != nil {
return nil, errors.Trace(err)
}
batch := make([]*MetricBatch, len(docs))
for i, doc := range docs {
batch[i] = &MetricBatch{st: st, doc: doc}
}
return batch, nil
}
// CountOfUnsentMetrics returns the number of metrics that
// haven't been sent to the collection service.
func (st *State) CountOfUnsentMetrics() (int, error) {
c, closer := st.db().GetCollection(metricsC)
defer closer()
return c.Find(bson.M{
"model-uuid": st.ModelUUID(),
"sent": false,
}).Count()
}
// CountOfSentMetrics returns the number of metrics that
// have been sent to the collection service and have not
// been removed by the cleanup worker.
func (st *State) CountOfSentMetrics() (int, error) {
c, closer := st.db().GetCollection(metricsC)
defer closer()
return c.Find(bson.M{
"model-uuid": st.ModelUUID(),
"sent": true,
}).Count()
}
// MarshalJSON defines how the MetricBatch type should be
// converted to json.
func (m *MetricBatch) MarshalJSON() ([]byte, error) {
return json.Marshal(m.doc)
}
// UUID returns to uuid of the metric.
func (m *MetricBatch) UUID() string {
return m.doc.UUID
}
// ModelUUID returns the model UUID this metric applies to.
func (m *MetricBatch) ModelUUID() string {
return m.doc.ModelUUID
}
// Unit returns the name of the unit this metric was generated in.
func (m *MetricBatch) Unit() string {
return m.doc.Unit
}
// CharmURL returns the charm url for the charm this metric was generated in.
func (m *MetricBatch) CharmURL() string {
return m.doc.CharmURL
}
// Created returns the time this metric batch was created.
func (m *MetricBatch) Created() time.Time {
return m.doc.Created
}
// Sent returns a flag to tell us if this metric has been sent to the metric
// collection service
func (m *MetricBatch) Sent() bool {
return m.doc.Sent
}
// Metrics returns the metrics in this batch.
func (m *MetricBatch) Metrics() []Metric {
result := make([]Metric, len(m.doc.Metrics))
copy(result, m.doc.Metrics)
return result
}
// UniqueMetrics returns only the last value for each
// metric key in this batch.
func (m *MetricBatch) UniqueMetrics() []Metric {
metrics := m.Metrics()
sort.Sort(byTime(metrics))
uniq := map[string]Metric{}
for _, m := range metrics {
uniq[fmt.Sprintf("%s-%s", m.Key, labelsKey(m.Labels))] = m
}
results := make([]Metric, len(uniq))
i := 0
for _, m := range uniq {
results[i] = m
i++
}
sort.Sort(byKey(results))
return results
}
// SetSent marks the metric has having been sent at
// the specified time.
func (m *MetricBatch) SetSent(t time.Time) error {
deleteTime := t.UTC().Add(CleanupAge)
ops := setSentOps([]string{m.UUID()}, deleteTime)
if err := m.st.db().RunTransaction(ops); err != nil {
return errors.Annotatef(err, "cannot set metric sent for metric %q", m.UUID())
}
m.doc.Sent = true
m.doc.DeleteTime = deleteTime
return nil
}
// Credentials returns any credentials associated with the metric batch.
func (m *MetricBatch) Credentials() []byte {
return m.doc.Credentials
}
// SLACredentials returns any sla credentials associated with the metric batch.
func (m *MetricBatch) SLACredentials() []byte {
return m.doc.SLACredentials
}
func setSentOps(batchUUIDs []string, deleteTime time.Time) []txn.Op {
ops := make([]txn.Op, len(batchUUIDs))
for i, u := range batchUUIDs {
ops[i] = txn.Op{
C: metricsC,
Id: u,
Assert: txn.DocExists,
Update: bson.M{"$set": bson.M{"sent": true, "delete-time": deleteTime}},
}
}
return ops
}
// SetMetricBatchesSent sets sent on each MetricBatch corresponding to the uuids provided.
func (st *State) SetMetricBatchesSent(batchUUIDs []string) error {
deleteTime := st.clock().Now().UTC().Add(CleanupAge)
ops := setSentOps(batchUUIDs, deleteTime)
if err := st.db().RunTransaction(ops); err != nil {
return errors.Annotatef(err, "cannot set metric sent in bulk call")
}
return nil
}