/
metrics.go
55 lines (46 loc) · 1.54 KB
/
metrics.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
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package uniter
import (
"time"
corecharm "gopkg.in/juju/charm.v5"
)
const (
// interval at which the unit's metrics should be collected
metricsPollInterval = 5 * time.Minute
)
// activeMetricsTimer returns a channel that will signal the collect metrics hook
// as close to interval after the last run as possible.
var activeMetricsTimer = func(now, lastRun time.Time, interval time.Duration) <-chan time.Time {
waitDuration := interval - now.Sub(lastRun)
logger.Debugf("metrics waiting for %v", waitDuration)
return time.After(waitDuration)
}
// inactiveMetricsTimer is the default metrics signal generation function, that
// returns no signal. It will be used in charms that do not declare metrics.
func inactiveMetricsTimer(_, _ time.Time, _ time.Duration) <-chan time.Time {
return nil
}
// timerChooser allows modeAbide to choose a proper timer for metrics
// depending on the charm.
type timerChooser struct {
active TimedSignal
inactive TimedSignal
}
// getMetricsTimer returns the metrics timer we should be using, given the supplied
// charm.
func (t *timerChooser) getMetricsTimer(ch corecharm.Charm) TimedSignal {
metrics := ch.Metrics()
if metrics != nil && len(metrics.Metrics) > 0 {
return t.active
}
return t.inactive
}
// NewMetricsTimerChooser returns a timerChooser for
// collect-metrics.
func NewMetricsTimerChooser() *timerChooser {
return &timerChooser{
active: activeMetricsTimer,
inactive: inactiveMetricsTimer,
}
}