-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* begins speccing out ruler ruler memhistory encapsulates metricsHistory removes mutex from memhistory ruler module * upstream conflicts * implicit ast impls, parser for ruler * /api/prom ruler routes, ruler enabled in single binary * registers ruler flags, doesnt double instantiate metrics * cleanup for old samples in ruler * begins ruler tests * ForStateAppenderQuerier tests * memhistory stop * RestoreForState test * upstream querier ifc * introducing loki ruler metrics * removes rule granularity metric -- to be discussed in pr * validates ruler cfg * renames gauge metrics to not use total * removes unnecessary logs * logs synthetic restoreforstate * logs tenant in ruler * sets cortex to owen's unmerged fork * begins porting rules pkg * memstore work * work on queryable based in memory series store * removes unused pkgs, adds memstore test * MemStore must be started after construction * MemstoreTenantManager * ruler loading * ruler instantiation * better metrics & logging in ruler * grpc cortex compatibility in go.mod * cortex vendoring compat * increments memory cache hits only if cached * loki in memory metrics use prometheus default registerer * ruler only depends on ring * managerfactory rename * revendors cortex * ignore emacs stashing * adds comments * ruler /loki/api/v1 prefix * revendoring compat * comment
- Loading branch information
Showing
9 changed files
with
868 additions
and
189 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,6 @@ dist | |
coverage.txt | ||
.DS_Store | ||
.aws-sam | ||
|
||
# emacs | ||
.#* |
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.