-
Notifications
You must be signed in to change notification settings - Fork 27
/
containers.go
89 lines (73 loc) · 1.88 KB
/
containers.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
package main
import (
"fmt"
"sort"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
const (
counterHelp = "Marathon counter %s"
gaugeHelp = "Marathon gauge %s"
meterHelp = "Marathon meter %s (%s)"
histogramHelp = "Marathon histogram %s"
timerHelp = "Marathon timer %s (%s)"
)
type CounterContainer struct {
counters map[string]*prometheus.CounterVec
namespace string
mutex sync.Mutex
}
func NewCounterContainer(namespace string) *CounterContainer {
return &CounterContainer{
counters: make(map[string]*prometheus.CounterVec),
namespace: namespace,
}
}
func (c *CounterContainer) Fetch(name, help string, labels ...string) (*prometheus.CounterVec, bool) {
key := containerKey(name, labels)
c.mutex.Lock()
defer c.mutex.Unlock()
counter, exists := c.counters[key]
if !exists {
counter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: c.namespace,
Name: name,
Help: help,
}, labels)
c.counters[key] = counter
}
return counter, !exists
}
type GaugeContainer struct {
gauges map[string]*prometheus.GaugeVec
namespace string
mutex sync.Mutex
}
func NewGaugeContainer(namespace string) *GaugeContainer {
return &GaugeContainer{
gauges: make(map[string]*prometheus.GaugeVec),
namespace: namespace,
}
}
func (c *GaugeContainer) Fetch(name, help string, labels ...string) (*prometheus.GaugeVec, bool) {
key := containerKey(name, labels)
c.mutex.Lock()
defer c.mutex.Unlock()
gauge, exists := c.gauges[key]
if !exists {
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: c.namespace,
Name: name,
Help: help,
}, labels)
c.gauges[key] = gauge
}
return gauge, !exists
}
func containerKey(metric string, labels []string) string {
s := make([]string, len(labels))
copy(s, labels)
sort.Strings(s)
return fmt.Sprintf("%s{%v}", metric, strings.Join(s, ","))
}