Skip to content

Commit

Permalink
fix duplicated collector when collectParams was passed in probe mode (#…
Browse files Browse the repository at this point in the history
…710)

* fix duplicated collector when collectParams was passed in probe mode

---------

Signed-off-by: qizhicheng <[email protected]>
  • Loading branch information
LeoQuote authored Mar 4, 2023
1 parent b3483ee commit aa12ba2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mysqld_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var scrapers = map[collector.Scraper]bool{
}

func filterScrapers(scrapers []collector.Scraper, collectParams []string) []collector.Scraper {
filteredScrapers := scrapers
var filteredScrapers []collector.Scraper

// Check if we have some "collect[]" query parameters.
if len(collectParams) > 0 {
Expand All @@ -121,6 +121,9 @@ func filterScrapers(scrapers []collector.Scraper, collectParams []string) []coll
}
}
}
if len(filteredScrapers) == 0 {
return scrapers
}
return filteredScrapers
}

Expand Down
47 changes: 47 additions & 0 deletions mysqld_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main
import (
"context"
"fmt"
"github.com/prometheus/mysqld_exporter/collector"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -218,3 +219,49 @@ func getBody(urlToGet string) ([]byte, error) {

return body, nil
}

func Test_filterScrapers(t *testing.T) {
type args struct {
scrapers []collector.Scraper
collectParams []string
}
tests := []struct {
name string
args args
want []collector.Scraper
}{
{"args_appears_in_collector",
args{
[]collector.Scraper{collector.ScrapeGlobalStatus{}},
[]string{collector.ScrapeGlobalStatus{}.Name()},
},
[]collector.Scraper{
collector.ScrapeGlobalStatus{},
}},
{"args_absent_in_collector",
args{
[]collector.Scraper{collector.ScrapeGlobalStatus{}},
[]string{collector.ScrapeGlobalVariables{}.Name()},
},
[]collector.Scraper{collector.ScrapeGlobalStatus{}}},
{"respect_params",
args{
[]collector.Scraper{
collector.ScrapeGlobalStatus{},
collector.ScrapeGlobalVariables{},
},
[]string{collector.ScrapeGlobalStatus{}.Name()},
},
[]collector.Scraper{
collector.ScrapeGlobalStatus{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := filterScrapers(tt.args.scrapers, tt.args.collectParams); !reflect.DeepEqual(got, tt.want) {
t.Errorf("filterScrapers() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit aa12ba2

Please sign in to comment.