-
Notifications
You must be signed in to change notification settings - Fork 569
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
Mimir Query Engine: Add instant-vector functions #8256
Merged
Merged
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
a297de4
Mimir query engine: Add support for histogram_count function
jhesketh 6fd0e99
Support new limit pooling
jhesketh fb6b581
Test mixed data use cases
jhesketh 0752b29
Refactor function calling
jhesketh 3aa7014
Add support for histogram_sum
jhesketh 5b02813
Add in simple function
jhesketh 0fbd7b4
Update CHANGELOG
jhesketh 0565a58
Fix lint
jhesketh c2e4ae5
Merge branch 'main' into jhesketh/promql
jhesketh 6924f7c
Refactor functions into their own package
jhesketh c531127
Fix lint
jhesketh d41ac2d
Remove dropMetricName helper
jhesketh 715dab0
Put series back into pool after count+sum
jhesketh 0caf6e2
Rename function over types
jhesketh abc09ba
Add support for clamp function
jhesketh cf98d31
Move clamp into functions package
jhesketh 13afa3f
We support scalars now
jhesketh 2cef2c6
fix lint
jhesketh b9e9710
Re-arrange functions slightly
jhesketh 88e0705
Rename methods+vars for consistency
jhesketh 076a367
Fix native_histogram tests
jhesketh 2ad5cbe
Ignore histogram with float functions
jhesketh 827b2f7
Add tests for functions
jhesketh 8024f1d
Remove scalar operator for now (along with clamp function)
jhesketh 3c7c56b
Address review feedback
jhesketh d67f81f
Merge branch 'main' into jhesketh/promql
jhesketh 30a3197
Rename instantVectorFunctionOperatorFactories to instantVectorFunctio…
jhesketh 7da3fa5
Allow registering instantVectorFunctions
jhesketh 881b816
Address review feedback
jhesketh bfc40d2
Add TestRegisterInstantVectorFunctionOperator
jhesketh cf89455
Merge branch 'main' into jhesketh/promql
jhesketh 8f40b7d
Fix unit tests
jhesketh 75506d6
Make clearer what are factories
jhesketh 70791a7
Fix lint
jhesketh 361070c
Test extra case
jhesketh 2199afe
Enable trig functions
jhesketh f484280
Remove redundant tests
jhesketh 5ff8972
Fix doc strings
jhesketh 7b7f169
Make sure tests tidy themselves up
jhesketh b4f3234
Add check for NaN in acos
jhesketh 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 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 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,113 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package streamingpromql | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/grafana/mimir/pkg/streamingpromql/functions" | ||
"github.com/grafana/mimir/pkg/streamingpromql/operators" | ||
"github.com/grafana/mimir/pkg/streamingpromql/pooling" | ||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||
) | ||
|
||
type InstantVectorFunctionOperatorFactory func(args []types.Operator, pool *pooling.LimitingPool) (types.InstantVectorOperator, error) | ||
|
||
// SingleInputVectorFunctionOperator creates an InstantVectorFunctionOperator for functions | ||
// that have exactly 1 argument (v instant-vector). | ||
// | ||
// Parameters: | ||
// - name: The name of the function. | ||
// - metadataFunc: The function for handling metadata | ||
// - seriesDataFunc: The function to handle series data | ||
// | ||
// Returns: | ||
// | ||
// An InstantVectorFunctionOperator. | ||
jhesketh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func SingleInputVectorFunctionOperator(name string, metadataFunc functions.SeriesMetadataFunction, seriesDataFunc functions.InstantVectorFunction) InstantVectorFunctionOperatorFactory { | ||
return func(args []types.Operator, pool *pooling.LimitingPool) (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 exactly 1 argument for %s, got %v", name, len(args)) | ||
} | ||
|
||
inner, ok := args[0].(types.InstantVectorOperator) | ||
if !ok { | ||
// Should be caught by the PromQL parser, but we check here for safety. | ||
return nil, fmt.Errorf("expected an instant vector argument for %s, got %T", name, args[0]) | ||
} | ||
|
||
return &operators.FunctionOverInstantVector{ | ||
Inner: inner, | ||
Pool: pool, | ||
|
||
MetadataFunc: metadataFunc, | ||
SeriesDataFunc: seriesDataFunc, | ||
}, nil | ||
} | ||
} | ||
|
||
// TransformationFunctionOperator creates an InstantVectorFunctionOperator for functions | ||
// that have exactly 1 argument (v instant-vector), and drop the series __name__ label. | ||
// | ||
// Parameters: | ||
// - name: The name of the function. | ||
// - seriesDataFunc: The function to handle series data | ||
// | ||
// Returns: | ||
// | ||
// An InstantVectorFunctionOperator. | ||
func TransformationFunctionOperator(name string, seriesDataFunc functions.InstantVectorFunction) InstantVectorFunctionOperatorFactory { | ||
return SingleInputVectorFunctionOperator(name, functions.DropSeriesName, seriesDataFunc) | ||
} | ||
|
||
// LabelManipulationFunctionOperator creates an InstantVectorFunctionOperator for functions | ||
// that have exactly 1 argument (v instant-vector), and need to manipulate the labels of | ||
// each series without manipulating the returned samples. | ||
// The values of v are passed through. | ||
// | ||
// Parameters: | ||
// - name: The name of the function. | ||
// - metadataFunc: The function for handling metadata | ||
// | ||
// Returns: | ||
// | ||
// An InstantVectorFunctionOperator. | ||
func LabelManipulationFunctionOperator(name string, metadataFunc functions.SeriesMetadataFunction) InstantVectorFunctionOperatorFactory { | ||
return SingleInputVectorFunctionOperator(name, metadataFunc, functions.Passthrough) | ||
} | ||
|
||
func rateFunctionOperator(args []types.Operator, pool *pooling.LimitingPool) (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 exactly 1 argument for rate, got %v", len(args)) | ||
} | ||
|
||
inner, ok := args[0].(types.RangeVectorOperator) | ||
if !ok { | ||
// Should be caught by the PromQL parser, but we check here for safety. | ||
return nil, fmt.Errorf("expected a range vector argument for rate, got %T", args[0]) | ||
} | ||
|
||
return &operators.FunctionOverRangeVector{ | ||
Inner: inner, | ||
Pool: pool, | ||
}, nil | ||
} | ||
|
||
// These functions return an instant-vector. | ||
var instantVectorFunctionOperatorFactories = map[string]InstantVectorFunctionOperatorFactory{ | ||
"acos": TransformationFunctionOperator("acos", functions.Acos), | ||
"histogram_count": TransformationFunctionOperator("histogram_count", functions.HistogramCount), | ||
"histogram_sum": TransformationFunctionOperator("histogram_sum", functions.HistogramSum), | ||
"rate": rateFunctionOperator, | ||
} | ||
|
||
func RegisterInstantVectorFunctionOperator(functionName string, functionOperator InstantVectorFunctionOperatorFactory) error { | ||
if _, exists := instantVectorFunctionOperatorFactories[functionName]; exists { | ||
return fmt.Errorf("function '%s' has already been registered", functionName) | ||
} | ||
|
||
instantVectorFunctionOperatorFactories[functionName] = functionOperator | ||
return nil | ||
} |
This file contains 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,45 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package functions | ||
|
||
import ( | ||
"github.com/grafana/mimir/pkg/streamingpromql/pooling" | ||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||
) | ||
|
||
type SeriesMetadataFunction func(seriesMetadata []types.SeriesMetadata, pool *pooling.LimitingPool) ([]types.SeriesMetadata, error) | ||
|
||
func DropSeriesName(seriesMetadata []types.SeriesMetadata, _ *pooling.LimitingPool) ([]types.SeriesMetadata, error) { | ||
for i := range seriesMetadata { | ||
seriesMetadata[i].Labels = seriesMetadata[i].Labels.DropMetricName() | ||
} | ||
|
||
return seriesMetadata, nil | ||
} | ||
|
||
type InstantVectorFunction func(seriesData types.InstantVectorSeriesData, pool *pooling.LimitingPool) (types.InstantVectorSeriesData, error) | ||
|
||
// floatTransformationFunc is not needed elsewhere, so it is not exported yet | ||
func floatTransformationFunc(transform func(f float64) float64) InstantVectorFunction { | ||
return func(seriesData types.InstantVectorSeriesData, pool *pooling.LimitingPool) (types.InstantVectorSeriesData, error) { | ||
for i := range seriesData.Floats { | ||
seriesData.Floats[i].F = transform(seriesData.Floats[i].F) | ||
} | ||
return seriesData, nil | ||
} | ||
} | ||
|
||
func FloatTransformationDropHistogramsFunc(transform func(f float64) float64) InstantVectorFunction { | ||
ft := floatTransformationFunc(transform) | ||
return func(seriesData types.InstantVectorSeriesData, pool *pooling.LimitingPool) (types.InstantVectorSeriesData, error) { | ||
// Functions that do not explicitly mention native histograms in their documentation will ignore histogram samples. | ||
// https://prometheus.io/docs/prometheus/latest/querying/functions | ||
pool.PutHPointSlice(seriesData.Histograms) | ||
seriesData.Histograms = nil | ||
return ft(seriesData, pool) | ||
} | ||
} | ||
|
||
func Passthrough(seriesData types.InstantVectorSeriesData, _ *pooling.LimitingPool) (types.InstantVectorSeriesData, error) { | ||
return seriesData, nil | ||
} |
This file contains 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,87 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package functions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/model/histogram" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/mimir/pkg/streamingpromql/pooling" | ||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||
) | ||
|
||
func TestDropSeriesName(t *testing.T) { | ||
seriesMetadata := []types.SeriesMetadata{ | ||
{Labels: labels.FromStrings("__name__", "metric_name", "label1", "value1")}, | ||
{Labels: labels.FromStrings("__name__", "another_metric", "label2", "value2")}, | ||
} | ||
|
||
expected := []types.SeriesMetadata{ | ||
{Labels: labels.FromStrings("label1", "value1")}, | ||
{Labels: labels.FromStrings("label2", "value2")}, | ||
} | ||
|
||
modifiedMetadata, err := DropSeriesName(seriesMetadata, pooling.NewLimitingPool(0, nil)) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, modifiedMetadata) | ||
} | ||
|
||
func TestFloatTransformationFunc(t *testing.T) { | ||
transform := func(f float64) float64 { return f * 2 } | ||
transformFunc := floatTransformationFunc(transform) | ||
|
||
seriesData := types.InstantVectorSeriesData{ | ||
Floats: []promql.FPoint{ | ||
{F: 1.0}, | ||
{F: 2.5}, | ||
}, | ||
Histograms: []promql.HPoint{ | ||
{H: &histogram.FloatHistogram{Count: 1, Sum: 2}}, | ||
}, | ||
} | ||
|
||
expected := types.InstantVectorSeriesData{ | ||
Floats: []promql.FPoint{ | ||
{F: 2.0}, | ||
{F: 5.0}, | ||
}, | ||
Histograms: []promql.HPoint{ | ||
{H: &histogram.FloatHistogram{Count: 1, Sum: 2}}, | ||
}, | ||
} | ||
|
||
modifiedSeriesData, err := transformFunc(seriesData, pooling.NewLimitingPool(0, nil)) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, modifiedSeriesData) | ||
} | ||
|
||
func TestFloatTransformationDropHistogramsFunc(t *testing.T) { | ||
transform := func(f float64) float64 { return f * 2 } | ||
transformFunc := FloatTransformationDropHistogramsFunc(transform) | ||
|
||
seriesData := types.InstantVectorSeriesData{ | ||
Floats: []promql.FPoint{ | ||
{F: 1.0}, | ||
{F: 2.5}, | ||
}, | ||
Histograms: []promql.HPoint{ | ||
{H: &histogram.FloatHistogram{Count: 1, Sum: 2}}, | ||
}, | ||
} | ||
|
||
expected := types.InstantVectorSeriesData{ | ||
Floats: []promql.FPoint{ | ||
{F: 2.0}, | ||
{F: 5.0}, | ||
}, | ||
Histograms: nil, // Histograms should be dropped | ||
} | ||
|
||
modifiedSeriesData, err := transformFunc(seriesData, pooling.NewLimitingPool(0, nil)) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, modifiedSeriesData) | ||
} |
This file contains 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,9 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package functions | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
var Acos = FloatTransformationDropHistogramsFunc(math.Acos) |
jhesketh marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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,43 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package functions | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/model/histogram" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/mimir/pkg/streamingpromql/pooling" | ||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||
) | ||
|
||
func TestAcos(t *testing.T) { | ||
pool := pooling.NewLimitingPool(0, nil) | ||
|
||
seriesData := types.InstantVectorSeriesData{ | ||
Floats: []promql.FPoint{ | ||
{F: 1.0}, | ||
{F: 0.5}, | ||
{F: 0.0}, | ||
}, | ||
Histograms: []promql.HPoint{ | ||
{H: &histogram.FloatHistogram{Count: 1, Sum: 2}}, | ||
}, | ||
} | ||
|
||
expected := types.InstantVectorSeriesData{ | ||
Floats: []promql.FPoint{ | ||
{F: math.Acos(1.0)}, | ||
{F: math.Acos(0.5)}, | ||
{F: math.Acos(0.0)}, | ||
}, | ||
Histograms: nil, // Histograms should be dropped | ||
} | ||
|
||
modifiedSeriesData, err := Acos(seriesData, pool) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, modifiedSeriesData) | ||
} |
This file contains 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,48 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package functions | ||
|
||
import ( | ||
"github.com/prometheus/prometheus/promql" | ||
|
||
"github.com/grafana/mimir/pkg/streamingpromql/pooling" | ||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||
) | ||
|
||
func HistogramCount(seriesData types.InstantVectorSeriesData, pool *pooling.LimitingPool) (types.InstantVectorSeriesData, error) { | ||
floats, err := pool.GetFPointSlice(len(seriesData.Histograms)) | ||
if err != nil { | ||
return types.InstantVectorSeriesData{}, err | ||
} | ||
|
||
data := types.InstantVectorSeriesData{ | ||
Floats: floats, | ||
} | ||
for _, histogram := range seriesData.Histograms { | ||
data.Floats = append(data.Floats, promql.FPoint{ | ||
T: histogram.T, | ||
F: histogram.H.Count, | ||
}) | ||
} | ||
pool.PutInstantVectorSeriesData(seriesData) | ||
return data, nil | ||
} | ||
|
||
func HistogramSum(seriesData types.InstantVectorSeriesData, pool *pooling.LimitingPool) (types.InstantVectorSeriesData, error) { | ||
floats, err := pool.GetFPointSlice(len(seriesData.Histograms)) | ||
if err != nil { | ||
return types.InstantVectorSeriesData{}, err | ||
} | ||
|
||
data := types.InstantVectorSeriesData{ | ||
Floats: floats, | ||
} | ||
for _, histogram := range seriesData.Histograms { | ||
data.Floats = append(data.Floats, promql.FPoint{ | ||
T: histogram.T, | ||
F: histogram.H.Sum, | ||
}) | ||
} | ||
pool.PutInstantVectorSeriesData(seriesData) | ||
return data, nil | ||
} |
Oops, something went wrong.
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.
[nit] Would be good to keep the PRs sorted
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.
Depends if you want them sorted by order merged, or order opened. I'd argue for merged since this branch merges main back into itself and will be squashed and applied on top of what was 8340.