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

@ -9,16 +9,9 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
var (
systemTime = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "time",
Help: "System time in seconds since epoch (1970).",
})
)
type timeCollector struct {
config Config
metric prometheus.Counter
}
func init() {
@ -28,17 +21,20 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing
// the current system time in seconds since epoch.
func NewTimeCollector(config Config) (Collector, error) {
c := timeCollector{
return &timeCollector{
config: config,
}
return &c, nil
metric: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "time",
Help: "System time in seconds since epoch (1970).",
}),
}, nil
}
func (c *timeCollector) Update(ch chan<- prometheus.Metric) (err error) {
now := time.Now()
glog.V(1).Infof("Set time: %f", now.Unix())
systemTime.Set(float64(now.Unix()))
systemTime.Collect(ch)
c.metric.Set(float64(now.Unix()))
c.metric.Collect(ch)
return err
}