Add collect[] parameter (#699)

* Add `collect[]` parameter

* Add TODo comment about staticcheck ignored

* Restore promhttp.HandlerOpts

* Log a warning and return HTTP error instead of failing

* Check collector existence and status, cleanups

* Fix warnings and error messages

* Don't panic, return error if collector registration failed

* Update README
This commit is contained in:
Siavash Safi 2017-10-14 14:23:42 +02:00 committed by Ben Kochie
commit f3a7022602
4 changed files with 91 additions and 14 deletions

View file

@ -80,7 +80,18 @@ type nodeCollector struct {
}
// NewNodeCollector creates a new NodeCollector
func NewNodeCollector() (*nodeCollector, error) {
func NewNodeCollector(filters ...string) (*nodeCollector, error) {
f := make(map[string]bool)
for _, filter := range filters {
enabled, exist := collectorState[filter]
if !exist {
return nil, fmt.Errorf("missing collector: %s", filter)
}
if !*enabled {
return nil, fmt.Errorf("disabled collector: %s", filter)
}
f[filter] = true
}
collectors := make(map[string]Collector)
for key, enabled := range collectorState {
if *enabled {
@ -88,7 +99,9 @@ func NewNodeCollector() (*nodeCollector, error) {
if err != nil {
return nil, err
}
collectors[key] = collector
if len(f) == 0 || f[key] {
collectors[key] = collector
}
}
}
return &nodeCollector{Collectors: collectors}, nil