mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
add name and labels to metric, eval all rules for each block
Signed-off-by: jessicagreben <jessicagrebens@gmail.com>
This commit is contained in:
parent
75654715d3
commit
19dee0a569
|
@ -20,12 +20,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-kit/kit/log"
|
"github.com/go-kit/kit/log"
|
||||||
|
"github.com/go-kit/kit/log/level"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/prometheus/client_golang/api"
|
"github.com/prometheus/client_golang/api"
|
||||||
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"github.com/prometheus/prometheus/pkg/labels"
|
"github.com/prometheus/prometheus/pkg/labels"
|
||||||
"github.com/prometheus/prometheus/rules"
|
"github.com/prometheus/prometheus/rules"
|
||||||
|
"github.com/prometheus/prometheus/storage"
|
||||||
"github.com/prometheus/prometheus/tsdb"
|
"github.com/prometheus/prometheus/tsdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -54,6 +56,7 @@ type ruleImporterConfig struct {
|
||||||
// newRuleImporter creates a new rule importer that can be used to backfill rules.
|
// newRuleImporter creates a new rule importer that can be used to backfill rules.
|
||||||
func newRuleImporter(logger log.Logger, config ruleImporterConfig) *ruleImporter {
|
func newRuleImporter(logger log.Logger, config ruleImporterConfig) *ruleImporter {
|
||||||
return &ruleImporter{
|
return &ruleImporter{
|
||||||
|
logger: logger,
|
||||||
config: config,
|
config: config,
|
||||||
groupLoader: rules.FileLoader{},
|
groupLoader: rules.FileLoader{},
|
||||||
}
|
}
|
||||||
|
@ -134,39 +137,53 @@ func (importer *ruleImporter) loadGroups(ctx context.Context, filenames []string
|
||||||
// and stores them in new blocks.
|
// and stores them in new blocks.
|
||||||
func (importer *ruleImporter) importAll(ctx context.Context) []error {
|
func (importer *ruleImporter) importAll(ctx context.Context) []error {
|
||||||
var errs = []error{}
|
var errs = []error{}
|
||||||
for _, group := range importer.groups {
|
var currentBlockEnd time.Time
|
||||||
|
var appender storage.Appender
|
||||||
|
|
||||||
|
for name, group := range importer.groups {
|
||||||
|
level.Info(importer.logger).Log("backfiller", fmt.Sprintf("processing group, name: %s", name))
|
||||||
stimeWithAlignment := group.EvalTimestamp(importer.config.Start.UnixNano())
|
stimeWithAlignment := group.EvalTimestamp(importer.config.Start.UnixNano())
|
||||||
|
|
||||||
for _, r := range group.Rules() {
|
ts := stimeWithAlignment
|
||||||
err := importer.importRule(ctx, r.Query().String(), stimeWithAlignment, group.Interval())
|
// a 2-hr block that contains all the data for each rule
|
||||||
|
for ts.Before(importer.config.End) {
|
||||||
|
currentBlockEnd = ts.Add(time.Duration(tsdb.DefaultBlockDuration) * time.Millisecond)
|
||||||
|
if currentBlockEnd.After(importer.config.End) {
|
||||||
|
currentBlockEnd = importer.config.End
|
||||||
|
}
|
||||||
|
// should we be creating a new appender for each block?
|
||||||
|
appender = importer.writer.Appender(ctx)
|
||||||
|
|
||||||
|
for i, r := range group.Rules() {
|
||||||
|
level.Info(importer.logger).Log("backfiller", fmt.Sprintf("processing rule %d, name: %s", i+1, r.Name()))
|
||||||
|
err := importer.importRule(ctx, r.Query().String(), r.Name(), r.Labels(), ts, currentBlockEnd, appender)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
ts = currentBlockEnd
|
||||||
_, err := importer.writer.Flush(ctx)
|
_, err := importer.writer.Flush(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = appender.Commit()
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// importRule imports the historical data for a single rule.
|
// importRule imports the historical data for a single rule.
|
||||||
func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr string, stimeWithAlignment time.Time, internval time.Duration) error {
|
func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr, ruleName string, ruleLabels labels.Labels, start, end time.Time, appender storage.Appender) error {
|
||||||
ts := stimeWithAlignment
|
|
||||||
appender := importer.writer.Appender(ctx)
|
|
||||||
|
|
||||||
for ts.Before(importer.config.End) {
|
|
||||||
currentBlockEnd := ts.Add(time.Duration(tsdb.DefaultBlockDuration) * time.Millisecond)
|
|
||||||
if currentBlockEnd.After(importer.config.End) {
|
|
||||||
currentBlockEnd = importer.config.End
|
|
||||||
}
|
|
||||||
|
|
||||||
val, warnings, err := importer.apiClient.QueryRange(ctx,
|
val, warnings, err := importer.apiClient.QueryRange(ctx,
|
||||||
ruleExpr,
|
ruleExpr,
|
||||||
v1.Range{
|
v1.Range{
|
||||||
Start: ts,
|
Start: start,
|
||||||
End: currentBlockEnd,
|
End: end,
|
||||||
Step: importer.config.EvalInterval,
|
Step: importer.config.EvalInterval,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -182,7 +199,15 @@ func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr string, s
|
||||||
case model.ValMatrix:
|
case model.ValMatrix:
|
||||||
matrix = val.(model.Matrix)
|
matrix = val.(model.Matrix)
|
||||||
for _, sample := range matrix {
|
for _, sample := range matrix {
|
||||||
|
|
||||||
currentLabels := make(labels.Labels, 0, len(sample.Metric))
|
currentLabels := make(labels.Labels, 0, len(sample.Metric))
|
||||||
|
currentLabels = append(currentLabels, labels.Label{
|
||||||
|
Name: labels.MetricName,
|
||||||
|
Value: ruleName,
|
||||||
|
})
|
||||||
|
for _, ruleLabel := range ruleLabels {
|
||||||
|
currentLabels = append(currentLabels, ruleLabel)
|
||||||
|
}
|
||||||
for k, v := range sample.Metric {
|
for k, v := range sample.Metric {
|
||||||
currentLabels = append(currentLabels, labels.Label{
|
currentLabels = append(currentLabels, labels.Label{
|
||||||
Name: string(k),
|
Name: string(k),
|
||||||
|
@ -199,12 +224,5 @@ func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr string, s
|
||||||
default:
|
default:
|
||||||
return errors.New("rule result is wrong type")
|
return errors.New("rule result is wrong type")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
ts = currentBlockEnd
|
|
||||||
}
|
|
||||||
_, err := importer.writer.Flush(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return appender.Commit()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,10 +84,6 @@ func (w *BlockWriter) Appender(ctx context.Context) storage.Appender {
|
||||||
// Flush implements the Writer interface. This is where actual block writing
|
// Flush implements the Writer interface. This is where actual block writing
|
||||||
// happens. After flush completes, no writes can be done.
|
// happens. After flush completes, no writes can be done.
|
||||||
func (w *BlockWriter) Flush(ctx context.Context) (ulid.ULID, error) {
|
func (w *BlockWriter) Flush(ctx context.Context) (ulid.ULID, error) {
|
||||||
if w.head.NumSeries() == 0 {
|
|
||||||
return ulid.ULID{}, errors.New("no series appended, aborting")
|
|
||||||
}
|
|
||||||
|
|
||||||
mint := w.head.MinTime()
|
mint := w.head.MinTime()
|
||||||
// Add +1 millisecond to block maxt because block intervals are half-open: [b.MinTime, b.MaxTime).
|
// Add +1 millisecond to block maxt because block intervals are half-open: [b.MinTime, b.MaxTime).
|
||||||
// Because of this block intervals are always +1 than the total samples it includes.
|
// Because of this block intervals are always +1 than the total samples it includes.
|
||||||
|
|
Loading…
Reference in a new issue