2020-09-13 08:07:59 -07: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 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2020-09-13 08:07:59 -07: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 07:58:33 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2020-09-13 08:07:59 -07:00
|
|
|
"github.com/prometheus/prometheus/rules"
|
2020-11-22 14:24:38 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2020-10-17 08:36:58 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
2021-03-14 10:10:55 -07:00
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
2020-09-13 08:07:59 -07:00
|
|
|
)
|
|
|
|
|
2021-03-14 10:10:55 -07:00
|
|
|
const maxSamplesInMemory = 5000
|
|
|
|
|
2020-11-26 08:30:06 -08:00
|
|
|
type queryRangeAPI interface {
|
|
|
|
QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, v1.Warnings, error)
|
|
|
|
}
|
|
|
|
|
2020-10-31 06:40:24 -07:00
|
|
|
type ruleImporter struct {
|
2020-09-13 08:07:59 -07:00
|
|
|
logger log.Logger
|
2020-10-31 06:40:24 -07:00
|
|
|
config ruleImporterConfig
|
2020-09-13 08:07:59 -07:00
|
|
|
|
2020-11-26 08:30:06 -08:00
|
|
|
apiClient queryRangeAPI
|
2020-09-13 08:07:59 -07:00
|
|
|
|
2020-11-26 08:30:06 -08:00
|
|
|
groups map[string]*rules.Group
|
|
|
|
ruleManager *rules.Manager
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
|
|
|
|
2020-10-31 06:40:24 -07:00
|
|
|
type ruleImporterConfig struct {
|
2021-03-14 10:10:55 -07:00
|
|
|
outputDir string
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
|
|
|
evalInterval time.Duration
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
|
|
|
|
2020-11-28 07:58:33 -08: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 10:10:55 -07:00
|
|
|
func newRuleImporter(logger log.Logger, config ruleImporterConfig, apiClient queryRangeAPI) *ruleImporter {
|
2021-09-06 19:19:29 -07:00
|
|
|
level.Info(logger).Log("backfiller", "new rule importer", "start", config.start.Format(time.RFC822), "end", config.end.Format(time.RFC822))
|
2020-10-31 06:40:24 -07:00
|
|
|
return &ruleImporter{
|
2020-11-22 14:24:38 -08:00
|
|
|
logger: logger,
|
2020-09-13 08:07:59 -07:00
|
|
|
config: config,
|
2020-11-26 08:30:06 -08:00
|
|
|
apiClient: apiClient,
|
|
|
|
ruleManager: rules.NewManager(&rules.ManagerOptions{}),
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
// loadGroups parses groups from a list of recording rule files.
|
2020-10-31 06:40:24 -07:00
|
|
|
func (importer *ruleImporter) loadGroups(ctx context.Context, filenames []string) (errs []error) {
|
2021-05-30 20:35:26 -07:00
|
|
|
groups, errs := importer.ruleManager.LoadGroups(importer.config.evalInterval, labels.Labels{}, "", filenames...)
|
2020-11-26 08:30:06 -08:00
|
|
|
if errs != nil {
|
|
|
|
return errs
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
|
|
|
importer.groups = groups
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
// importAll evaluates all the recording rules and creates new time series and writes them to disk in blocks.
|
2020-11-26 08:30:06 -08:00
|
|
|
func (importer *ruleImporter) importAll(ctx context.Context) (errs []error) {
|
2020-11-22 14:24:38 -08:00
|
|
|
for name, group := range importer.groups {
|
2021-03-20 12:38:30 -07:00
|
|
|
level.Info(importer.logger).Log("backfiller", "processing group", "name", name)
|
2020-11-26 08:30:06 -08:00
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
for i, r := range group.Rules() {
|
2021-03-20 12:38:30 -07:00
|
|
|
level.Info(importer.logger).Log("backfiller", "processing rule", "id", i, "name", r.Name())
|
2021-09-14 14:10:43 -07:00
|
|
|
if err := importer.importRule(ctx, r.Query().String(), r.Name(), r.Labels(), importer.config.start, importer.config.end, group); err != nil {
|
2020-11-28 07:58:33 -08:00
|
|
|
errs = append(errs, err)
|
2020-11-22 14:24:38 -08:00
|
|
|
}
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
// importRule queries a prometheus API to evaluate rules at times in the past.
|
2021-03-15 12:44:58 -07: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 10:10:55 -07:00
|
|
|
blockDuration := tsdb.DefaultBlockDuration
|
2021-03-15 12:44:58 -07:00
|
|
|
startInMs := start.Unix() * int64(time.Second/time.Millisecond)
|
|
|
|
endInMs := end.Unix() * int64(time.Second/time.Millisecond)
|
|
|
|
|
2021-03-20 12:38:30 -07:00
|
|
|
for startOfBlock := blockDuration * (startInMs / blockDuration); startOfBlock <= endInMs; startOfBlock = startOfBlock + blockDuration {
|
|
|
|
endOfBlock := startOfBlock + blockDuration - 1
|
2020-11-28 07:58:33 -08:00
|
|
|
|
2021-03-20 12:38:30 -07:00
|
|
|
currStart := max(startOfBlock/int64(time.Second/time.Millisecond), start.Unix())
|
|
|
|
startWithAlignment := grp.EvalTimestamp(time.Unix(currStart, 0).UTC().UnixNano())
|
2021-09-11 09:31:40 -07:00
|
|
|
for startWithAlignment.Unix() < currStart {
|
|
|
|
startWithAlignment = startWithAlignment.Add(grp.Interval())
|
|
|
|
}
|
2021-09-15 07:02:41 -07:00
|
|
|
end := time.Unix(min(endOfBlock/int64(time.Second/time.Millisecond), end.Unix()), 0).UTC()
|
|
|
|
if end.Before(startWithAlignment) {
|
|
|
|
break
|
|
|
|
}
|
2020-11-28 07:58:33 -08:00
|
|
|
val, warnings, err := importer.apiClient.QueryRange(ctx,
|
|
|
|
ruleExpr,
|
|
|
|
v1.Range{
|
2021-03-15 12:44:58 -07:00
|
|
|
Start: startWithAlignment,
|
2021-09-15 07:02:41 -07:00
|
|
|
End: end,
|
2021-03-15 12:44:58 -07:00
|
|
|
Step: grp.Interval(),
|
2020-11-28 07:58:33 -08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-03-14 10:10:55 -07:00
|
|
|
return errors.Wrap(err, "query range")
|
2020-11-28 07:58:33 -08:00
|
|
|
}
|
|
|
|
if warnings != nil {
|
2021-03-20 12:38:30 -07:00
|
|
|
level.Warn(importer.logger).Log("msg", "Range query returned warnings.", "warnings", warnings)
|
2020-11-28 07:58:33 -08:00
|
|
|
}
|
|
|
|
|
2021-03-14 10:10:55 -07: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 12:13:58 -07:00
|
|
|
var closed bool
|
2021-03-14 10:10:55 -07:00
|
|
|
defer func() {
|
2021-03-28 12:13:58 -07:00
|
|
|
if !closed {
|
|
|
|
err = tsdb_errors.NewMulti(err, w.Close()).Err()
|
|
|
|
}
|
2021-03-14 10:10:55 -07:00
|
|
|
}()
|
|
|
|
app := newMultipleAppender(ctx, w)
|
2020-11-28 07:58:33 -08:00
|
|
|
var matrix model.Matrix
|
|
|
|
switch val.Type() {
|
|
|
|
case model.ValMatrix:
|
|
|
|
matrix = val.(model.Matrix)
|
2021-03-20 12:38:30 -07:00
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
for _, sample := range matrix {
|
2021-09-11 09:31:40 -07:00
|
|
|
lb := labels.NewBuilder(ruleLabels)
|
2020-11-30 08:17:51 -08:00
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
for name, value := range sample.Metric {
|
2021-09-11 09:31:40 -07:00
|
|
|
lb.Set(string(name), string(value))
|
2020-11-28 07:58:33 -08:00
|
|
|
}
|
2021-09-11 09:31:40 -07:00
|
|
|
|
|
|
|
lb.Set(labels.MetricName, ruleName)
|
|
|
|
|
2020-11-28 07:58:33 -08:00
|
|
|
for _, value := range sample.Values {
|
2021-09-11 09:31:40 -07:00
|
|
|
if err := app.add(ctx, lb.Labels(), timestamp.FromTime(value.Timestamp.Time()), float64(value.Value)); err != nil {
|
2021-03-14 10:10:55 -07:00
|
|
|
return errors.Wrap(err, "add")
|
2020-11-28 07:58:33 -08:00
|
|
|
}
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 07:58:33 -08:00
|
|
|
default:
|
|
|
|
return errors.New(fmt.Sprintf("rule result is wrong type %s", val.Type().String()))
|
2020-09-13 08:07:59 -07:00
|
|
|
}
|
2021-03-14 10:10:55 -07:00
|
|
|
|
|
|
|
if err := app.flushAndCommit(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "flush and commit")
|
|
|
|
}
|
2021-03-29 06:45:12 -07:00
|
|
|
err = tsdb_errors.NewMulti(err, w.Close()).Err()
|
|
|
|
closed = true
|
2021-03-14 10:10:55 -07: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 08:07:59 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 08:30:06 -08:00
|
|
|
|
|
|
|
// multipleAppender keeps track of how many series have been added to the current appender.
|
2021-04-01 18:01:17 -07:00
|
|
|
// If the max samples have been added, then all series are committed and a new appender is created.
|
2020-11-26 08:30:06 -08: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 05:49:49 -08:00
|
|
|
if _, err := m.appender.Append(0, l, t, v); err != nil {
|
2021-03-14 10:10:55 -07:00
|
|
|
return errors.Wrap(err, "multiappender append")
|
2020-11-26 08:30:06 -08:00
|
|
|
}
|
|
|
|
m.currentSampleCount++
|
2020-11-28 07:58:33 -08:00
|
|
|
if m.currentSampleCount >= m.maxSamplesInMemory {
|
2021-03-14 10:10:55 -07:00
|
|
|
return m.commit(ctx)
|
2020-11-26 08:30:06 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 10:10:55 -07:00
|
|
|
func (m *multipleAppender) commit(ctx context.Context) error {
|
2020-11-28 07:58:33 -08:00
|
|
|
if m.currentSampleCount == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 11:02:45 -08:00
|
|
|
if err := m.appender.Commit(); err != nil {
|
2021-03-14 10:10:55 -07: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 08:30:06 -08:00
|
|
|
return err
|
|
|
|
}
|
2020-11-30 11:02:45 -08:00
|
|
|
if _, err := m.writer.Flush(ctx); err != nil {
|
2021-03-14 10:10:55 -07:00
|
|
|
return errors.Wrap(err, "multiappender flush")
|
2020-11-26 08:30:06 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-15 12:44:58 -07: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
|
|
|
|
}
|