Reduce number of global variables used

This is the first step to make the exporter more testable.
This commit is contained in:
Tobias Schmidt 2014-11-24 21:00:17 -05:00
commit 872f921867
15 changed files with 473 additions and 482 deletions

View file

@ -16,17 +16,9 @@ import (
const lastLoginSubsystem = "last_login"
var (
lastSeen = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: lastLoginSubsystem,
Name: "time",
Help: "The time of the last login.",
})
)
type lastLoginCollector struct {
config Config
metric prometheus.Gauge
}
func init() {
@ -36,10 +28,15 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing
// load, seconds since last login and a list of tags as specified by config.
func NewLastLoginCollector(config Config) (Collector, error) {
c := lastLoginCollector{
return &lastLoginCollector{
config: config,
}
return &c, nil
metric: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: lastLoginSubsystem,
Name: "time",
Help: "The time of the last login.",
}),
}, nil
}
func (c *lastLoginCollector) Update(ch chan<- prometheus.Metric) (err error) {
@ -48,8 +45,8 @@ func (c *lastLoginCollector) Update(ch chan<- prometheus.Metric) (err error) {
return fmt.Errorf("Couldn't get last seen: %s", err)
}
glog.V(1).Infof("Set node_last_login_time: %f", last)
lastSeen.Set(last)
lastSeen.Collect(ch)
c.metric.Set(last)
c.metric.Collect(ch)
return err
}