From 2e526cf2a7b26706701878e2095f6c8127160269 Mon Sep 17 00:00:00 2001 From: jessicagreben Date: Sun, 13 Sep 2020 08:38:32 -0700 Subject: [PATCH] add output dir parameter Signed-off-by: jessicagreben --- cmd/promtool/main.go | 9 ++++++--- cmd/promtool/rules.go | 18 +++++------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 0c78d93e7f..8197047bcf 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -135,6 +135,7 @@ func main() { Required().String() backfillRuleEnd := backfillRuleCmd.Flag("end", "If an end time is provided, all recording rules in the rule files provided will be backfilled to the end time. Default will backfill up to 3 hrs ago. End time should be RFC3339 or Unix timestamp."). Default("-3h").String() + backfillOutputDir := backfillRuleCmd.Flag("output dir", "The filepath on the local filesystem to write the output to. Output will be blocks containing the data of the backfilled recording rules.").Default("backfilldata/").String() backfillRuleURL := backfillRuleCmd.Flag("url", "Prometheus API url with the data where the rule will be backfilled from.").Default("localhost:9090").String() backfillRuleEvalInterval := backfillRuleCmd.Flag("evaluation_interval", "How frequently to evaluate rules when backfilling."). Default("15s").Duration() @@ -200,7 +201,7 @@ func main() { os.Exit(checkErr(dumpSamples(*dumpPath, *dumpMinTime, *dumpMaxTime))) case backfillRuleCmd.FullCommand(): - os.Exit(BackfillRule(*backfillRuleURL, *backfillRuleStart, *backfillRuleEnd, *backfillRuleEvalInterval, *backfillRuleFiles...)) + os.Exit(BackfillRule(*backfillRuleURL, *backfillRuleStart, *backfillRuleEnd, *backfillOutputDir, *backfillRuleEvalInterval, *backfillRuleFiles...)) } } @@ -766,8 +767,9 @@ func (j *jsonPrinter) printLabelValues(v model.LabelValues) { json.NewEncoder(os.Stdout).Encode(v) } -// BackfillRule backfills rules from the files provided. -func BackfillRule(url, start, end string, evalInterval time.Duration, files ...string) int { +// BackfillRule backfills recording rules from the files provided. The output are blocks of data +// at the outputDir location. +func BackfillRule(url, start, end, outputDir string, evalInterval time.Duration, files ...string) int { ctx := context.Background() stime, etime, err := parseStartTimeAndEndTime(start, end) if err != nil { @@ -777,6 +779,7 @@ func BackfillRule(url, start, end string, evalInterval time.Duration, files ...s cfg := RuleImporterConfig{ Start: stime, End: etime, + OutputDir: outputDir, EvalInterval: evalInterval, URL: url, } diff --git a/cmd/promtool/rules.go b/cmd/promtool/rules.go index 52bf226880..5fa100e49f 100644 --- a/cmd/promtool/rules.go +++ b/cmd/promtool/rules.go @@ -16,7 +16,6 @@ package main import ( "context" "fmt" - "io/ioutil" "os" "time" @@ -49,6 +48,7 @@ type RuleImporter struct { type RuleImporterConfig struct { Start time.Time End time.Time + OutputDir string EvalInterval time.Duration URL string } @@ -64,12 +64,10 @@ func NewRuleImporter(logger log.Logger, config RuleImporterConfig) *RuleImporter // Init initializes the rule importer which creates a new block writer // and creates an Prometheus API client. func (importer *RuleImporter) Init() error { - // todo: clean up dir - newBlockDir, err := ioutil.TempDir("", "rule_blocks") - if err != nil { - return err - } - importer.writer = blocks.NewMultiWriter(importer.logger, newBlockDir, importer.config.EvalInterval.Nanoseconds()) + importer.writer = blocks.NewMultiWriter(importer.logger, + importer.config.OutputDir, + importer.config.EvalInterval.Nanoseconds(), + ) config := api.Config{ Address: importer.config.URL, @@ -84,7 +82,6 @@ func (importer *RuleImporter) Init() error { // Close cleans up any open resources. func (importer *RuleImporter) Close() error { - // todo: clean up any dirs that were created return importer.writer.Close() } @@ -99,21 +96,17 @@ func (importer *RuleImporter) LoadGroups(ctx context.Context, filenames []string } for _, ruleGroup := range rgs.Groups { - itv := importer.config.EvalInterval if ruleGroup.Interval != 0 { itv = time.Duration(ruleGroup.Interval) } rgRules := make([]rules.Rule, 0, len(ruleGroup.Rules)) - for _, r := range ruleGroup.Rules { - expr, err := importer.groupLoader.Parse(r.Expr.Value) if err != nil { return []error{errors.Wrap(err, filename)} } - rgRules = append(rgRules, rules.NewRecordingRule( r.Record.Value, expr, @@ -158,7 +151,6 @@ func (importer *RuleImporter) ImportAll(ctx context.Context) []error { // 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 { ts := stimeWithAlignment - appender := importer.writer.Appender() for ts.Before(importer.config.End) {