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

MQE time related functions #10486

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ecff315
Implement time functions
lamida Jan 6, 2025
7389d10
Add more times function
lamida Jan 6, 2025
a71e555
Try restructuring the functions
lamida Jan 16, 2025
b9b6d83
Implement time related functions
lamida Jan 20, 2025
59b2039
Remove unused field
lamida Jan 21, 2025
001b929
Remove commented import
lamida Jan 21, 2025
ac31177
Add comment for readability
lamida Jan 21, 2025
91989e2
Missed to uncomment test
lamida Jan 21, 2025
9de48b5
Add missing license header
lamida Jan 21, 2025
5ebc1ff
Refactor the time function name
lamida Jan 22, 2025
eeafd62
Missed function to uncomment
lamida Jan 22, 2025
0e1f3a7
Split the file between times function and time operator
lamida Jan 22, 2025
9b4361e
Push new times function
lamida Jan 22, 2025
55e1998
Update pkg/streamingpromql/functions.go
lamida Jan 23, 2025
b0d0a7d
Move ok declaration inside if
lamida Jan 27, 2025
2aa5ed6
Remove unneeded block
lamida Jan 27, 2025
05bd286
Return histogram slice to pool in defer
lamida Jan 27, 2025
a75f93b
Fix unused parameter
lamida Jan 27, 2025
aa4d1f2
Add range query test for times function
lamida Jan 27, 2025
2185f84
Add more time tests
lamida Jan 27, 2025
83024da
Update pkg/streamingpromql/testdata/ours/functions.test
lamida Jan 28, 2025
6e42b66
Update pkg/streamingpromql/testdata/ours/functions.test
lamida Jan 28, 2025
2510116
Update pkg/streamingpromql/testdata/ours/functions.test
lamida Jan 28, 2025
16843e7
Inline TimeFunctionOperatorFactory
lamida Jan 28, 2025
dbbba00
Fix the test
lamida Jan 28, 2025
40cb57a
Return histogram to the poll
lamida Jan 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/streamingpromql/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func TimeFunctionOperatorFactory(name string, f functions.FunctionOverInstantVec
return func(args []types.Operator, memoryConsumptionTracker *limiting.MemoryConsumptionTracker, _ *annotations.Annotations, expressionPosition posrange.PositionRange, timeRange types.QueryTimeRange) (types.InstantVectorOperator, error) {
if len(args) > 1 {
// Should be caught by the PromQL parser, but we check here for safety.
return nil, fmt.Errorf("expected 1 or 0 argument for %s, got %v", name, len(args))
return nil, fmt.Errorf("expected 0 or 1 argument for %s, got %v", name, len(args))
}

var inner types.InstantVectorOperator
var ok bool
if len(args) > 0 {
var ok bool
// time based function expect instant vector argument
inner, ok = args[0].(types.InstantVectorOperator)
if !ok {
Expand Down
13 changes: 2 additions & 11 deletions pkg/streamingpromql/operators/functions/times.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package functions
import (
"time"

"github.com/prometheus/prometheus/promql"

"github.com/grafana/mimir/pkg/streamingpromql/limiting"
"github.com/grafana/mimir/pkg/streamingpromql/types"
)
Expand Down Expand Up @@ -44,14 +42,8 @@ var Year = timeWrapperFunc(func(t time.Time) float64 {
})

func timeWrapperFunc(f func(t time.Time) float64) InstantVectorSeriesFunction {
return func(seriesData types.InstantVectorSeriesData, _ []types.ScalarData, tr types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error) {
if len(seriesData.Floats)+len(seriesData.Histograms) == 0 {
fp := promql.FPoint{
F: f(time.Unix(tr.StartT, 0).UTC()),
}
seriesData.Floats = append(seriesData.Floats, fp)
return seriesData, nil
}
return func(seriesData types.InstantVectorSeriesData, _ []types.ScalarData, _ types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error) {
defer types.HPointSlicePool.Put(seriesData.Histograms, memoryConsumptionTracker)

if len(seriesData.Floats) > 0 {
for i := range seriesData.Floats {
Expand All @@ -62,7 +54,6 @@ func timeWrapperFunc(f func(t time.Time) float64) InstantVectorSeriesFunction {
}

// we don't do time based function on histograms
types.HPointSlicePool.Put(seriesData.Histograms, memoryConsumptionTracker)
seriesData.Histograms = nil
charleskorn marked this conversation as resolved.
Show resolved Hide resolved
return seriesData, nil
}
Expand Down
140 changes: 140 additions & 0 deletions pkg/streamingpromql/testdata/ours/functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,143 @@ eval range from 0 to 7m step 1m last_over_time(some_metric_count[3m])
some_metric_count{env="prod", cluster="eu"} _ _ _ 0 1 2 3 4
some_metric_count{env="prod", cluster="us"} _ _ _ 0 2 4 6 8
some_metric_count{env="prod", cluster="au"} _ _ _ {{count:5}} {{count:10}} {{count:15}} {{count:20}} {{count:25}}

# Test time-related functions.
load 5m
histogram_sample {{schema:0 sum:1 count:1}}

eval range from 0 to 7m step 1m year()
{} 1970 1970 1970 1970 1970 1970 1970 1970

eval range from 0 to 7m step 1m time()
{} 0 60 120 180 240 300 360 420

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m year(vector(1136239445))
{} 2006 2006 2006 2006 2006 2006 2006 2006

eval range from 0 to 7m step 1m month()
{} 1 1 1 1 1 1 1 1

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m month(vector(1136239445))
{} 1 1 1 1 1 1 1 1

eval range from 0 to 7m step 1m day_of_month()
{} 1 1 1 1 1 1 1 1

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m day_of_month(vector(1136239445))
{} 2 2 2 2 2 2 2 2

eval range from 0 to 7m step 1m day_of_year()
{} 1 1 1 1 1 1 1 1

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m day_of_year(vector(1136239445))
{} 2 2 2 2 2 2 2 2

# Thursday.
eval range from 0 to 7m step 1m day_of_week()
{} 4 4 4 4 4 4 4 4

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m day_of_week(vector(1136239445))
{} 1 1 1 1 1 1 1 1

eval range from 0 to 7m step 1m hour()
{} 0 0 0 0 0 0 0 0

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m hour(vector(1136239445))
{} 22 22 22 22 22 22 22 22

eval range from 0 to 7m step 1m minute()
{} 0 1 2 3 4 5 6 7

# 2006-01-02 22:04:05
eval range from 0 to 7m step 1m minute(vector(1136239445))
{} 4 4 4 4 4 4 4 4

# 2008-12-31 23:59:59 just before leap second.
eval range from 0 to 7m step 1m year(vector(1230767999))
{} 2008 2008 2008 2008 2008 2008 2008 2008

# 2009-01-01 00:00:00 just after leap second.
eval range from 0 to 7m step 1m year(vector(1230768000))
{} 2009 2009 2009 2009 2009 2009 2009 2009

# 2016-02-29 23:59:59 February 29th in leap year.
eval range from 0 to 7m step 1m month(vector(1456790399)) + day_of_month(vector(1456790399)) / 100
{} 2.29 2.29 2.29 2.29 2.29 2.29 2.29 2.29

# 2016-03-01 00:00:00 March 1st in leap year.
eval range from 0 to 7m step 1m month(vector(1456790400)) + day_of_month(vector(1456790400)) / 100
{} 3.01 3.01 3.01 3.01 3.01 3.01 3.01 3.01

# 2016-12-31 13:37:00 366th day in leap year.
eval range from 0 to 7m step 1m day_of_year(vector(1483191420))
{} 366 366 366 366 366 366 366 366

# 2022-12-31 13:37:00 365th day in non-leap year.
eval range from 0 to 7m step 1m day_of_year(vector(1672493820))
{} 365 365 365 365 365 365 365 365

# February 1st 2016 in leap year.
eval range from 0 to 7m step 1m days_in_month(vector(1454284800))
{} 29 29 29 29 29 29 29 29

# February 1st 2017 not in leap year.
eval range from 0 to 7m step 1m days_in_month(vector(1485907200))
{} 28 28 28 28 28 28 28 28

# Test for histograms.
eval range from 0 to 7m step 1m day_of_month(histogram_sample)

eval range from 0 to 7m step 1m day_of_week(histogram_sample)

eval range from 0 to 7m step 1m day_of_year(histogram_sample)

eval range from 0 to 7m step 1m days_in_month(histogram_sample)

eval range from 0 to 7m step 1m hour(histogram_sample)

eval range from 0 to 7m step 1m minute(histogram_sample)
lamida marked this conversation as resolved.
Show resolved Hide resolved

eval range from 0 to 7m step 1m month(histogram_sample)
lamida marked this conversation as resolved.
Show resolved Hide resolved

eval range from 0 to 7m step 1m year(histogram_sample)

clear

# Test time with custom metric
# 1136239445: 2006-01-02 22:04:05
# 1485907200: February 1st 2017 not in leap year.
# 1672493820: 2022-12-31 13:37:00 365th day in non-leap year.
load 1m
some_metric{case="series1"} 0+1136239445x7
another_metric{case="series1"} 0+536254375x7
some_metric{case="series2"} 0+1672493820x7
some_metric{case="gap1"} _ 1136239445 _ 1485907200 _ 1672493820
some_metric{case="gap2"} 1136239445 _ _ 1485907200 _ _ 1672493820
some_metric{case="histogram-gap"} _ {{count:5}} _ {{count:5}}
some_metric{case="histogram-float"} 1136239445x7 {{count:5}} 1485907200 {{count:5}}
lamida marked this conversation as resolved.
Show resolved Hide resolved

eval range from 0 to 7m step 1m year(some_metric)
{case="gap1"} _ 2006 2006 2017 2017 2022 2022 2022
{case="gap2"} 2006 2006 2006 2017 2017 2017 2022 2022
{case="histogram-float"} 2006 2006 2006 2006 2006 2006 2006 2006
{case="series1"} 1970 2006 2042 2078 2114 2150 2186 2222
{case="series2"} 1970 2022 2075 2128 2181 2234 2287 2340

eval range from 0 to 7m step 1m month(some_metric)
{case="gap1"} _ 1 1 2 2 12 12 12
{case="gap2"} 1 1 1 2 2 2 12 12
{case="histogram-float"} 1 1 1 1 1 1 1 1
{case="series1"} 1 1 1 1 1 1 1 1
{case="series2"} 1 12 12 12 12 12 12 12

eval range from 0 to 7m step 1m year(some_metric{case="series1"} + another_metric{case="series1"})
{case="series1"} 1970 2022 2075 2128 2181 2234 2287 2340

clear
Loading