From d94e7b4838af524bffe06330b35c72968bf54af6 Mon Sep 17 00:00:00 2001 From: Philipp Defner Date: Mon, 17 Feb 2025 15:34:39 +0100 Subject: [PATCH] Check if query names are unique across jobs --- exporter.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/exporter.go b/exporter.go index 46a24307..b7ccd851 100644 --- a/exporter.go +++ b/exporter.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "strings" "cloud.google.com/go/cloudsqlconn" "cloud.google.com/go/cloudsqlconn/mysql/mysql" @@ -36,6 +37,22 @@ func NewExporter(logger log.Logger, configFile string) (*Exporter, error) { return nil, err } + // Validate config file before, this will otherwise break Prometheus metrics ("was collected before with the same name and label values") + uniqueQueries := make(map[string]struct{}) + var duplicateQueries []string + for _, job := range cfg.Jobs { + for _, query := range job.Queries { + if _, ok := uniqueQueries[query.Name]; ok { + duplicateQueries = append(duplicateQueries, query.Name) + continue + } + uniqueQueries[query.Name] = struct{}{} + } + } + if len(duplicateQueries) > 0 { + return nil, fmt.Errorf("invalid config file, query name is not unique across jobs: %v", strings.Join(duplicateQueries, ", ")) + } + var queryDurationHistogramBuckets []float64 if len(cfg.Configuration.HistogramBuckets) == 0 { queryDurationHistogramBuckets = DefaultQueryDurationHistogramBuckets