Skip to content

Add rule importer to backfill - #7675

Merged
beorn7 merged 24 commits into
prometheus:mainfrom
JessicaGreben:jg/11-retroactive-rule-eval
Mar 29, 2021
Merged

beorn7 merged 24 commits into
prometheus:mainfrom
JessicaGreben:jg/11-retroactive-rule-eval

Conversation

@JessicaGreben

@JessicaGreben JessicaGreben commented Jul 27, 2020 •

Copy link
Copy Markdown
Contributor

This PR implements a solution for issue #11 to add the ability to retroactively evaluate new rules so that there is historical data for new rules. For details see design doc here.

This PR includes the following code changes:

  • creates a rule importer. The rule importer parses recording rule files and creates tsdb blocks of recording rules from historical prometheus data.
  • creates a promtool subcommand promtool tsdb create-blocks-from rules to execute backfilling data for a new rule
  • the output of the subcommand is a directory that contains blocks with the series created from evaluating the recording rules on old data

Things not in the PR that must be added:

  • add documentation in the "storage" page (ref)
  • add stale markers

Things not in this PR, but might be nice to add at a later date:

  • long running service to backfill any amount of data
  • backfill alerting rules
  • ability to move the output blocks to a prometheus data dir and perform validation before doing so

Example usage:

$ promtool tsdb create-blocks-from rules --start=1606382352 --end=1606389552 \
    --output-dir <path/to/dir/to/write/data> \
    --url <prometheus api addr and port> \
    rules1.yml,rules2.yml

Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread importers/rules.go Resolved
Comment thread importers/rules.go Resolved
Comment thread importers/rules.go Outdated
}

appender := importer.writer.Appender()
for ts.Before(end) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still using the user-specified time for alignment, which won't work out.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we just talked about this.

The idea would be starting from zero if no data is yet in Prom
But actually we could use instead of 0, a group hash as in Prometheus. The tricky part is a filename, as it obviously will be different.

// evalTimestamp returns the immediately preceding consistently slotted evaluation time.
func (g *Group) evalTimestamp() time.Time {
	var (
		offset = int64(g.hash() % uint64(g.interval))
		now    = time.Now().UnixNano()
		adjNow = now - offset
		base   = adjNow - (adjNow % int64(g.interval))
	)

	return time.Unix(0, base+offset).UTC()
}

If the data exists we can fetch samples from Prometheus and aligned (:

@JessicaGreben JessicaGreben Sep 13, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I exported the evalTimestamp on the Group and used that in my code for the alignment. Let me know if you see any glaring problems with that. I still need to test it, which I'm going to do today.

@bwplotka why will the filename obviously be different? And does it matter if it is? If the end user changes the filename, then the alignment will be different. That seems like it could be fine.

Comment thread importers/rules.go Resolved
Comment thread importers/rules.go Resolved
Comment thread importers/rules.go Resolved
@bwplotka

Copy link
Copy Markdown
Member

Let us know if it's ready to review! (:

@JessicaGreben

JessicaGreben commented Sep 2, 2020 •

Copy link
Copy Markdown
Contributor Author

Sorry for the slow reply to comments (had some life stuff to take care of). I'm back at it now and aiming to have this PR ready for review in a week. Will ping you all when it's ready.

@JessicaGreben

JessicaGreben commented Sep 13, 2020 •

Copy link
Copy Markdown
Contributor Author

@bwplotka and @brian-brazil I made some changes per your comments, not sure if I'm handling the alignment correctly and the queryRange API right. I would appreciate another review when you all have a free moment. Thanks so much for the help! Once I get a green light on the implementation I will start writing tests.

Signed-off-by: jessicagreben <[email protected]>

@brian-brazil brian-brazil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall approach seems okay, but some details need improvement.

Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread rules/manager.go Resolved
Comment thread tsdb/importer/blocks/multi.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
@brian-brazil

Copy link
Copy Markdown
Contributor

Did you forget to push?

Can we just hardcode 2 hr blocks for now and make it a flag another day if needed?

It should be a flag from the start I think, so you can pass in a smaller value if you've an expensive query.

@JessicaGreben

JessicaGreben commented Sep 21, 2020 •

Copy link
Copy Markdown
Contributor Author

@brian-brazil I will add a flag, I will push a change in a bit.

@codesome

Copy link
Copy Markdown
Member

I have not looked at the implementation yet, but wanted to say that as with the normal backfill, we need to align the block boundaries of new blocks being created with existing blocks to avoid formation of a single huge block (vertical compaction merges all overlapping block into one).

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial review.

Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/rules.go Resolved
@bwplotka

Copy link
Copy Markdown
Member

Ups, misclick I did not plan to put green mark yet ;p But very close!

Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
@JessicaGreben
JessicaGreben force-pushed the jg/11-retroactive-rule-eval branch from e03fd4e to 3ed6457 Compare October 31, 2020 13:38
jessicagreben added 3 commits October 31, 2020 06:40
Signed-off-by: jessicagreben <[email protected]>
Signed-off-by: jessicagreben <[email protected]>
Signed-off-by: jessicagreben <[email protected]>
@beorn7

beorn7 commented Nov 2, 2020

Copy link
Copy Markdown
Contributor

I collected a few thoughts about the command structure here, roughly continuing where we just ended the sync meeting.

@JessicaGreben

Copy link
Copy Markdown
Contributor Author

Its not quite ready. Needs a little more testing, but almost there. I have been busy with other things, but I can get back on it this week.

Base automatically changed from master to main February 23, 2021 19:36
jessicagreben added 4 commits February 24, 2021 09:42
Signed-off-by: jessicagreben <[email protected]>
Signed-off-by: jessicagreben <[email protected]>
Signed-off-by: jessicagreben <[email protected]>

@roidelapluie roidelapluie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have looked at this pull request during our contributor hour. The pull request is in a good shape and would be ready to be merged after we aligned blocks capped at the 2hrs mark.

We have agreed that staleness markers would be handled later, in a future pull request.

Comment thread cmd/promtool/main.go Resolved
@JessicaGreben
JessicaGreben requested a review from codesome as a code owner March 14, 2021 17:18
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved

@roidelapluie roidelapluie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again for your work!

I don't think we should have all those details in the command line. Let's instead work on documentation in the storage part in another pull request.

I have added a couple of small comments, then I think we are ready to go.

Comment thread .gitignore Resolved
Comment thread tsdb/blockwriter.go
// Flush implements the Writer interface. This is where actual block writing
// happens. After flush completes, no writes can be done.
func (w *BlockWriter) Flush(ctx context.Context) (ulid.ULID, error) {
seriesCount := w.head.NumSeries()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only trying to remove line 94-96 because if I backfill a recording rule and there are no series created for that rule, it errors on line 95. Plus when I flush when there are no series, it does not create a block so I'm not sure why the w.head.NumSeries() == 0 check was being made.

However I can keep line 93 and line 102 to log that info.

Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/main.go Outdated
importRulesStart := importRulesCmd.Flag("start", "The time to start backfilling the new rule from. It is required. Start time should be RFC3339 or Unix timestamp.").
Required().String()
importRulesEnd := importRulesCmd.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 hours ago. End time should be RFC3339 or Unix timestamp.").String()
importRulesOutputDir := importRulesCmd.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. Don't use an active Prometheus data directory. If command is run many times with same start/end time, it will create duplicate series.").Default("backfilldata/").String()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
importRulesOutputDir := importRulesCmd.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. Don't use an active Prometheus data directory. If command is run many times with same start/end time, it will create duplicate series.").Default("backfilldata/").String()
importRulesOutputDir := importRulesCmd.Flag("output-dir", "Output directory for generated blocks.").Default(defaultDBPath).String()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those should go to the "storage" page in the documentation. We can do it in a second PR.

Comment thread cmd/promtool/main.go Resolved
Comment thread cmd/promtool/main.go
Default("60s").Duration()
importRulesFiles := importRulesCmd.Arg(
"rule-files",
"A list of one or more files containing recording rules to be backfilled. All recording rules listed in the files will be backfilled. Alerting rules are not evaluated.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"A list of one or more files containing recording rules to be backfilled. All recording rules listed in the files will be backfilled. Alerting rules are not evaluated.",
"A list of one or more files containing recording rules to be backfilled. Please refer to the storage docs for more details.",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to make this change once we add the docs.

Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved

@roidelapluie roidelapluie left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

Since the end time is dynamic, I would add a log line about the actual start and end time picked by promtool.

I also think we should try to close the blocks earlier than at the end. I have added a suggestion.

Comment thread cmd/promtool/rules.go Resolved
Comment thread cmd/promtool/rules.go Resolved
@roidelapluie

Copy link
Copy Markdown
Member

Thank you! Did you notice that the tests are failing?

@JessicaGreben

Copy link
Copy Markdown
Contributor Author

oops let me fix those failing tests

Signed-off-by: jessicagreben <[email protected]>
@beorn7

beorn7 commented Mar 29, 2021

Copy link
Copy Markdown
Contributor

As per message from @roidelapluie, merging this now.

@beorn7
beorn7 merged commit 9549a15 into prometheus:main Mar 29, 2021
@bboreham

Copy link
Copy Markdown
Member

Wow, heroic year-long effort. Well done!

@JessicaGreben
JessicaGreben deleted the jg/11-retroactive-rule-eval branch March 30, 2021 14:13
@marco-m-pix4d

Copy link
Copy Markdown

Hello @JessicaGreben, when I discovered about this feature in the Medium article you wrote, I had to find this PR, to thank you. You are reducing my pain with Prometheus rules. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants