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

@ -7,12 +7,9 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
var (
attributes *prometheus.GaugeVec
)
type attributesCollector struct {
config Config
metric *prometheus.GaugeVec
}
func init() {
@ -22,28 +19,28 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing
// labels from the config.
func NewAttributesCollector(config Config) (Collector, error) {
c := attributesCollector{
config: config,
}
labelNames := []string{}
for l := range c.config.Attributes {
for l := range config.Attributes {
labelNames = append(labelNames, l)
}
attributes = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Name: "attributes",
Help: "The node_exporter attributes.",
},
labelNames,
)
return &c, nil
return &attributesCollector{
config: config,
metric: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Name: "attributes",
Help: "The node_exporter attributes.",
},
labelNames,
),
}, nil
}
func (c *attributesCollector) Update(ch chan<- prometheus.Metric) (err error) {
glog.V(1).Info("Set node_attributes{%v}: 1", c.config.Attributes)
attributes.Reset()
attributes.With(c.config.Attributes).Set(1)
attributes.Collect(ch)
c.metric.Reset()
c.metric.With(c.config.Attributes).Set(1)
c.metric.Collect(ch)
return err
}