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

Open
wants to merge 1 commit into
base: lamida/mqe-delta-function
Choose a base branch
from

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.

Comment on lines +284 to +285
func delta() RangeVectorStepFunction {
return func(step *types.RangeVectorStepData, rangeSeconds float64, emitAnnotation types.EmitAnnotationFunc) (float64, bool, *histogram.FloatHistogram, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be simplified to this - there's no need for the wrapper function given we don't need different functionality like we do for rate and increase:

Suggested change
func delta() RangeVectorStepFunction {
return func(step *types.RangeVectorStepData, rangeSeconds float64, emitAnnotation types.EmitAnnotationFunc) (float64, bool, *histogram.FloatHistogram, error) {
func delta(step *types.RangeVectorStepData, rangeSeconds float64, emitAnnotation types.EmitAnnotationFunc) (float64, bool, *histogram.FloatHistogram, error) {


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.

Comment on lines +348 to +351
desiredSchema := firstPoint.H.Schema
if lastPoint.H.Schema < desiredSchema {
desiredSchema = lastPoint.H.Schema
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] This could be simplified to:

Suggested change
desiredSchema := firstPoint.H.Schema
if lastPoint.H.Schema < desiredSchema {
desiredSchema = lastPoint.H.Schema
}
desiredSchema := min(firstPoint.H.Schema, lastPoint.H.Schema)

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)

Comment on lines +353 to +354
usingCustomBuckets := firstPoint.H.UsesCustomBuckets()
if lastPoint.H.UsesCustomBuckets() != usingCustomBuckets {
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] This could be simplified to:

Suggested change
usingCustomBuckets := firstPoint.H.UsesCustomBuckets()
if lastPoint.H.UsesCustomBuckets() != usingCustomBuckets {
if firstPoint.H.UsesCustomBuckets() != lastPoint.H.UsesCustomBuckets() {

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.

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.

}

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?

hCount := len(hHead) + len(hTail)

if fCount > 0 && hCount > 0 {
// We need either at least two histograms and no floats, or at least two floats and no histograms to calculate a rate.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// We need either at least two histograms and no floats, or at least two floats and no histograms to calculate a rate.
// We need either at least two histograms and no floats, or at least two floats and no histograms to calculate a delta.

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