/
metric.go
70 lines (59 loc) · 1.27 KB
/
metric.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
package exporttools
import "github.com/prometheus/client_golang/prometheus"
type MetricGroup interface {
Collect() ([]*Metric, error)
}
type MetricStore interface {
MetricNames() []string
Set(m *Metric) error
Get(name string) (*Metric, error)
}
type Metric struct {
Name string
Description string
Type metricType
Value int64
LabelKeys []string
LabelVals []string
}
type metricType int
const (
Counter metricType = iota
Gauge
)
func (m *Metric) Update(newMetric *Metric) error {
switch newMetric.Type {
case Gauge:
if m.Type != Gauge {
return ErrIncompatibleMetricType
}
m.Value = newMetric.Value
case Counter:
if m.Type != Counter {
return ErrIncompatibleMetricType
}
m.Value = m.Value + newMetric.Value
default:
return ErrUnknownMetricType
}
return nil
}
func (m *Metric) PromDescription(exporterName string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName("", exporterName, m.Name),
m.Description,
m.LabelKeys, nil,
)
}
func (m *Metric) PromType() prometheus.ValueType {
if m.Type == Counter {
return prometheus.CounterValue
}
return prometheus.GaugeValue
}
func (m *Metric) PromValue() float64 {
return float64(m.Value)
}
func (m *Metric) PromLabels() []string {
return m.LabelVals
}