From 091813d0f1289f50673d4df24cf7272b549d4679 Mon Sep 17 00:00:00 2001 From: Wei Li Date: Thu, 7 Dec 2017 00:05:40 +0800 Subject: [PATCH] textfile: fix duplicate metrics error (#738) The textfile gatherer should only be added to gatherer list once. Signed-off-by: Li Wei --- collector/textfile.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/collector/textfile.go b/collector/textfile.go index b9f99c8eb8..6db5a0c3a9 100644 --- a/collector/textfile.go +++ b/collector/textfile.go @@ -23,6 +23,7 @@ import ( "path/filepath" "sort" "strings" + "sync" "time" "github.com/golang/protobuf/proto" @@ -35,6 +36,7 @@ import ( var ( textFileDirectory = kingpin.Flag("collector.textfile.directory", "Directory to read text files with metrics from.").Default("").String() + textFileAddOnce sync.Once ) type textFileCollector struct { @@ -57,10 +59,12 @@ func NewTextFileCollector() (Collector, error) { // the flag is not passed. log.Infof("No directory specified, see --collector.textfile.directory") } else { - prometheus.DefaultGatherer = prometheus.Gatherers{ - prometheus.DefaultGatherer, - prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) { return c.parseTextFiles(), nil }), - } + textFileAddOnce.Do(func() { + prometheus.DefaultGatherer = prometheus.Gatherers{ + prometheus.DefaultGatherer, + prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) { return c.parseTextFiles(), nil }), + } + }) } return c, nil