-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Stateless ruler restores alert state #5230
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package rules | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/storage" | ||
|
||
"github.com/thanos-io/thanos/internal/cortex/querier/series" | ||
"github.com/thanos-io/thanos/pkg/httpconfig" | ||
"github.com/thanos-io/thanos/pkg/promclient" | ||
"github.com/thanos-io/thanos/pkg/store/storepb" | ||
) | ||
|
||
type promClientsQueryable struct { | ||
httpMethod string | ||
step time.Duration | ||
|
||
logger log.Logger | ||
promClients []*promclient.Client | ||
queryClients []*httpconfig.Client | ||
ignoredLabelNames []string | ||
|
||
duplicatedQuery prometheus.Counter | ||
} | ||
type promClientsQuerier struct { | ||
ctx context.Context | ||
mint, maxt int64 | ||
step int64 | ||
httpMethod string | ||
|
||
logger log.Logger | ||
promClients []*promclient.Client | ||
queryClients []*httpconfig.Client | ||
restoreIgnoreLabels []string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we could make this a bit more generic and rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
|
||
// We use a dummy counter here because the duplicated | ||
// addresses are already tracked by rule evaluation part. | ||
duplicatedQuery prometheus.Counter | ||
} | ||
|
||
// NewPromClientsQueryable creates a queryable that queries queriers from Prometheus clients. | ||
func NewPromClientsQueryable(logger log.Logger, queryClients []*httpconfig.Client, promClients []*promclient.Client, | ||
httpMethod string, step time.Duration, ignoredLabelNames []string) *promClientsQueryable { | ||
return &promClientsQueryable{ | ||
logger: logger, | ||
queryClients: queryClients, | ||
promClients: promClients, | ||
duplicatedQuery: promauto.With(nil).NewCounter(prometheus.CounterOpts{}), | ||
httpMethod: httpMethod, | ||
step: step, | ||
ignoredLabelNames: ignoredLabelNames, | ||
} | ||
} | ||
|
||
// Querier returns a new Querier for the given time range. | ||
func (q *promClientsQueryable) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) { | ||
return &promClientsQuerier{ | ||
ctx: ctx, | ||
mint: mint, | ||
maxt: maxt, | ||
step: int64(q.step / time.Second), | ||
httpMethod: q.httpMethod, | ||
logger: q.logger, | ||
queryClients: q.queryClients, | ||
promClients: q.promClients, | ||
restoreIgnoreLabels: q.ignoredLabelNames, | ||
}, nil | ||
} | ||
|
||
// Select implements storage.Querier interface. | ||
func (q *promClientsQuerier) Select(_ bool, _ *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet { | ||
query := storepb.PromMatchersToString(matchers...) | ||
|
||
for _, i := range rand.Perm(len(q.queryClients)) { | ||
promClient := q.promClients[i] | ||
endpoints := RemoveDuplicateQueryEndpoints(q.logger, q.duplicatedQuery, q.queryClients[i].Endpoints()) | ||
for _, i := range rand.Perm(len(endpoints)) { | ||
m, warns, err := promClient.QueryRange(q.ctx, endpoints[i], query, q.mint, q.maxt, q.step, promclient.QueryOptions{ | ||
Deduplicate: true, | ||
Method: q.httpMethod, | ||
}) | ||
|
||
if err != nil { | ||
level.Error(q.logger).Log("err", err, "query", q) | ||
continue | ||
} | ||
if len(warns) > 0 { | ||
level.Warn(q.logger).Log("warnings", strings.Join(warns, ", "), "query", q) | ||
} | ||
matrix := make([]*model.SampleStream, 0, m.Len()) | ||
for _, metric := range m { | ||
for _, label := range q.restoreIgnoreLabels { | ||
delete(metric.Metric, model.LabelName(label)) | ||
} | ||
|
||
matrix = append(matrix, &model.SampleStream{ | ||
Metric: metric.Metric, | ||
Values: metric.Values, | ||
}) | ||
} | ||
|
||
return series.MatrixToSeriesSet(matrix) | ||
} | ||
} | ||
return storage.NoopSeriesSet() | ||
} | ||
|
||
// LabelValues implements storage.LabelQuerier interface. | ||
func (q *promClientsQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, storage.Warnings, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
// LabelNames implements storage.LabelQuerier interface. | ||
func (q *promClientsQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
// Close implements storage.LabelQuerier interface. | ||
func (q *promClientsQuerier) Close() error { | ||
return nil | ||
} | ||
|
||
// RemoveDuplicateQueryEndpoints removes duplicate endpoints from the list of urls. | ||
func RemoveDuplicateQueryEndpoints(logger log.Logger, duplicatedQueriers prometheus.Counter, urls []*url.URL) []*url.URL { | ||
set := make(map[string]struct{}) | ||
deduplicated := make([]*url.URL, 0, len(urls)) | ||
for _, u := range urls { | ||
if _, ok := set[u.String()]; ok { | ||
level.Warn(logger).Log("msg", "duplicate query address is provided", "addr", u.String()) | ||
duplicatedQueriers.Inc() | ||
continue | ||
} | ||
deduplicated = append(deduplicated, u) | ||
set[u.String()] = struct{}{} | ||
} | ||
return deduplicated | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this so small by default? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure whether we want to expose this to users or not. The step can be calculated simply by
max((maxt - mint) / 250, 1s)
. But in E2E test we might still want to modify it.For the default value
1s
, I just use the same default query step set inquery.go
. Do you have any suggested value?