Skip to content
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

Extract delta from rate function #10353

Merged
merged 13 commits into from
Jan 9, 2025

Conversation

lamida
Copy link
Contributor

@lamida lamida commented Jan 6, 2025

This PR is trying to address suggestion in #9795 to extract delta function from rate. The diff will show how we have many more line additions if we extract the logic from rate.

How this different with #9795

  • We created a new step function called delta
  • Delta has no reset handling
  • Delta will call calculateFloatRate and calculateHistogramRate to do extrapolation

My concern with extracting delta from rate is we create many line duplications, such as most of lines in delta(), floatDelta() and histogramDelta(). This is reflected from 118 line additions and 36 line removal in this PR diffs.

What this PR does

Which issue(s) this PR fixes or relates to

Fixes #

Checklist

  • Tests updated.
  • Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • about-versioning.md updated with experimental features.

Signed-off-by: Jon Kartago Lamida <[email protected]>
@lamida lamida requested a review from a team as a code owner January 6, 2025 13:38
Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

I'm OK with the extra duplication this introduces - I think this makes the logic of delta much clearer.

pkg/streamingpromql/operators/functions/rate_increase.go Outdated Show resolved Hide resolved

func floatDelta(fCount int, fHead []promql.FPoint, fTail []promql.FPoint, rangeStart int64, rangeEnd int64, rangeSeconds float64) float64 {
firstPoint := fHead[0]
fHead = fHead[1:]
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] We don't need to do this for delta.


func histogramDelta(hCount int, hHead []promql.HPoint, hTail []promql.HPoint, rangeStart int64, rangeEnd int64, rangeSeconds float64, emitAnnotation types.EmitAnnotationFunc) (*histogram.FloatHistogram, error) {
firstPoint := hHead[0]
hHead = hHead[1:]
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] We don't need to do this for delta.

pkg/streamingpromql/operators/functions/rate_increase.go Outdated Show resolved Hide resolved
Comment on lines 344 to 346
if firstPoint.H.CounterResetHint == histogram.GaugeType || lastPoint.H.CounterResetHint == histogram.GaugeType {
emitAnnotation(annotations.NewNativeHistogramNotCounterWarning)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this correct? We emit a "not a gauge" warning further down if either point is not a gauge.

(we might need to add more test cases to TestAnnotations in engine_test.go)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should apply only for rate and increase. Removed in 7ba501f

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added one test case in 56f67de. Still figuring out how to set CounterResetHint from promql test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found the way to set reset hint by setting something like counter_reset_hint:gauge. Added the test in #9795 in commit 4b170e7

pkg/streamingpromql/operators/functions/rate_increase.go Outdated Show resolved Hide resolved
Comment on lines 367 to 369
if delta.Schema != desiredSchema {
delta = delta.CopyToSchema(desiredSchema)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is necessary - the Sub and CopyToSchema calls above should cover this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed in 175a67f

Comment on lines 358 to 359
delta := lastPoint.H.CopyToSchema(desiredSchema)
_, err := delta.Sub(firstPoint.H)
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't we do this? Sub will automatically adjust the schema if necessary.

Suggested change
delta := lastPoint.H.CopyToSchema(desiredSchema)
_, err := delta.Sub(firstPoint.H)
delta, err := lastPoint.H.Copy().Sub(firstPoint.H)

If so, then we don't need desiredSchema at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 9c6474a

}

if fCount >= 2 {
// TODO: just pass step here? (and below)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this TODO still relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

pkg/streamingpromql/operators/functions/rate_increase.go Outdated Show resolved Hide resolved
@lamida
Copy link
Contributor Author

lamida commented Jan 9, 2025

@charleskorn I have addressed almost all of your suggestion. I will merge this to #9795 and we can continue the discussion there.

@lamida lamida merged commit 9d21d80 into lamida/mqe-delta-function Jan 9, 2025
28 checks passed
@lamida lamida deleted the lamida/mqe-delta-extract-rate branch January 9, 2025 16:30
lamida added a commit that referenced this pull request Jan 9, 2025
* Extract delta from rate function

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Tidy up after applying PR suggestion

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove unnecessary head subslice

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove wrong placed annotation

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove extra copySchema

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Simplify native histogram sub schema

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove TODO

Signed-off-by: Jon Kartago Lamida <[email protected]>

* The lastPoint should be last index of the head

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Add delta counterResetHint test

Signed-off-by: Jon Kartago Lamida <[email protected]>

---------

Signed-off-by: Jon Kartago Lamida <[email protected]>
Co-authored-by: Charles Korn <[email protected]>
lamida added a commit that referenced this pull request Jan 10, 2025
* Extract delta from rate function

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Update pkg/streamingpromql/operators/functions/rate_increase.go

Co-authored-by: Charles Korn <[email protected]>

* Tidy up after applying PR suggestion

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove unnecessary head subslice

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove wrong placed annotation

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove extra copySchema

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Simplify native histogram sub schema

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Remove TODO

Signed-off-by: Jon Kartago Lamida <[email protected]>

* The lastPoint should be last index of the head

Signed-off-by: Jon Kartago Lamida <[email protected]>

* Add delta counterResetHint test

Signed-off-by: Jon Kartago Lamida <[email protected]>

---------

Signed-off-by: Jon Kartago Lamida <[email protected]>
Co-authored-by: Charles Korn <[email protected]>
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.

2 participants