textfile: fix duplicate metrics error (#738)

The textfile gatherer should only be added to gatherer list once.

Signed-off-by: Li Wei <liwei@anbutu.com>
This commit is contained in:
Wei Li 2017-12-07 00:05:40 +08:00 committed by Ben Kochie
parent a96f1738b3
commit 1e9bb4ec3a
1 changed files with 8 additions and 4 deletions

View File

@ -22,6 +22,7 @@ import (
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
@ -34,6 +35,7 @@ import (
var ( var (
textFileDirectory = kingpin.Flag("collector.textfile.directory", "Directory to read text files with metrics from.").Default("").String() textFileDirectory = kingpin.Flag("collector.textfile.directory", "Directory to read text files with metrics from.").Default("").String()
textFileAddOnce sync.Once
) )
type textFileCollector struct { type textFileCollector struct {
@ -56,10 +58,12 @@ func NewTextFileCollector() (Collector, error) {
// the flag is not passed. // the flag is not passed.
log.Infof("No directory specified, see --collector.textfile.directory") log.Infof("No directory specified, see --collector.textfile.directory")
} else { } else {
textFileAddOnce.Do(func() {
prometheus.DefaultGatherer = prometheus.Gatherers{ prometheus.DefaultGatherer = prometheus.Gatherers{
prometheus.DefaultGatherer, prometheus.DefaultGatherer,
prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) { return c.parseTextFiles(), nil }), prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) { return c.parseTextFiles(), nil }),
} }
})
} }
return c, nil return c, nil