Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make caser a local varialbe, not a global one. #927

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary to point to the gh issue here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt it was necessary to keep the reference close to the code because the original version is what I would have written, too. The wording in the docs, "a Caser may", sounds like if that were the actual case, the individual Caser implementations would spell that out. Given that in this particular instance there no such note, I would have assumed that this Caser is not stateful.

Thanks for the review and merging this!


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