Skip to content

Commit

Permalink
Make caser a local varialbe, not a global one.
Browse files Browse the repository at this point in the history
The cases.Caser returned by calling cases.Title *cannot* be shared among
goroutines. This might happen when Prometheus tries to scrape multiple
targets at the same time. From the docs:

A Caser may be stateful and should therefore not be shared between
goroutines.

Fixes: #922

Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
  • Loading branch information
mem authored and roidelapluie committed Jun 17, 2022
1 parent 148a9de commit 54ed14c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ import (
"github.com/prometheus/blackbox_exporter/config"
)

var caser = cases.Title(language.Und)

func matchRegularExpressions(reader io.Reader, httpConfig config.HTTPProbe, logger log.Logger) bool {
body, err := ioutil.ReadAll(reader)
if err != nil {
Expand Down Expand Up @@ -342,6 +340,16 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
return false
}

// Do not move the following variable to global scope. The cases.Caser returned by
// calling cases.Title *cannot* be shared among goroutines. This might happen when
// Prometheus tries to scrape multiple targets at the same time. From the docs:
//
// A Caser may be stateful and should therefore not be shared between goroutines.
//
// Issue: https://github.com/prometheus/blackbox_exporter/issues/922

caser := cases.Title(language.Und)

httpClientConfig := module.HTTP.HTTPClientConfig
if len(httpClientConfig.TLSConfig.ServerName) == 0 {
// If there is no `server_name` in tls_config, use
Expand Down
3 changes: 3 additions & 0 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
pconfig "github.com/prometheus/common/config"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/prometheus/blackbox_exporter/config"
)
Expand Down Expand Up @@ -1068,6 +1070,7 @@ func TestHTTPHeaders(t *testing.T) {
"Accept-Language": "en-US",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
caser := cases.Title(language.Und)
for key, value := range headers {
if caser.String(key) == "Host" {
if r.Host != value {
Expand Down

0 comments on commit 54ed14c

Please sign in to comment.