-
Notifications
You must be signed in to change notification settings - Fork 695
Adding step invariant metrics and fast path for instant queries. #13911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tcp13equals2
wants to merge
3
commits into
main
Choose a base branch
from
track_step_invariant_usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
pkg/streamingpromql/operators/metrics/operator_metrics_tracker.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // SPDX-License-Identifier: AGPL-3.0-only | ||
|
|
||
| package operatormetrics | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| ) | ||
|
|
||
| // MetricsTracker holds metrics trackers used by operators. | ||
| // These trackers can be passed into operators to elicit metrics | ||
| // during their lifecycle. | ||
| type MetricsTracker struct { | ||
| StepInvariantTracker *StepInvariantExpressionMetricsTracker | ||
| } | ||
|
|
||
| func NewOperatorMetricsTracker(reg prometheus.Registerer) *MetricsTracker { | ||
| return &MetricsTracker{ | ||
| StepInvariantTracker: newStepInvariantExpressionMetricsTracker(reg), | ||
| } | ||
| } | ||
|
|
||
| type StepInvariantPointType int | ||
|
|
||
| const ( | ||
| FPoint StepInvariantPointType = iota | ||
| HPoint StepInvariantPointType = iota | ||
| ) | ||
|
|
||
| var stepInvariantPointTypeLabels = []string{ | ||
| "fpoint", "hpoint", | ||
| } | ||
|
|
||
| type StepInvariantExpressionMetricsTracker struct { | ||
| // total number of observed step invariant nodes | ||
| nodes prometheus.Counter | ||
|
|
||
| // total number of points which were saved from being retrieved from the inner operation | ||
| points *prometheus.CounterVec | ||
| } | ||
|
|
||
| // OnStepInvariantNodeObserved is called when a step invariant node is observed. | ||
| // It increments the nodes counter by 1. | ||
| func (t *StepInvariantExpressionMetricsTracker) OnStepInvariantNodeObserved() { | ||
| t.nodes.Inc() | ||
| } | ||
|
|
||
| // NodeCounter returns the counter tracking step invariant nodes. | ||
| // This is provided for unit tests and allowing the underlying counter to remain unexported. | ||
| func (t *StepInvariantExpressionMetricsTracker) NodeCounter() prometheus.Counter { | ||
| return t.nodes | ||
| } | ||
|
|
||
| // Counter returns the counter tracking step invariant point savings for a given type. | ||
| // This is provided for unit tests and allowing the underlying counter to remain unexported. | ||
| func (t *StepInvariantExpressionMetricsTracker) Counter(pointType StepInvariantPointType) prometheus.Counter { | ||
| return t.points.WithLabelValues(stepInvariantPointTypeLabels[pointType]) | ||
| } | ||
|
|
||
| // OnStepInvariantStepsSaved increments the counter related to tracking step invariant point savings for the given point type. | ||
| // The actual counter will be incremented by stepCount-1, reflecting that the first step loaded data. | ||
| // If the stepCount is less than or equal to 1 then this function is a no-op. | ||
| func (t *StepInvariantExpressionMetricsTracker) OnStepInvariantStepsSaved(pointType StepInvariantPointType, stepCount int) { | ||
| if stepCount <= 1 { | ||
| return | ||
| } | ||
| t.points.WithLabelValues(stepInvariantPointTypeLabels[pointType]).Add(float64(stepCount - 1)) | ||
| } | ||
|
|
||
| func newStepInvariantExpressionMetricsTracker(reg prometheus.Registerer) *StepInvariantExpressionMetricsTracker { | ||
| return &StepInvariantExpressionMetricsTracker{ | ||
| nodes: promauto.With(reg).NewCounter(prometheus.CounterOpts{ | ||
| Name: "cortex_mimir_query_engine_step_invariant_nodes_total", | ||
| Help: "Total number of step invariant nodes.", | ||
| }), | ||
| points: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "cortex_mimir_query_engine_step_invariant_points_saved_total", | ||
| Help: "Total number of samples which were saved from being queried / loaded due to step invariant handling.", | ||
| }, []string{"type"}), | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
pkg/streamingpromql/operators/metrics/operator_metrics_tracker_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // SPDX-License-Identifier: AGPL-3.0-only | ||
|
|
||
| package operatormetrics | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/testutil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNewStepInvariantExpressionMetricsTrackerNodes(t *testing.T) { | ||
| tracker := NewOperatorMetricsTracker(prometheus.NewRegistry()) | ||
| require.Same(t, tracker.StepInvariantTracker.nodes, tracker.StepInvariantTracker.NodeCounter()) | ||
|
|
||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.nodes)) | ||
| tracker.StepInvariantTracker.OnStepInvariantNodeObserved() | ||
| require.Equal(t, float64(1), testutil.ToFloat64(tracker.StepInvariantTracker.nodes)) | ||
| } | ||
|
|
||
| func TestNewStepInvariantExpressionMetricsTrackerFloatPoints(t *testing.T) { | ||
| tracker := NewOperatorMetricsTracker(prometheus.NewRegistry()) | ||
|
|
||
| // Passing in a step count <= 1 has no effect | ||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(FPoint))) | ||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(FPoint, 0) | ||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(FPoint))) | ||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(FPoint, 1) | ||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(FPoint))) | ||
|
|
||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(FPoint, 2) | ||
| require.Equal(t, float64(1), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(FPoint))) | ||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(FPoint, 12) | ||
| require.Equal(t, float64(1+11), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(FPoint))) | ||
| } | ||
|
|
||
| func TestNewStepInvariantExpressionMetricsTrackerHistogramPoints(t *testing.T) { | ||
| tracker := NewOperatorMetricsTracker(prometheus.NewRegistry()) | ||
|
|
||
| // Passing in a step count <= 1 has no effect | ||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(HPoint))) | ||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(HPoint, 0) | ||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(HPoint))) | ||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(HPoint, 1) | ||
| require.Equal(t, float64(0), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(HPoint))) | ||
|
|
||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(HPoint, 2) | ||
| require.Equal(t, float64(1), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(HPoint))) | ||
| tracker.StepInvariantTracker.OnStepInvariantStepsSaved(HPoint, 12) | ||
| require.Equal(t, float64(1+11), testutil.ToFloat64(tracker.StepInvariantTracker.Counter(HPoint))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should track only the number of times a step-invariant node is added as well as the number of steps (not samples) saved.
This would simplify things greatly as we could calculate these at planning time, rather than during evaluation.