functions

package
v0.0.0-...-a0fb2f5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2025 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AvgOverTime = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction:         DropSeriesName,
	StepFunc:                       avgOverTime,
	NeedsSeriesNamesForAnnotations: true,
}
View Source
var Changes = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               resetsChanges(false),
}
View Source
var CountOverTime = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               countOverTime,
}
View Source
var Deg = FloatTransformationDropHistogramsFunc(func(f float64) float64 {
	return f * 180 / math.Pi
})
View Source
var Deriv = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction:         DropSeriesName,
	StepFunc:                       deriv,
	NeedsSeriesNamesForAnnotations: true,
}
View Source
var DropSeriesName = SeriesMetadataFunctionDefinition{
	Func: func(seriesMetadata []types.SeriesMetadata, _ *limiting.MemoryConsumptionTracker) ([]types.SeriesMetadata, error) {
		for i := range seriesMetadata {
			seriesMetadata[i].Labels = seriesMetadata[i].Labels.DropMetricName()
		}

		return seriesMetadata, nil
	},
	NeedsSeriesDeduplication: true,
}

DropSeriesName is a series metadata function that removes the __name__ label from all series.

View Source
var Idelta = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               irateIdelta(false),
}
View Source
var Increase = FunctionOverRangeVectorDefinition{
	StepFunc:                       rate(false),
	SeriesValidationFuncFactory:    rateSeriesValidator,
	SeriesMetadataFunction:         DropSeriesName,
	NeedsSeriesNamesForAnnotations: true,
}
View Source
var Irate = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               irateIdelta(true),
}
View Source
var LastOverTime = FunctionOverRangeVectorDefinition{

	StepFunc: lastOverTime,
}
View Source
var MaxOverTime = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               maxOverTime,
}
View Source
var MinOverTime = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               minOverTime,
}
View Source
var PresentOverTime = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               presentOverTime,
}
View Source
var Rad = FloatTransformationDropHistogramsFunc(func(f float64) float64 {
	return f * math.Pi / 180
})
View Source
var Rate = FunctionOverRangeVectorDefinition{
	StepFunc:                       rate(true),
	SeriesValidationFuncFactory:    rateSeriesValidator,
	SeriesMetadataFunction:         DropSeriesName,
	NeedsSeriesNamesForAnnotations: true,
}
View Source
var Resets = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction: DropSeriesName,
	StepFunc:               resetsChanges(true),
}
View Source
var Sgn = FloatTransformationDropHistogramsFunc(func(f float64) float64 {
	if f < 0 {
		return -1
	}

	if f > 0 {
		return 1
	}

	return f
})
View Source
var SumOverTime = FunctionOverRangeVectorDefinition{
	SeriesMetadataFunction:         DropSeriesName,
	StepFunc:                       sumOverTime,
	NeedsSeriesNamesForAnnotations: true,
}

Functions

func HistogramFraction

func HistogramFraction(seriesData types.InstantVectorSeriesData, scalarArgsData []types.ScalarData, timeRange types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error)

func NativeHistogramErrorToAnnotation

func NativeHistogramErrorToAnnotation(err error, emitAnnotation types.EmitAnnotationFunc) error

Types

type FunctionOverInstantVector

type FunctionOverInstantVector struct {
	// At the moment no instant-vector promql function takes more than one instant-vector
	// as an argument. We can assume this will always be the Inner operator and therefore
	// what we use for the SeriesMetadata.
	Inner types.InstantVectorOperator
	// Any scalar arguments will be read once and passed to Func.SeriesDataFunc.
	ScalarArgs               []types.ScalarOperator
	MemoryConsumptionTracker *limiting.MemoryConsumptionTracker
	Func                     FunctionOverInstantVectorDefinition
	// contains filtered or unexported fields
}

FunctionOverInstantVector performs a function over each series in an instant vector.

func NewFunctionOverInstantVector

func NewFunctionOverInstantVector(
	inner types.InstantVectorOperator,
	scalarArgs []types.ScalarOperator,
	memoryConsumptionTracker *limiting.MemoryConsumptionTracker,
	f FunctionOverInstantVectorDefinition,
	expressionPosition posrange.PositionRange,
	timeRange types.QueryTimeRange,
) *FunctionOverInstantVector

func (*FunctionOverInstantVector) Close

func (m *FunctionOverInstantVector) Close()

func (*FunctionOverInstantVector) ExpressionPosition

func (m *FunctionOverInstantVector) ExpressionPosition() posrange.PositionRange

func (*FunctionOverInstantVector) NextSeries

func (*FunctionOverInstantVector) SeriesMetadata

func (m *FunctionOverInstantVector) SeriesMetadata(ctx context.Context) ([]types.SeriesMetadata, error)

type FunctionOverInstantVectorDefinition

type FunctionOverInstantVectorDefinition struct {
	// SeriesDataFunc is the function that computes an output series for a single input series.
	SeriesDataFunc InstantVectorSeriesFunction

	// SeriesMetadataFunction is the function that computes the output series for this function based on the given input series.
	//
	// If SeriesMetadataFunction.Func is nil, the input series are used as-is.
	SeriesMetadataFunction SeriesMetadataFunctionDefinition
}

type FunctionOverRangeVector

type FunctionOverRangeVector struct {
	Inner                    types.RangeVectorOperator
	MemoryConsumptionTracker *limiting.MemoryConsumptionTracker
	Func                     FunctionOverRangeVectorDefinition

	Annotations *annotations.Annotations
	// contains filtered or unexported fields
}

FunctionOverRangeVector performs a rate calculation over a range vector.

func NewFunctionOverRangeVector

func NewFunctionOverRangeVector(
	inner types.RangeVectorOperator,
	memoryConsumptionTracker *limiting.MemoryConsumptionTracker,
	f FunctionOverRangeVectorDefinition,
	annotations *annotations.Annotations,
	expressionPosition posrange.PositionRange,
	timeRange types.QueryTimeRange,
) *FunctionOverRangeVector

func (*FunctionOverRangeVector) Close

func (m *FunctionOverRangeVector) Close()

func (*FunctionOverRangeVector) ExpressionPosition

func (m *FunctionOverRangeVector) ExpressionPosition() posrange.PositionRange

func (*FunctionOverRangeVector) NextSeries

func (*FunctionOverRangeVector) SeriesMetadata

func (m *FunctionOverRangeVector) SeriesMetadata(ctx context.Context) ([]types.SeriesMetadata, error)

type FunctionOverRangeVectorDefinition

type FunctionOverRangeVectorDefinition struct {
	// StepFunc is the function that computes an output sample for a single step.
	StepFunc RangeVectorStepFunction

	// SeriesValidationFuncFactory is the function that creates a validator for a complete series, emitting any annotations
	// for that series.
	//
	// A new validator instance is created for each instance of this range vector function (ie. a validator is not shared
	// between queries or between different invocations of the same range vector function in a single query). A single
	// instance of a range vector function can then implement optimisations such as skipping checks for repeated metric
	// names.
	//
	// SeriesValidationFuncFactory can be nil, in which case no validation is performed.
	SeriesValidationFuncFactory RangeVectorSeriesValidationFunctionFactory

	// SeriesMetadataFunction is the function that computes the output series for this function based on the given input series.
	//
	// If SeriesMetadataFunction.Func is nil, the input series are used as-is.
	SeriesMetadataFunction SeriesMetadataFunctionDefinition

	// NeedsSeriesNamesForAnnotations indicates that this function uses the names of input series when emitting annotations.
	NeedsSeriesNamesForAnnotations bool
}

type HistogramQuantileFunction

type HistogramQuantileFunction struct {
	// contains filtered or unexported fields
}

HistogramQuantileFunction performs a function over each series in an instant vector, with special handling for classic and native histograms. At the moment, it only supports histogram_quantile

func NewHistogramQuantileFunction

func NewHistogramQuantileFunction(
	phArg types.ScalarOperator,
	inner types.InstantVectorOperator,
	memoryConsumptionTracker *limiting.MemoryConsumptionTracker,
	annotations *annotations.Annotations,
	expressionPosition posrange.PositionRange,
	timeRange types.QueryTimeRange,
) *HistogramQuantileFunction

func (*HistogramQuantileFunction) Close

func (h *HistogramQuantileFunction) Close()

func (*HistogramQuantileFunction) ExpressionPosition

func (h *HistogramQuantileFunction) ExpressionPosition() posrange.PositionRange

func (*HistogramQuantileFunction) NextSeries

func (*HistogramQuantileFunction) SeriesMetadata

func (h *HistogramQuantileFunction) SeriesMetadata(ctx context.Context) ([]types.SeriesMetadata, error)

type InstantVectorSeriesFunction

type InstantVectorSeriesFunction func(seriesData types.InstantVectorSeriesData, scalarArgsData []types.ScalarData, timeRange types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error)

InstantVectorSeriesFunction is a function that takes in an instant vector and produces an instant vector.

var Clamp InstantVectorSeriesFunction = func(seriesData types.InstantVectorSeriesData, scalarArgsData []types.ScalarData, timeRange types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error) {
	outputIdx := 0
	minArg := scalarArgsData[0]
	maxArg := scalarArgsData[1]

	for _, data := range seriesData.Floats {

		idx := timeRange.PointIndex(data.T)
		minVal := minArg.Samples[idx].F
		maxVal := maxArg.Samples[idx].F

		if maxVal < minVal {

			continue
		}

		seriesData.Floats[outputIdx].T = data.T
		seriesData.Floats[outputIdx].F = max(minVal, min(maxVal, data.F))
		outputIdx++
	}
	seriesData.Floats = seriesData.Floats[:outputIdx]

	types.HPointSlicePool.Put(seriesData.Histograms, memoryConsumptionTracker)
	seriesData.Histograms = nil
	return seriesData, nil
}
var Round InstantVectorSeriesFunction = func(seriesData types.InstantVectorSeriesData, scalarArgsData []types.ScalarData, timeRange types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error) {
	toNearest := scalarArgsData[0]

	for step, data := range seriesData.Floats {

		idx := timeRange.PointIndex(data.T)

		toNearestInverse := 1.0 / toNearest.Samples[idx].F

		seriesData.Floats[step].F = math.Floor(data.F*toNearestInverse+0.5) / toNearestInverse
	}

	types.HPointSlicePool.Put(seriesData.Histograms, memoryConsumptionTracker)
	seriesData.Histograms = nil
	return seriesData, nil
}

round returns a number rounded to toNearest. Ties are solved by rounding up.

var UnaryNegation InstantVectorSeriesFunction = func(seriesData types.InstantVectorSeriesData, _ []types.ScalarData, _ types.QueryTimeRange, _ *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error) {
	for i := range seriesData.Floats {
		seriesData.Floats[i].F = -seriesData.Floats[i].F
	}

	for i := range seriesData.Histograms {
		seriesData.Histograms[i].H.Mul(-1)
	}

	return seriesData, nil
}

func ClampMinMaxFactory

func ClampMinMaxFactory(isMin bool) InstantVectorSeriesFunction

func FloatTransformationDropHistogramsFunc

func FloatTransformationDropHistogramsFunc(transform func(f float64) float64) InstantVectorSeriesFunction

func HistogramStdDevStdVar

func HistogramStdDevStdVar(isStdDev bool) InstantVectorSeriesFunction

HistogramStdDevStdVar returns either the standard deviation, or standard variance of a native histogram. Float values are ignored.

type RangeVectorSeriesValidationFunction

type RangeVectorSeriesValidationFunction func(seriesData types.InstantVectorSeriesData, metricName string, emitAnnotation types.EmitAnnotationFunc)

RangeVectorSeriesValidationFunction is a function that is called after a series is completed for a function over a range vector.

type RangeVectorSeriesValidationFunctionFactory

type RangeVectorSeriesValidationFunctionFactory func() RangeVectorSeriesValidationFunction

RangeVectorSeriesValidationFunctionFactory is a factory function that returns a RangeVectorSeriesValidationFunction

type RangeVectorStepFunction

type RangeVectorStepFunction func(
	step *types.RangeVectorStepData,
	rangeSeconds float64,
	emitAnnotation types.EmitAnnotationFunc,
) (f float64, hasFloat bool, h *histogram.FloatHistogram, err error)

RangeVectorStepFunction is a function that operates on a range vector step.

floatBuffer and histogramBuffer will contain the values for the step range. They may also contain values outside of the step, so step.RangeEnd needs to be considered.

Parameters:

  • step: the range vector step data to be processed.
  • rangeSeconds: the duration of the range in seconds.
  • floatBuffer: a ring buffer containing float values for the step range.
  • histogramBuffer: a ring buffer containing histogram values for the step range.
  • emitAnnotation: a callback function to emit an annotation for the current series.

Returns:

  • hasFloat bool: a boolean indicating if a float value is present.
  • f float64: the float value.
  • h *histogram.FloatHistogram: nil if no histogram is present.
  • err error.

type SeriesMetadataFunction

type SeriesMetadataFunction func(seriesMetadata []types.SeriesMetadata, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) ([]types.SeriesMetadata, error)

SeriesMetadataFunction is a function to operate on the metadata across series.

func LabelReplaceFactory

func LabelReplaceFactory(dstLabelOp, replacementOp, srcLabelOp, regexOp types.StringOperator) SeriesMetadataFunction

type SeriesMetadataFunctionDefinition

type SeriesMetadataFunctionDefinition struct {
	// Func is the function that computes the output series for this function based on the given input series.
	//
	// If Func is nil, the input series are used as-is.
	Func SeriesMetadataFunction

	// NeedsSeriesDeduplication enables deduplication and merging of output series with the same labels.
	//
	// This should be set to true if Func modifies the input series labels in such a way that duplicates may be
	// present in the output series labels (eg. dropping a label).
	NeedsSeriesDeduplication bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL