/
metrics.go
64 lines (52 loc) · 1.58 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuctesting
import (
"time"
"github.com/juju/errors"
"github.com/juju/juju/worker/uniter/runner/jujuc"
)
// Metrics holds the values for the hook sub-context.
type Metrics struct {
Metrics []jujuc.Metric
}
// AddMetric adds a Metric for the provided data.
func (m *Metrics) AddMetric(key, value string, created time.Time) {
m.Metrics = append(m.Metrics, jujuc.Metric{
Key: key,
Value: value,
Time: created,
})
}
// AddMetricLabels adds a Metric with labels for the provided data.
func (m *Metrics) AddMetricLabels(key, value string, created time.Time, labels map[string]string) {
m.Metrics = append(m.Metrics, jujuc.Metric{
Key: key,
Value: value,
Time: created,
Labels: labels,
})
}
// ContextMetrics is a test double for jujuc.ContextMetrics.
type ContextMetrics struct {
contextBase
info *Metrics
}
// AddMetric implements jujuc.ContextMetrics.
func (c *ContextMetrics) AddMetric(key, value string, created time.Time) error {
c.stub.AddCall("AddMetric", key, value, created)
if err := c.stub.NextErr(); err != nil {
return errors.Trace(err)
}
c.info.AddMetric(key, value, created)
return nil
}
// AddMetricLabels implements jujuc.ContextMetrics.
func (c *ContextMetrics) AddMetricLabels(key, value string, created time.Time, labels map[string]string) error {
c.stub.AddCall("AddMetricLabels", key, value, created, labels)
if err := c.stub.NextErr(); err != nil {
return errors.Trace(err)
}
c.info.AddMetricLabels(key, value, created, labels)
return nil
}