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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
26 changes: 12 additions & 14 deletions cmd/thanos/rule.go
Expand Up @@ -288,17 +288,16 @@ func runRule(
metrics := newRuleMetrics(reg)

var queryCfg []query.Config
var err error
if len(queryConfigYAML) > 0 {
var err error
queryCfg, err = query.LoadConfigs(queryConfigYAML)
if err != nil {
return err
}
} else {
for _, addr := range queryAddrs {
if addr == "" {
return errors.New("static querier address cannot be empty")
}
queryCfg, err = query.BuildQueryConfig(queryAddrs)
if err != nil {
return err
}

// Build the query configuration from the legacy query flags.
Expand All @@ -308,16 +307,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
31 changes: 31 additions & 0 deletions pkg/query/config.go
Expand Up @@ -4,8 +4,13 @@
package query

import (
"fmt"
"net/url"
"strings"

"gopkg.in/yaml.v2"

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

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

// BuildQueryConfig returns a query client configuration from a static address.
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
}
// If addr is missing schema, add http.
if !strings.Contains(addr, "://") {
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
addr = fmt.Sprintf("http://%s", addr)
}
u, err := url.Parse(addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

we could also verify that the scheme is http or https? It would catch typos like ttp://example.com which is a valid URL.

@bwplotka WDYT?

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 added support for it.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks 👍

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
}
85 changes: 85 additions & 0 deletions pkg/query/config_test.go
@@ -0,0 +1,85 @@
// 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: "signe addr with path and http scheme",
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
addresses: []string{"http://localhost:9093"},
expected: []Config{{
EndpointsConfig: http.EndpointsConfig{
StaticAddresses: []string{"localhost:9093"},
Scheme: "http",
},
}},
},
{
desc: "signe addr with path and https scheme",
tobiaszheller marked this conversation as resolved.
Show resolved Hide resolved
addresses: []string{"https://localhost:9093"},
expected: []Config{{
EndpointsConfig: http.EndpointsConfig{
StaticAddresses: []string{"localhost:9093"},
Scheme: "https",
},
}},
},
{
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)
})
}
}