2020-09-13 15:07:59 +00:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 16:17:59 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2020-09-13 15:07:59 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2020-11-28 15:58:33 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2020-09-13 15:07:59 +00:00
|
|
|
"github.com/prometheus/prometheus/rules"
|
2020-11-22 22:24:38 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2020-10-17 15:36:58 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
2021-03-14 17:10:55 +00:00
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
2020-09-13 15:07:59 +00:00
|
|
|
)
|
|
|
|
|
2021-03-14 17:10:55 +00:00
|
|
|
const maxSamplesInMemory = 5000
|
|
|
|
|
2020-11-26 16:30:06 +00:00
|
|
|
type queryRangeAPI interface {
|
|
|
|
QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, v1.Warnings, error)
|
|
|
|
}
|
|
|
|
|
2020-10-31 13:40:24 +00:00
|
|
|
type ruleImporter struct {
|
2020-09-13 15:07:59 +00:00
|
|
|
logger log.Logger
|
2020-10-31 13:40:24 +00:00
|
|
|
config ruleImporterConfig
|
2020-09-13 15:07:59 +00:00
|
|
|
|
2020-11-26 16:30:06 +00:00
|
|
|
apiClient queryRangeAPI
|
2020-09-13 15:07:59 +00:00
|
|
|
|
2020-11-26 16:30:06 +00:00
|
|
|
groups map[string]*rules.Group
|
|
|
|
ruleManager *rules.Manager
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 13:40:24 +00:00
|
|
|
type ruleImporterConfig struct {
|
2021-03-14 17:10:55 +00:00
|
|
|
outputDir string
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
|
|
|
evalInterval time.Duration
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:58:33 +00:00
|
|
|
// newRuleImporter creates a new rule importer that can be used to parse and evaluate recording rule files and create new series
|
|
|
|
// written to disk in blocks.
|
2021-03-14 17:10:55 +00:00
|
|
|
func newRuleImporter(logger log.Logger, config ruleImporterConfig, apiClient queryRangeAPI) *ruleImporter {
|
2021-03-28 19:13:58 +00:00
|
|
|
level.Info(logger).Log("backfiller", "new rule importer from start", config.start.Format(time.RFC822), " to end", config.end.Format(time.RFC822))
|
2020-10-31 13:40:24 +00:00
|
|
|
return &ruleImporter{
|
2020-11-22 22:24:38 +00:00
|
|
|
logger: logger,
|
2020-09-13 15:07:59 +00:00
|
|
|
config: config,
|
2020-11-26 16:30:06 +00:00
|
|
|
apiClient: apiClient,
|
|
|
|
ruleManager: rules.NewManager(&rules.ManagerOptions{}),
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 15:58:33 +00:00
|
|
|
// loadGroups parses groups from a list of recording rule files.
|
2020-10-31 13:40:24 +00:00
|
|
|
func (importer *ruleImporter) loadGroups(ctx context.Context, filenames []string) (errs []error) {
|
2021-05-31 03:35:26 +00:00
|
|
|
groups, errs := importer.ruleManager.LoadGroups(importer.config.evalInterval, labels.Labels{}, "", filenames...)
|
2020-11-26 16:30:06 +00:00
|
|
|
if errs != nil {
|
|
|
|
return errs
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
importer.groups = groups
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-28 15:58:33 +00:00
|
|
|
// importAll evaluates all the recording rules and creates new time series and writes them to disk in blocks.
|
2020-11-26 16:30:06 +00:00
|
|
|
func (importer *ruleImporter) importAll(ctx context.Context) (errs []error) {
|
2020-11-22 22:24:38 +00:00
|
|
|
for name, group := range importer.groups {
|
2021-03-20 19:38:30 +00:00
|
|
|
level.Info(importer.logger).Log("backfiller", "processing group", "name", name)
|
2020-11-26 16:30:06 +00:00
|
|
|
|
2021-03-14 17:10:55 +00:00
|
|
|
stimeWithAlignment := group.EvalTimestamp(importer.config.start.UnixNano())
|
2020-11-28 15:58:33 +00:00
|
|
|
for i, r := range group.Rules() {
|
2021-03-20 19:38:30 +00:00
|
|
|
level.Info(importer.logger).Log("backfiller", "processing rule", "id", i, "name", r.Name())
|
2021-03-15 19:44:58 +00:00
|
|
|
if err := importer.importRule(ctx, r.Query().String(), r.Name(), r.Labels(), stimeWithAlignment, importer.config.end, group); err != nil {
|
2020-11-28 15:58:33 +00:00
|
|
|
errs = append(errs, err)
|
2020-11-22 22:24:38 +00:00
|
|
|
}
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2020-11-28 15:58:33 +00:00
|
|
|
// importRule queries a prometheus API to evaluate rules at times in the past.
|
2021-03-15 19:44:58 +00:00
|
|
|
func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr, ruleName string, ruleLabels labels.Labels, start, end time.Time, grp *rules.Group) (err error) {
|
2021-03-14 17:10:55 +00:00
|
|
|
blockDuration := tsdb.DefaultBlockDuration
|
2021-03-15 19:44:58 +00:00
|
|
|
startInMs := start.Unix() * int64(time.Second/time.Millisecond)
|
|
|
|
endInMs := end.Unix() * int64(time.Second/time.Millisecond)
|
|
|
|
|
2021-03-20 19:38:30 +00:00
|
|
|
for startOfBlock := blockDuration * (startInMs / blockDuration); startOfBlock <= endInMs; startOfBlock = startOfBlock + blockDuration {
|
|
|
|
endOfBlock := startOfBlock + blockDuration - 1
|
2020-11-28 15:58:33 +00:00
|
|
|
|
2021-03-20 19:38:30 +00:00
|
|
|
currStart := max(startOfBlock/int64(time.Second/time.Millisecond), start.Unix())
|
|
|
|
startWithAlignment := grp.EvalTimestamp(time.Unix(currStart, 0).UTC().UnixNano())
|
2020-11-28 15:58:33 +00:00
|
|
|
val, warnings, err := importer.apiClient.QueryRange(ctx,
|
|
|
|
ruleExpr,
|
|
|
|
v1.Range{
|
2021-03-15 19:44:58 +00:00
|
|
|
Start: startWithAlignment,
|
2021-03-20 19:38:30 +00:00
|
|
|
End: time.Unix(min(endOfBlock/int64(time.Second/time.Millisecond), end.Unix()), 0).UTC(),
|
2021-03-15 19:44:58 +00:00
|
|
|
Step: grp.Interval(),
|
2020-11-28 15:58:33 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-03-14 17:10:55 +00:00
|
|
|
return errors.Wrap(err, "query range")
|
2020-11-28 15:58:33 +00:00
|
|
|
}
|
|
|
|
if warnings != nil {
|
2021-03-20 19:38:30 +00:00
|
|
|
level.Warn(importer.logger).Log("msg", "Range query returned warnings.", "warnings", warnings)
|
2020-11-28 15:58:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 17:10:55 +00:00
|
|
|
// To prevent races with compaction, a block writer only allows appending samples
|
|
|
|
// that are at most half a block size older than the most recent sample appended so far.
|
|
|
|
// However, in the way we use the block writer here, compaction doesn't happen, while we
|
|
|
|
// also need to append samples throughout the whole block range. To allow that, we
|
|
|
|
// pretend that the block is twice as large here, but only really add sample in the
|
|
|
|
// original interval later.
|
|
|
|
w, err := tsdb.NewBlockWriter(log.NewNopLogger(), importer.config.outputDir, 2*tsdb.DefaultBlockDuration)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "new block writer")
|
|
|
|
}
|
2021-03-28 19:13:58 +00:00
|
|
|
var closed bool
|
2021-03-14 17:10:55 +00:00
|
|
|
defer func() {
|
2021-03-28 19:13:58 +00:00
|
|
|
if !closed {
|
|
|
|
err = tsdb_errors.NewMulti(err, w.Close()).Err()
|
|
|
|
}
|
2021-03-14 17:10:55 +00:00
|
|
|
}()
|
|
|
|
app := newMultipleAppender(ctx, w)
|
2020-11-28 15:58:33 +00:00
|
|
|
var matrix model.Matrix
|
|
|
|
switch val.Type() {
|
|
|
|
case model.ValMatrix:
|
|
|
|
matrix = val.(model.Matrix)
|
2021-03-20 19:38:30 +00:00
|
|
|
|
2020-11-28 15:58:33 +00:00
|
|
|
for _, sample := range matrix {
|
|
|
|
currentLabels := make(labels.Labels, 0, len(sample.Metric)+len(ruleLabels)+1)
|
2020-11-22 22:24:38 +00:00
|
|
|
currentLabels = append(currentLabels, labels.Label{
|
2020-11-28 15:58:33 +00:00
|
|
|
Name: labels.MetricName,
|
|
|
|
Value: ruleName,
|
2020-11-22 22:24:38 +00:00
|
|
|
})
|
2020-11-30 16:17:51 +00:00
|
|
|
|
|
|
|
currentLabels = append(currentLabels, ruleLabels...)
|
|
|
|
|
2020-11-28 15:58:33 +00:00
|
|
|
for name, value := range sample.Metric {
|
|
|
|
currentLabels = append(currentLabels, labels.Label{
|
|
|
|
Name: string(name),
|
|
|
|
Value: string(value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, value := range sample.Values {
|
2021-03-14 17:10:55 +00:00
|
|
|
if err := app.add(ctx, currentLabels, timestamp.FromTime(value.Timestamp.Time()), float64(value.Value)); err != nil {
|
|
|
|
return errors.Wrap(err, "add")
|
2020-11-28 15:58:33 +00:00
|
|
|
}
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 15:58:33 +00:00
|
|
|
default:
|
|
|
|
return errors.New(fmt.Sprintf("rule result is wrong type %s", val.Type().String()))
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
2021-03-14 17:10:55 +00:00
|
|
|
|
|
|
|
if err := app.flushAndCommit(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "flush and commit")
|
|
|
|
}
|
2021-03-29 13:45:12 +00:00
|
|
|
err = tsdb_errors.NewMulti(err, w.Close()).Err()
|
|
|
|
closed = true
|
2021-03-14 17:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMultipleAppender(ctx context.Context, blockWriter *tsdb.BlockWriter) *multipleAppender {
|
|
|
|
return &multipleAppender{
|
|
|
|
maxSamplesInMemory: maxSamplesInMemory,
|
|
|
|
writer: blockWriter,
|
|
|
|
appender: blockWriter.Appender(ctx),
|
2020-09-13 15:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 16:30:06 +00:00
|
|
|
|
|
|
|
// multipleAppender keeps track of how many series have been added to the current appender.
|
2021-04-02 01:01:17 +00:00
|
|
|
// If the max samples have been added, then all series are committed and a new appender is created.
|
2020-11-26 16:30:06 +00:00
|
|
|
type multipleAppender struct {
|
|
|
|
maxSamplesInMemory int
|
|
|
|
currentSampleCount int
|
|
|
|
writer *tsdb.BlockWriter
|
|
|
|
appender storage.Appender
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multipleAppender) add(ctx context.Context, l labels.Labels, t int64, v float64) error {
|
2021-03-01 13:49:49 +00:00
|
|
|
if _, err := m.appender.Append(0, l, t, v); err != nil {
|
2021-03-14 17:10:55 +00:00
|
|
|
return errors.Wrap(err, "multiappender append")
|
2020-11-26 16:30:06 +00:00
|
|
|
}
|
|
|
|
m.currentSampleCount++
|
2020-11-28 15:58:33 +00:00
|
|
|
if m.currentSampleCount >= m.maxSamplesInMemory {
|
2021-03-14 17:10:55 +00:00
|
|
|
return m.commit(ctx)
|
2020-11-26 16:30:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 17:10:55 +00:00
|
|
|
func (m *multipleAppender) commit(ctx context.Context) error {
|
2020-11-28 15:58:33 +00:00
|
|
|
if m.currentSampleCount == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 19:02:45 +00:00
|
|
|
if err := m.appender.Commit(); err != nil {
|
2021-03-14 17:10:55 +00:00
|
|
|
return errors.Wrap(err, "multiappender commit")
|
|
|
|
}
|
|
|
|
m.appender = m.writer.Appender(ctx)
|
|
|
|
m.currentSampleCount = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multipleAppender) flushAndCommit(ctx context.Context) error {
|
|
|
|
if err := m.commit(ctx); err != nil {
|
2020-11-26 16:30:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-30 19:02:45 +00:00
|
|
|
if _, err := m.writer.Flush(ctx); err != nil {
|
2021-03-14 17:10:55 +00:00
|
|
|
return errors.Wrap(err, "multiappender flush")
|
2020-11-26 16:30:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-15 19:44:58 +00:00
|
|
|
|
|
|
|
func max(x, y int64) int64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(x, y int64) int64 {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|