Skip to content

Commit

Permalink
textfile: fix duplicate metrics error (prometheus#738)
Browse files Browse the repository at this point in the history
The textfile gatherer should only be added to gatherer list once.

Signed-off-by: Li Wei <[email protected]>
  • Loading branch information
liwei authored and oblitorum committed Apr 9, 2024
1 parent e1a7e93 commit 091813d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions collector/textfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"time"

"github.com/golang/protobuf/proto"
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 091813d

Please sign in to comment.