-
Notifications
You must be signed in to change notification settings - Fork 1
/
new.go
115 lines (106 loc) · 2.46 KB
/
new.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
/* 2019-01-07 (cc) <paul4hough@gmail.com>
create remed instance
*/
package remed
import (
"fmt"
"sync"
promp "github.com/prometheus/client_golang/prometheus"
proma "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/pahoughton/agate/config"
"github.com/pahoughton/agate/notify"
)
type Metrics struct {
ansible *promp.CounterVec
scripts *promp.CounterVec
remedq promp.Gauge
remedm promp.Gauge
errors promp.Counter
}
type Remed struct {
debug bool
playbookDir string
scriptsDir string
cnt int32
parallel int32
wg sync.WaitGroup
metrics Metrics
notify *notify.Notify
}
func New(c config.Remed, n *notify.Notify, dbg bool) *Remed {
r := &Remed{
debug: dbg,
playbookDir: c.PlaybookDir,
scriptsDir: c.ScriptsDir,
parallel: int32(c.Parallel),
notify: n,
metrics: Metrics{
ansible: proma.NewCounterVec(
promp.CounterOpts{
Namespace: "agate",
Subsystem: "remed",
Name: "ansible",
Help: "number of ansible playbook runs",
}, []string{
"role",
"status",
}),
scripts: proma.NewCounterVec(
promp.CounterOpts{
Namespace: "agate",
Subsystem: "remed",
Name: "script",
Help: "number of script runs",
}, []string{
"script",
"status",
}),
errors: proma.NewCounter(
promp.CounterOpts{
Namespace: "agate",
Subsystem: "remed",
Name: "errors",
Help: "number of errors",
}),
remedq: proma.NewGauge(
promp.GaugeOpts{
Namespace: "agate",
Subsystem: "remed",
Name: "queue",
Help: "remed queue size",
}),
remedm: proma.NewGauge(
promp.GaugeOpts{
Namespace: "agate",
Subsystem: "remed",
Name: "queue_max",
Help: "max remed queue size",
}),
},
}
r.metrics.remedm.Set(float64(r.parallel))
return r
}
func (r *Remed) Del() {
r.unregister()
}
func (r *Remed) unregister() {
if r != nil && r.metrics.ansible != nil {
promp.Unregister(r.metrics.ansible);
r.metrics.ansible = nil
promp.Unregister(r.metrics.scripts);
promp.Unregister(r.metrics.errors);
promp.Unregister(r.metrics.remedm);
promp.Unregister(r.metrics.remedq);
}
}
func (r *Remed) error(err error) {
r.metrics.errors.Inc()
fmt.Println("ERROR: ",err.Error())
if r.debug { panic(err); }
}
func (r *Remed) errorf(msg string,args ...interface{}) error {
r.metrics.errors.Inc()
fmt.Println("ERROR: ",fmt.Sprintf(msg,args...))
return fmt.Errorf(msg,args...)
}