-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stateless ruler restores alert state
Signed-off-by: Ben Ye <[email protected]>
- Loading branch information
Ben Ye
committed
Mar 13, 2022
1 parent
6eb5ce6
commit 3e2375c
Showing
4 changed files
with
171 additions
and
36 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package rules | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/querier/series" | ||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/storage" | ||
|
||
"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 | ||
|
||
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 | ||
|
||
// 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) *promClientsQueryable { | ||
return &promClientsQueryable{ | ||
logger: logger, | ||
queryClients: queryClients, | ||
promClients: promClients, | ||
duplicatedQuery: prometheus.NewCounter(prometheus.CounterOpts{}), | ||
httpMethod: httpMethod, | ||
step: step, | ||
} | ||
} | ||
|
||
// 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.Millisecond), | ||
httpMethod: q.httpMethod, | ||
logger: q.logger, | ||
queryClients: q.queryClients, | ||
promClients: q.promClients, | ||
}, 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) | ||
} | ||
|
||
return series.MatrixToSeriesSet(m) | ||
} | ||
} | ||
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 | ||
} |