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