-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Feature/ruler (take 2) #2458
Merged
Merged
Feature/ruler (take 2) #2458
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
ed55330
begins speccing out ruler
owen-d 1f261a7
upstream conflicts
owen-d 1157df5
implicit ast impls, parser for ruler
owen-d 6beb78d
/api/prom ruler routes, ruler enabled in single binary
owen-d 9f785db
registers ruler flags, doesnt double instantiate metrics
owen-d 728436b
cleanup for old samples in ruler
owen-d 22f764b
begins ruler tests
owen-d 4e2f97b
ForStateAppenderQuerier tests
owen-d 26fe293
memhistory stop
owen-d f753eb4
RestoreForState test
owen-d 70cd160
upstream querier ifc
owen-d 68eab85
introducing loki ruler metrics
owen-d 9c83bae
removes rule granularity metric -- to be discussed in pr
owen-d 5e90c13
validates ruler cfg
owen-d e575b15
renames gauge metrics to not use total
owen-d 4f30817
removes unnecessary logs
owen-d 30e9b52
logs synthetic restoreforstate
owen-d 272043d
logs tenant in ruler
owen-d 9d299e2
sets cortex to owen's unmerged fork
owen-d 026abb9
begins porting rules pkg
owen-d 2d9ce8b
memstore work
owen-d e825231
work on queryable based in memory series store
owen-d 33570eb
removes unused pkgs, adds memstore test
owen-d 82f2e79
MemStore must be started after construction
owen-d 5f7e57e
MemstoreTenantManager
owen-d a254a9a
ruler loading
owen-d 367e18a
ruler instantiation
owen-d 7c9eca4
better metrics & logging in ruler
owen-d 25ef5aa
grpc cortex compatibility in go.mod
owen-d 94b80ca
cortex vendoring compat
owen-d 72dbbd8
increments memory cache hits only if cached
owen-d 04e6950
loki in memory metrics use prometheus default registerer
owen-d 262e394
ruler only depends on ring
owen-d cabf036
managerfactory rename
owen-d f384d8e
revendors cortex
owen-d 5f0ec00
Merge remote-tracking branch 'upstream/master' into feature/ruler-v3
owen-d d872100
ignore emacs stashing
owen-d 91a71e2
adds comments
owen-d 7bad731
ruler /loki/api/v1 prefix
owen-d 20e8d2d
Merge remote-tracking branch 'upstream/master' into feature/ruler-v3
owen-d cff0efa
revendoring compat
owen-d 2a8662a
comment
owen-d 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,6 @@ dist | |
coverage.txt | ||
.DS_Store | ||
.aws-sam | ||
|
||
# emacs | ||
.#* |
Large diffs are not rendered by default.
Oops, something went wrong.
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,123 @@ | ||
package manager | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/ruler" | ||
"github.com/go-kit/kit/log" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/prometheus/notifier" | ||
"github.com/prometheus/prometheus/pkg/labels" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/prometheus/prometheus/promql/parser" | ||
"github.com/prometheus/prometheus/rules" | ||
"github.com/weaveworks/common/user" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
"github.com/grafana/loki/pkg/logql" | ||
) | ||
|
||
// engineQueryFunc returns a new query function using the rules.EngineQueryFunc function | ||
// and passing an altered timestamp. | ||
func engineQueryFunc(engine *logql.Engine, delay time.Duration) rules.QueryFunc { | ||
return rules.QueryFunc(func(ctx context.Context, qs string, t time.Time) (promql.Vector, error) { | ||
adjusted := t.Add(-delay) | ||
params := logql.NewLiteralParams( | ||
qs, | ||
adjusted, | ||
adjusted, | ||
0, | ||
0, | ||
logproto.FORWARD, | ||
0, | ||
nil, | ||
) | ||
q := engine.Query(params) | ||
|
||
res, err := q.Exec(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch v := res.Data.(type) { | ||
case promql.Vector: | ||
return v, nil | ||
case promql.Scalar: | ||
return promql.Vector{promql.Sample{ | ||
Point: promql.Point(v), | ||
Metric: labels.Labels{}, | ||
}}, nil | ||
default: | ||
return nil, errors.New("rule result is not a vector or scalar") | ||
} | ||
}) | ||
|
||
} | ||
|
||
func MemstoreTenantManager( | ||
cfg ruler.Config, | ||
engine *logql.Engine, | ||
) ruler.ManagerFactory { | ||
var metrics *Metrics | ||
|
||
return func( | ||
ctx context.Context, | ||
userID string, | ||
notifier *notifier.Manager, | ||
logger log.Logger, | ||
reg prometheus.Registerer, | ||
) *rules.Manager { | ||
|
||
// We'll ignore the passed registere and use the default registerer to avoid prefix issues and other weirdness. | ||
// This closure prevents re-registering. | ||
if metrics == nil { | ||
metrics = NewMetrics(prometheus.DefaultRegisterer) | ||
} | ||
logger = log.With(logger, "user", userID) | ||
queryFunc := engineQueryFunc(engine, cfg.EvaluationDelay) | ||
memStore := NewMemStore(userID, queryFunc, metrics, 5*time.Minute, log.With(logger, "subcomponent", "MemStore")) | ||
|
||
mgr := rules.NewManager(&rules.ManagerOptions{ | ||
Appendable: NoopAppender{}, | ||
Queryable: memStore, | ||
QueryFunc: queryFunc, | ||
Context: user.InjectOrgID(ctx, userID), | ||
ExternalURL: cfg.ExternalURL.URL, | ||
NotifyFunc: ruler.SendAlerts(notifier, cfg.ExternalURL.URL.String()), | ||
Logger: logger, | ||
Registerer: reg, | ||
OutageTolerance: cfg.OutageTolerance, | ||
ForGracePeriod: cfg.ForGracePeriod, | ||
ResendDelay: cfg.ResendDelay, | ||
GroupLoader: groupLoader{}, | ||
}) | ||
|
||
// initialize memStore, bound to the manager's alerting rules | ||
memStore.Start(mgr) | ||
|
||
return mgr | ||
} | ||
} | ||
|
||
type groupLoader struct { | ||
rules.FileLoader // embed the default and override the parse method for logql queries | ||
} | ||
|
||
func (groupLoader) Parse(query string) (parser.Expr, error) { | ||
expr, err := logql.ParseExpr(query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return exprAdapter{expr}, nil | ||
} | ||
|
||
// Allows logql expressions to be treated as promql expressions by the prometheus rules pkg. | ||
type exprAdapter struct { | ||
logql.Expr | ||
} | ||
|
||
func (exprAdapter) PositionRange() parser.PositionRange { return parser.PositionRange{} } | ||
func (exprAdapter) PromQLExpr() {} | ||
func (exprAdapter) Type() parser.ValueType { return parser.ValueType("unimplemented") } |
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.
note: these changes are unrelated, but I snuck them in 😈