This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_impl.go
166 lines (144 loc) · 3.92 KB
/
collector_impl.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
package metrics
import (
"bytes"
"sync"
"github.com/containerssh/geoip/geoipprovider"
)
type collector struct {
geoIpLookupProvider geoipprovider.LookupProvider
mutex *sync.Mutex
metricsMap map[string]Metric
metrics []Metric
values map[string]*metricValue
}
func (c *collector) MustCreateCounter(name string, unit string, help string) Counter {
counter, err := c.CreateCounter(name, unit, help)
if err != nil {
panic(err)
}
return counter
}
func (c *collector) MustCreateCounterGeo(name string, unit string, help string) GeoCounter {
counter, err := c.CreateCounterGeo(name, unit, help)
if err != nil {
panic(err)
}
return counter
}
func (c *collector) MustCreateGauge(name string, unit string, help string) Gauge {
gauge, err := c.CreateGauge(name, unit, help)
if err != nil {
panic(err)
}
return gauge
}
func (c *collector) MustCreateGaugeGeo(name string, unit string, help string) GeoGauge {
gauge, err := c.CreateGaugeGeo(name, unit, help)
if err != nil {
panic(err)
}
return gauge
}
type metricValue struct {
metricValueMap map[string]*MetricValue
metricValues []*MetricValue
}
func (c *collector) createMetric(name string, unit string, help string, metricType MetricType) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if _, ok := c.metricsMap[name]; ok {
return MetricAlreadyExists
}
metric := Metric{
Name: name,
Unit: unit,
Help: help,
Type: metricType,
}
c.metricsMap[name] = metric
c.metrics = append(c.metrics, metric)
return nil
}
func (c *collector) CreateCounter(name string, unit string, help string) (Counter, error) {
if err := c.createMetric(name, unit, help, MetricTypeCounter); err != nil {
return nil, err
}
return &counterImpl{
name: name,
collector: c,
}, nil
}
func (c *collector) CreateCounterGeo(name string, unit string, help string) (GeoCounter, error) {
if err := c.createMetric(name, unit, help, MetricTypeCounter); err != nil {
return nil, err
}
return &counterGeoImpl{
name: name,
collector: c,
}, nil
}
func (c *collector) CreateGauge(name string, unit string, help string) (Gauge, error) {
if err := c.createMetric(name, unit, help, MetricTypeGauge); err != nil {
return nil, err
}
return &gaugeImpl{
name: name,
collector: c,
}, nil
}
func (c *collector) CreateGaugeGeo(name string, unit string, help string) (GeoGauge, error) {
if err := c.createMetric(name, unit, help, MetricTypeGauge); err != nil {
return nil, err
}
return &gaugeGeoImpl{
name: name,
collector: c,
}, nil
}
func (c *collector) ListMetrics() []Metric {
return c.metrics
}
func (c *collector) GetMetric(name string) []MetricValue {
var results []MetricValue
if val, ok := c.values[name]; ok {
for _, v := range val.metricValues {
results = append(results, *v)
}
}
return results
}
func (c *collector) String() string {
var buffer bytes.Buffer
for _, metric := range c.metrics {
buffer.WriteString(metric.String())
for _, value := range c.GetMetric(metric.Name) {
buffer.WriteString(value.String())
}
}
buffer.WriteString("# EOF\n")
return buffer.String()
}
func (c *collector) getValueStruct(name string, labels map[string]string) *MetricValue {
if _, ok := c.values[name]; !ok {
c.values[name] = &metricValue{
metricValueMap: map[string]*MetricValue{},
metricValues: []*MetricValue{},
}
}
valueStruct := MetricValue{
Name: name,
Labels: labels,
Value: 0,
}
if _, ok2 := c.values[name].metricValueMap[valueStruct.CombinedName()]; !ok2 {
c.values[name].metricValueMap[valueStruct.CombinedName()] = &valueStruct
c.values[name].metricValues = append(c.values[name].metricValues, &valueStruct)
}
return c.values[name].metricValueMap[valueStruct.CombinedName()]
}
func (c *collector) get(name string, labels map[string]string) float64 {
return c.getValueStruct(name, labels).Value
}
func (c *collector) set(name string, labels map[string]string, value float64) {
c.getValueStruct(name, labels).Value = value
}