-
Notifications
You must be signed in to change notification settings - Fork 29
/
tally.go
276 lines (213 loc) · 6.66 KB
/
tally.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package tally
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/choria-io/go-choria/aagent/machine"
"github.com/choria-io/go-choria/choria"
"github.com/choria-io/go-choria/internal/util"
"github.com/choria-io/go-choria/lifecycle"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/prometheus/client_golang/prometheus"
)
// Connector is a connection to the middleware
type Connector interface {
QueueSubscribe(ctx context.Context, name string, subject string, group string, output chan *choria.ConnectorMessage) error
}
// Recorder listens for alive events and records the versions
// and expose the results to Prometheus
type Recorder struct {
sync.Mutex
options *options
observed map[string]*observation
// lifecycle
okEvents *prometheus.CounterVec
badEvents *prometheus.CounterVec
eventsTally *prometheus.GaugeVec
maintTime *prometheus.SummaryVec
processTime *prometheus.SummaryVec
eventTypes *prometheus.CounterVec
nodesExpired *prometheus.CounterVec
// transitions
transitionEvent *prometheus.CounterVec
}
type observation struct {
ts time.Time
version string
}
// New creates a new Recorder
func New(opts ...Option) (recorder *Recorder, err error) {
recorder = &Recorder{
options: &options{},
observed: make(map[string]*observation),
}
for _, opt := range opts {
opt(recorder.options)
}
err = recorder.options.Validate()
if err != nil {
return nil, fmt.Errorf("invalid options supplied: %s", err)
}
recorder.createStats()
return recorder, nil
}
func (r *Recorder) processAlive(e lifecycle.Event) error {
alive := e.(*lifecycle.AliveEvent)
r.Lock()
defer r.Unlock()
hname := alive.Identity()
obs, ok := r.observed[hname]
if !ok {
r.observed[hname] = &observation{
ts: time.Now(),
version: alive.Version,
}
r.eventsTally.WithLabelValues(alive.Component(), alive.Version).Inc()
return nil
}
r.observed[hname].ts = time.Now()
if obs.version != alive.Version {
r.eventsTally.WithLabelValues(alive.Component(), obs.version).Dec()
obs.version = alive.Version
r.eventsTally.WithLabelValues(alive.Component(), obs.version).Inc()
}
return nil
}
func (r *Recorder) processStartup(e lifecycle.Event) error {
startup := e.(*lifecycle.StartupEvent)
r.Lock()
defer r.Unlock()
hname := startup.Identity()
obs, ok := r.observed[hname]
if ok {
r.eventsTally.WithLabelValues(startup.Component(), obs.version).Dec()
}
r.observed[hname] = &observation{
ts: time.Now(),
version: startup.Version,
}
r.eventsTally.WithLabelValues(startup.Component(), startup.Version).Inc()
return nil
}
func (r *Recorder) processShutdown(e lifecycle.Event) error {
shutdown := e.(*lifecycle.ShutdownEvent)
r.Lock()
defer r.Unlock()
hname := shutdown.Identity()
obs, ok := r.observed[hname]
if ok {
r.eventsTally.WithLabelValues(shutdown.Component(), obs.version).Dec()
delete(r.observed, hname)
}
return nil
}
func (r *Recorder) process(e lifecycle.Event) (err error) {
r.options.Log.Debugf("Processing %s event from %s %s", e.TypeString(), e.Component(), e.Identity())
timer := r.processTime.WithLabelValues(r.options.Component)
obs := prometheus.NewTimer(timer)
defer obs.ObserveDuration()
r.eventTypes.WithLabelValues(e.Component(), e.TypeString()).Inc()
switch e.Type() {
case lifecycle.Alive:
err = r.processAlive(e)
case lifecycle.Startup:
err = r.processStartup(e)
case lifecycle.Shutdown:
err = r.processShutdown(e)
}
if err == nil {
r.okEvents.WithLabelValues(r.options.Component).Inc()
} else {
r.badEvents.WithLabelValues(r.options.Component).Inc()
}
return err
}
func (r *Recorder) maintenance() {
if r.options.Component == "" {
return
}
r.Lock()
defer r.Unlock()
timer := r.maintTime.WithLabelValues(r.options.Component)
obs := prometheus.NewTimer(timer)
defer obs.ObserveDuration()
oldest := time.Now().Add(-80 * time.Minute)
older := []string{}
for host, obs := range r.observed {
if obs.ts.Before(oldest) {
r.eventsTally.WithLabelValues(r.options.Component, obs.version).Dec()
older = append(older, host)
}
}
for _, host := range older {
r.options.Log.Debugf("Removing node %s, last seen %v", host, r.observed[host].ts)
delete(r.observed, host)
r.nodesExpired.WithLabelValues(r.options.Component).Inc()
}
if len(older) > 0 {
r.options.Log.Infof("Removed %d hosts that have not been seen in over an hour", len(older))
}
}
func (r *Recorder) processStateTransition(m *choria.ConnectorMessage) (err error) {
ce := cloudevents.NewEvent("1.0")
event := &machine.TransitionNotification{}
err = json.Unmarshal(m.Bytes(), &ce)
if err != nil {
return fmt.Errorf("could not parse cloudevent: %s", err)
}
err = ce.DataAs(event)
if err != nil {
return fmt.Errorf("could not parse transition event: %s", err)
}
if event.Protocol != "io.choria.machine.v1.transition" {
return fmt.Errorf("unknown notification protocol %s", event.Protocol)
}
r.transitionEvent.WithLabelValues(event.Machine, event.Version, event.Transition, event.FromState, event.ToState).Inc()
return nil
}
// Run starts listening for events and record statistics about it in prometheus
func (r *Recorder) Run(ctx context.Context) (err error) {
lifeEvents := make(chan *choria.ConnectorMessage, 100)
machineTransitions := make(chan *choria.ConnectorMessage, 100)
maintSched := time.NewTicker(time.Minute)
subid := util.UniqueID()
if r.options.Component == "" {
r.options.Log.Warn("Component was not specified, disabling lifecycle tallies")
} else {
err = r.options.Connector.QueueSubscribe(ctx, fmt.Sprintf("tally_%s_%s", r.options.Component, subid), fmt.Sprintf("choria.lifecycle.event.*.%s", r.options.Component), "", lifeEvents)
if err != nil {
return fmt.Errorf("could not subscribe to lifecycle events: %s", err)
}
}
err = r.options.Connector.QueueSubscribe(ctx, fmt.Sprintf("tally_transitions_%s", subid), "choria.machine.transition", "", machineTransitions)
if err != nil {
return fmt.Errorf("could not subscribe to machine transition events: %s", err)
}
for {
select {
case e := <-lifeEvents:
event, err := lifecycle.NewFromJSON(e.Data)
if err != nil {
r.options.Log.Errorf("could not process event: %s", err)
r.badEvents.WithLabelValues(r.options.Component).Inc()
continue
}
err = r.process(event)
if err != nil {
r.options.Log.Errorf("could not process event from %s: %s", event.Identity(), err)
}
case t := <-machineTransitions:
err = r.processStateTransition(t)
if err != nil {
r.options.Log.Errorf("could not process transition event: %s", err)
r.badEvents.WithLabelValues(r.options.Component).Inc()
}
case <-maintSched.C:
r.maintenance()
case <-ctx.Done():
return nil
}
}
}