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

rule: fix query addr parsing #2288

Merged
merged 4 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel

### Fixed

- [#2288](https://github.com/thanos-io/thanos/pull/2288) Ruler: Fixes issue #2281 bug in ruler with parsing query addr with path prefix
- [#2238](https://github.com/thanos-io/thanos/pull/2238) Ruler: Fixed Issue #2204 bug in alert queue signalling filled up queue and alerts were dropped
- [#2231](https://github.com/thanos-io/thanos/pull/2231) Bucket Web - Sort chunks by thanos.downsample.resolution for better grouping
- [#2254](https://github.com/thanos-io/thanos/pull/2254) Bucket: Fix metrics registered multiple times in bucket replicate
Expand Down
25 changes: 12 additions & 13 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ func runRule(
return err
}
} else {
for _, addr := range queryAddrs {
if addr == "" {
return errors.New("static querier address cannot be empty")
}
var err error
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
queryCfg, err = query.BuildQueryConfig(queryAddrs)
if err != nil {
return err
}

// Build the query configuration from the legacy query flags.
Expand All @@ -308,16 +308,15 @@ func runRule(
Files: querySDFiles,
RefreshInterval: model.Duration(querySDInterval),
})
}
queryCfg = append(queryCfg,
query.Config{
EndpointsConfig: http_util.EndpointsConfig{
Scheme: "http",
StaticAddresses: queryAddrs,
FileSDConfigs: fileSDConfigs,
queryCfg = append(queryCfg,
query.Config{
EndpointsConfig: http_util.EndpointsConfig{
Scheme: "http",
FileSDConfigs: fileSDConfigs,
},
},
},
)
)
}
}

queryProvider := dns.NewProvider(
Expand Down
26 changes: 26 additions & 0 deletions pkg/query/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
package query

import (
"fmt"
"net/url"

"gopkg.in/yaml.v2"

"github.com/pkg/errors"
http_util "github.com/thanos-io/thanos/pkg/http"
)

Expand Down Expand Up @@ -39,3 +43,25 @@ func LoadConfigs(confYAML []byte) ([]Config, error) {
}
return queryCfg, nil
}

// BuildQueryConfig initializes and returns an Query client configuration from a static address.
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
func BuildQueryConfig(queryAddrs []string) ([]Config, error) {
configs := make([]Config, 0, len(queryAddrs))
for _, addr := range queryAddrs {
if addr == "" {
return nil, errors.New("static querier address cannot be empty")
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
}
u, err := url.Parse(fmt.Sprintf("http://%s", addr))
Copy link
Member

Choose a reason for hiding this comment

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

hmmm what if someone will want HTTPS?

Copy link
Member

Choose a reason for hiding this comment

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

or essentially what if someone will pass with http://query.com?

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 have implemented it in a way how it worked in v0.10.
But maybe you are right, that it should also support passing scheme. I will try to think about something and will get back to you when it's ready.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I am not saying what was there was the best (: That's why we always look for improvement (:

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 went with checking if :// is present. If yes, schema is parse. If not, http is added as schema.
I don't have an idea how to do it more clever, apparently it is not that easy looking at google results.

if err != nil {
return nil, errors.Wrapf(err, "failed to parse addr %q", addr)
}
configs = append(configs, Config{
EndpointsConfig: http_util.EndpointsConfig{
Scheme: u.Scheme,
StaticAddresses: []string{u.Host},
PathPrefix: u.Path,
},
})
}
return configs, nil
}
65 changes: 65 additions & 0 deletions pkg/query/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package query

import (
"testing"

"github.com/thanos-io/thanos/pkg/http"
"github.com/thanos-io/thanos/pkg/testutil"
)

func TestBuildQueryConfig(t *testing.T) {
for _, tc := range []struct {
desc string
addresses []string
err bool
expected []Config
}{
{
desc: "signe addr without path",
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
addresses: []string{"localhost:9093"},
expected: []Config{{
EndpointsConfig: http.EndpointsConfig{
StaticAddresses: []string{"localhost:9093"},
Scheme: "http",
},
}},
},
{
desc: "1st addr without path, 2nd with",
addresses: []string{"localhost:9093", "localhost:9094/prefix"},
expected: []Config{
{
EndpointsConfig: http.EndpointsConfig{
StaticAddresses: []string{"localhost:9093"},
Scheme: "http",
},
},
{
EndpointsConfig: http.EndpointsConfig{
StaticAddresses: []string{"localhost:9094"},
Scheme: "http",
PathPrefix: "/prefix",
},
},
},
},
{
desc: "invalid addr",
addresses: []string{"this is not a valid addr"},
err: true,
},
} {
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
t.Run(tc.desc, func(t *testing.T) {
cfg, err := BuildQueryConfig(tc.addresses)
if tc.err {
testutil.NotOk(t, err)
return
}

testutil.Equals(t, tc.expected, cfg)
})
}
}