Apply struct alignment to promql package

Apply struct alignment with betteralign to the promql packages.

Signed-off-by: SuperQ <superq@gmail.com>
This commit is contained in:
SuperQ 2024-07-18 14:29:33 +02:00
parent a60e5ce362
commit 6043298a9f
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1
9 changed files with 444 additions and 434 deletions

View file

@ -188,13 +188,13 @@ type query struct {
stats *stats.QueryTimers stats *stats.QueryTimers
// Sample stats for the query execution. // Sample stats for the query execution.
sampleStats *stats.QuerySamples sampleStats *stats.QuerySamples
// Result matrix for reuse.
matrix Matrix
// Cancellation function for the query. // Cancellation function for the query.
cancel func() cancel func()
// The engine against which the query is executed. // The engine against which the query is executed.
ng *Engine ng *Engine
// Result matrix for reuse.
matrix Matrix
} }
type QueryOrigin struct{} type QueryOrigin struct{}
@ -287,17 +287,18 @@ type QueryTracker interface {
type EngineOpts struct { type EngineOpts struct {
Logger log.Logger Logger log.Logger
Reg prometheus.Registerer Reg prometheus.Registerer
MaxSamples int
Timeout time.Duration
ActiveQueryTracker QueryTracker ActiveQueryTracker QueryTracker
// LookbackDelta determines the time since the last sample after which a time
// series is considered stale.
LookbackDelta time.Duration
// NoStepSubqueryIntervalFn is the default evaluation interval of // NoStepSubqueryIntervalFn is the default evaluation interval of
// a subquery in milliseconds if no step in range vector was specified `[30m:<step>]`. // a subquery in milliseconds if no step in range vector was specified `[30m:<step>]`.
NoStepSubqueryIntervalFn func(rangeMillis int64) int64 NoStepSubqueryIntervalFn func(rangeMillis int64) int64
MaxSamples int
Timeout time.Duration
// LookbackDelta determines the time since the last sample after which a time
// series is considered stale.
LookbackDelta time.Duration
// EnableAtModifier if true enables @ modifier. Disabled otherwise. This // EnableAtModifier if true enables @ modifier. Disabled otherwise. This
// is supposed to be enabled for regular PromQL (as of Prometheus v2.33) // is supposed to be enabled for regular PromQL (as of Prometheus v2.33)
// but the option to disable it is still provided here for those using // but the option to disable it is still provided here for those using
@ -319,14 +320,14 @@ type EngineOpts struct {
// It is connected to a querier. // It is connected to a querier.
type Engine struct { type Engine struct {
logger log.Logger logger log.Logger
metrics *engineMetrics
timeout time.Duration
maxSamplesPerQuery int
activeQueryTracker QueryTracker activeQueryTracker QueryTracker
queryLogger QueryLogger queryLogger QueryLogger
queryLoggerLock sync.RWMutex metrics *engineMetrics
lookbackDelta time.Duration
noStepSubqueryIntervalFn func(rangeMillis int64) int64 noStepSubqueryIntervalFn func(rangeMillis int64) int64
timeout time.Duration
maxSamplesPerQuery int
lookbackDelta time.Duration
queryLoggerLock sync.RWMutex
enableAtModifier bool enableAtModifier bool
enableNegativeOffset bool enableNegativeOffset bool
enablePerStepStats bool enablePerStepStats bool
@ -1022,16 +1023,17 @@ func (e errWithWarnings) Error() string { return e.err.Error() }
type evaluator struct { type evaluator struct {
ctx context.Context ctx context.Context
logger log.Logger
samplesStats *stats.QuerySamples
noStepSubqueryIntervalFn func(rangeMillis int64) int64
startTimestamp int64 // Start time in milliseconds. startTimestamp int64 // Start time in milliseconds.
endTimestamp int64 // End time in milliseconds. endTimestamp int64 // End time in milliseconds.
interval int64 // Interval in milliseconds. interval int64 // Interval in milliseconds.
maxSamples int maxSamples int
currentSamples int currentSamples int
logger log.Logger
lookbackDelta time.Duration lookbackDelta time.Duration
samplesStats *stats.QuerySamples
noStepSubqueryIntervalFn func(rangeMillis int64) int64
} }
// errorf causes a panic with the input formatted into an error. // errorf causes a panic with the input formatted into an error.
@ -1084,23 +1086,24 @@ type EvalSeriesHelper struct {
// EvalNodeHelper stores extra information and caches for evaluating a single node across steps. // EvalNodeHelper stores extra information and caches for evaluating a single node across steps.
type EvalNodeHelper struct { type EvalNodeHelper struct {
// Evaluation timestamp.
Ts int64
// Vector that can be used for output.
Out Vector
// Caches. // Caches.
// funcHistogramQuantile for classic histograms. // funcHistogramQuantile for classic histograms.
signatureToMetricWithBuckets map[string]*metricWithBuckets signatureToMetricWithBuckets map[string]*metricWithBuckets
lb *labels.Builder lb *labels.Builder
lblBuf []byte
lblResultBuf []byte
// For binary vector matching. // For binary vector matching.
rightSigs map[string]Sample rightSigs map[string]Sample
matchedSigs map[string]map[uint64]struct{} matchedSigs map[string]map[uint64]struct{}
resultMetric map[string]labels.Labels resultMetric map[string]labels.Labels
// Vector that can be used for output.
Out Vector
lblBuf []byte
lblResultBuf []byte
// Evaluation timestamp.
Ts int64
} }
func (enh *EvalNodeHelper) resetBuilder(lbls labels.Labels) { func (enh *EvalNodeHelper) resetBuilder(lbls labels.Labels) {
@ -2773,15 +2776,15 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64, hlhs, hrhs *histogram
} }
type groupedAggregation struct { type groupedAggregation struct {
histogramValue *histogram.FloatHistogram
heap vectorByValueHeap
floatValue float64
floatMean float64 // Mean, or "compensating value" for Kahan summation.
groupCount int
seen bool // Was this output groups seen in the input at this timestamp. seen bool // Was this output groups seen in the input at this timestamp.
hasFloat bool // Has at least 1 float64 sample aggregated. hasFloat bool // Has at least 1 float64 sample aggregated.
hasHistogram bool // Has at least 1 histogram sample aggregated. hasHistogram bool // Has at least 1 histogram sample aggregated.
floatValue float64
histogramValue *histogram.FloatHistogram
floatMean float64 // Mean, or "compensating value" for Kahan summation.
groupCount int
groupAggrComplete bool // Used by LIMITK to short-cut series loop when we've reached K elem on every group groupAggrComplete bool // Used by LIMITK to short-cut series loop when we've reached K elem on every group
heap vectorByValueHeap
} }
// aggregation evaluates sum, avg, count, stdvar, stddev or quantile at one timestep on inputMatrix. // aggregation evaluates sum, avg, count, stdvar, stddev or quantile at one timestep on inputMatrix.

View file

@ -90,23 +90,24 @@ type Expressions []Expr
// AggregateExpr represents an aggregation operation on a Vector. // AggregateExpr represents an aggregation operation on a Vector.
type AggregateExpr struct { type AggregateExpr struct {
Op ItemType // The used aggregation operation.
Expr Expr // The Vector expression over which is aggregated. Expr Expr // The Vector expression over which is aggregated.
Param Expr // Parameter used by some aggregators. Param Expr // Parameter used by some aggregators.
Grouping []string // The labels by which to group the Vector. Grouping []string // The labels by which to group the Vector.
Without bool // Whether to drop the given labels rather than keep them.
PosRange posrange.PositionRange PosRange posrange.PositionRange
Op ItemType // The used aggregation operation.
Without bool // Whether to drop the given labels rather than keep them.
} }
// BinaryExpr represents a binary expression between two child expressions. // BinaryExpr represents a binary expression between two child expressions.
type BinaryExpr struct { type BinaryExpr struct {
Op ItemType // The operation of the expression.
LHS, RHS Expr // The operands on the respective sides of the operator. LHS, RHS Expr // The operands on the respective sides of the operator.
// The matching behavior for the operation if both operands are Vectors. // The matching behavior for the operation if both operands are Vectors.
// If they are not this field is nil. // If they are not this field is nil.
VectorMatching *VectorMatching VectorMatching *VectorMatching
Op ItemType // The operation of the expression.
// If a comparison operator, return 0/1 rather than filtering. // If a comparison operator, return 0/1 rather than filtering.
ReturnBool bool ReturnBool bool
} }
@ -132,6 +133,7 @@ type MatrixSelector struct {
// SubqueryExpr represents a subquery. // SubqueryExpr represents a subquery.
type SubqueryExpr struct { type SubqueryExpr struct {
Expr Expr Expr Expr
Timestamp *int64
Range time.Duration Range time.Duration
// OriginalOffset is the actual offset that was set in the query. // OriginalOffset is the actual offset that was set in the query.
// This never changes. // This never changes.
@ -140,7 +142,6 @@ type SubqueryExpr struct {
// which is calculated using the original offset, at modifier time, // which is calculated using the original offset, at modifier time,
// eval time, and subquery offsets in the AST tree. // eval time, and subquery offsets in the AST tree.
Offset time.Duration Offset time.Duration
Timestamp *int64
StartOrEnd ItemType // Set when @ is used with start() or end() StartOrEnd ItemType // Set when @ is used with start() or end()
Step time.Duration Step time.Duration
@ -170,9 +171,10 @@ type StringLiteral struct {
// UnaryExpr represents a unary operation on another expression. // UnaryExpr represents a unary operation on another expression.
// Currently unary operations are only supported for Scalars. // Currently unary operations are only supported for Scalars.
type UnaryExpr struct { type UnaryExpr struct {
Op ItemType
Expr Expr Expr Expr
Op ItemType
StartPos posrange.Pos StartPos posrange.Pos
} }
@ -191,7 +193,15 @@ func (e *StepInvariantExpr) PositionRange() posrange.PositionRange {
// VectorSelector represents a Vector selection. // VectorSelector represents a Vector selection.
type VectorSelector struct { type VectorSelector struct {
// The unexpanded seriesSet populated at query preparation time.
UnexpandedSeriesSet storage.SeriesSet
Timestamp *int64
Name string Name string
LabelMatchers []*labels.Matcher
Series []storage.Series
PosRange posrange.PositionRange
// OriginalOffset is the actual offset that was set in the query. // OriginalOffset is the actual offset that was set in the query.
// This never changes. // This never changes.
OriginalOffset time.Duration OriginalOffset time.Duration
@ -199,16 +209,8 @@ type VectorSelector struct {
// which is calculated using the original offset, at modifier time, // which is calculated using the original offset, at modifier time,
// eval time, and subquery offsets in the AST tree. // eval time, and subquery offsets in the AST tree.
Offset time.Duration Offset time.Duration
Timestamp *int64
SkipHistogramBuckets bool // Set when decoding native histogram buckets is not needed for query evaluation.
StartOrEnd ItemType // Set when @ is used with start() or end() StartOrEnd ItemType // Set when @ is used with start() or end()
LabelMatchers []*labels.Matcher SkipHistogramBuckets bool // Set when decoding native histogram buckets is not needed for query evaluation.
// The unexpanded seriesSet populated at query preparation time.
UnexpandedSeriesSet storage.SeriesSet
Series []storage.Series
PosRange posrange.PositionRange
} }
// TestStmt is an internal helper statement that allows execution // TestStmt is an internal helper statement that allows execution
@ -282,17 +284,17 @@ func (vmc VectorMatchCardinality) String() string {
// VectorMatching describes how elements from two Vectors in a binary // VectorMatching describes how elements from two Vectors in a binary
// operation are supposed to be matched. // operation are supposed to be matched.
type VectorMatching struct { type VectorMatching struct {
// The cardinality of the two Vectors.
Card VectorMatchCardinality
// MatchingLabels contains the labels which define equality of a pair of // MatchingLabels contains the labels which define equality of a pair of
// elements from the Vectors. // elements from the Vectors.
MatchingLabels []string MatchingLabels []string
// On includes the given label names from matching,
// rather than excluding them.
On bool
// Include contains additional labels that should be included in // Include contains additional labels that should be included in
// the result from the side with the lower cardinality. // the result from the side with the lower cardinality.
Include []string Include []string
// The cardinality of the two Vectors.
Card VectorMatchCardinality
// On includes the given label names from matching,
// rather than excluding them.
On bool
} }
// Visitor allows visiting a Node and its child nodes. The Visit method is // Visitor allows visiting a Node and its child nodes. The Visit method is

View file

@ -17,9 +17,9 @@ package parser
// used by function nodes. // used by function nodes.
type Function struct { type Function struct {
Name string Name string
ReturnType ValueType
ArgTypes []ValueType ArgTypes []ValueType
Variadic int Variadic int
ReturnType ValueType
Experimental bool Experimental bool
} }

View file

@ -24,9 +24,9 @@ import (
// Item represents a token or text string returned from the scanner. // Item represents a token or text string returned from the scanner.
type Item struct { type Item struct {
Val string // The value of this Item.
Typ ItemType // The type of this Item. Typ ItemType // The type of this Item.
Pos posrange.Pos // The starting position, in bytes, of this Item in the input string. Pos posrange.Pos // The starting position, in bytes, of this Item in the input string.
Val string // The value of this Item.
} }
// String returns a descriptive string for the Item. // String returns a descriptive string for the Item.
@ -250,25 +250,27 @@ const (
// Lexer holds the state of the scanner. // Lexer holds the state of the scanner.
type Lexer struct { type Lexer struct {
input string // The string being scanned.
state stateFn // The next lexing function to enter. state stateFn // The next lexing function to enter.
itemp *Item // Pointer to where the next scanned item should be placed.
input string // The string being scanned.
pos posrange.Pos // Current position in the input. pos posrange.Pos // Current position in the input.
start posrange.Pos // Start position of this Item. start posrange.Pos // Start position of this Item.
width posrange.Pos // Width of last rune read from input. width posrange.Pos // Width of last rune read from input.
lastPos posrange.Pos // Position of most recent Item returned by NextItem. lastPos posrange.Pos // Position of most recent Item returned by NextItem.
itemp *Item // Pointer to where the next scanned item should be placed.
scannedItem bool // Set to true every time an item is scanned.
parenDepth int // Nesting depth of ( ) exprs. parenDepth int // Nesting depth of ( ) exprs.
histogramState histogramState // Determines whether or not inside of a histogram description.
stringOpen rune // Quote rune of the string currently being read.
scannedItem bool // Set to true every time an item is scanned.
braceOpen bool // Whether a { is opened. braceOpen bool // Whether a { is opened.
bracketOpen bool // Whether a [ is opened. bracketOpen bool // Whether a [ is opened.
gotColon bool // Whether we got a ':' after [ was opened. gotColon bool // Whether we got a ':' after [ was opened.
stringOpen rune // Quote rune of the string currently being read.
// series description variables for internal PromQL testing framework as well as in promtool rules unit tests. // series description variables for internal PromQL testing framework as well as in promtool rules unit tests.
// see https://prometheus.io/docs/prometheus/latest/configuration/unit_testing_rules/#series // see https://prometheus.io/docs/prometheus/latest/configuration/unit_testing_rules/#series
seriesDesc bool // Whether we are lexing a series description. seriesDesc bool // Whether we are lexing a series description.
histogramState histogramState // Determines whether or not inside of a histogram description.
} }
// next returns the next rune in the input. // next returns the next rune in the input.
@ -297,7 +299,7 @@ func (l *Lexer) backup() {
// emit passes an Item back to the client. // emit passes an Item back to the client.
func (l *Lexer) emit(t ItemType) { func (l *Lexer) emit(t ItemType) {
*l.itemp = Item{t, l.start, l.input[l.start:l.pos]} *l.itemp = Item{Typ: t, Pos: l.start, Val: l.input[l.start:l.pos]}
l.start = l.pos l.start = l.pos
l.scannedItem = true l.scannedItem = true
} }
@ -332,7 +334,7 @@ func (l *Lexer) acceptRun(valid string) {
// errorf returns an error token and terminates the scan by passing // errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.NextItem. // back a nil pointer that will be the next state, terminating l.NextItem.
func (l *Lexer) errorf(format string, args ...interface{}) stateFn { func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
*l.itemp = Item{ERROR, l.start, fmt.Sprintf(format, args...)} *l.itemp = Item{Typ: ERROR, Pos: l.start, Val: fmt.Sprintf(format, args...)}
l.scannedItem = true l.scannedItem = true
return nil return nil

File diff suppressed because it is too large Load diff

View file

@ -45,22 +45,23 @@ type Parser interface {
} }
type parser struct { type parser struct {
lex Lexer generatedParserResult interface{}
inject ItemType
injecting bool
// functions contains all functions supported by the parser instance. // functions contains all functions supported by the parser instance.
functions map[string]*Function functions map[string]*Function
parseErrors ParseErrors
lex Lexer
yyParser yyParserImpl
inject ItemType
// Everytime an Item is lexed that could be the end // Everytime an Item is lexed that could be the end
// of certain expressions its end position is stored here. // of certain expressions its end position is stored here.
lastClosing posrange.Pos lastClosing posrange.Pos
yyParser yyParserImpl injecting bool
generatedParserResult interface{}
parseErrors ParseErrors
} }
type Opt func(p *parser) type Opt func(p *parser)
@ -121,10 +122,11 @@ func (p *parser) Close() {
// ParseErr wraps a parsing error with line and position context. // ParseErr wraps a parsing error with line and position context.
type ParseErr struct { type ParseErr struct {
PositionRange posrange.PositionRange
Err error Err error
Query string Query string
PositionRange posrange.PositionRange
// LineOffset is an additional line offset to be added. Only used inside unit tests. // LineOffset is an additional line offset to be added. Only used inside unit tests.
LineOffset int LineOffset int
} }
@ -224,9 +226,9 @@ func ParseMetricSelectors(matchers []string) (m [][]*labels.Matcher, err error)
// SequenceValue is an omittable value in a sequence of time series values. // SequenceValue is an omittable value in a sequence of time series values.
type SequenceValue struct { type SequenceValue struct {
Histogram *histogram.FloatHistogram
Value float64 Value float64
Omitted bool Omitted bool
Histogram *histogram.FloatHistogram
} }
func (v SequenceValue) String() string { func (v SequenceValue) String() string {

View file

@ -152,12 +152,13 @@ func runTest(t testutil.T, input string, engine promql.QueryEngine) error {
type test struct { type test struct {
testutil.T testutil.T
cmds []testCommand context context.Context
storage *teststorage.TestStorage storage *teststorage.TestStorage
context context.Context
cancelCtx context.CancelFunc cancelCtx context.CancelFunc
cmds []testCommand
} }
// newTest returns an initialized empty Test. // newTest returns an initialized empty Test.
@ -407,10 +408,10 @@ func (*evalCmd) testCmd() {}
// loadCmd is a command that loads sequences of sample values for specific // loadCmd is a command that loads sequences of sample values for specific
// metrics into the storage. // metrics into the storage.
type loadCmd struct { type loadCmd struct {
gap time.Duration
metrics map[uint64]labels.Labels metrics map[uint64]labels.Labels
defs map[uint64][]promql.Sample defs map[uint64][]promql.Sample
exemplars map[uint64][]exemplar.Exemplar exemplars map[uint64][]exemplar.Exemplar
gap time.Duration
withNHCB bool withNHCB bool
} }
@ -476,9 +477,9 @@ func getHistogramMetricBase(m labels.Labels, suffix string) (labels.Labels, uint
} }
type tempHistogramWrapper struct { type tempHistogramWrapper struct {
histogramByTs map[int64]tempHistogram
metric labels.Labels metric labels.Labels
upperBounds []float64 upperBounds []float64
histogramByTs map[int64]tempHistogram
} }
func newTempHistogramWrapper() tempHistogramWrapper { func newTempHistogramWrapper() tempHistogramWrapper {
@ -639,24 +640,24 @@ func appendSample(a storage.Appender, s promql.Sample, m labels.Labels) error {
// evalCmd is a command that evaluates an expression for the given time (range) // evalCmd is a command that evaluates an expression for the given time (range)
// and expects a specific result. // and expects a specific result.
type evalCmd struct { type evalCmd struct {
expr string
start time.Time start time.Time
end time.Time end time.Time
expectedFailRegexp *regexp.Regexp
metrics map[uint64]labels.Labels
expected map[uint64]entry
expr string
expectedFailMessage string
step time.Duration step time.Duration
line int line int
isRange bool // if false, instant query isRange bool // if false, instant query
fail, warn, ordered bool fail, warn, ordered bool
expectedFailMessage string
expectedFailRegexp *regexp.Regexp
metrics map[uint64]labels.Labels
expected map[uint64]entry
} }
type entry struct { type entry struct {
pos int
vals []parser.SequenceValue vals []parser.SequenceValue
pos int
} }
func (e entry) String() string { func (e entry) String() string {
@ -886,8 +887,8 @@ func (cmd clearCmd) String() string {
} }
type atModifierTestCase struct { type atModifierTestCase struct {
expr string
evalTime time.Time evalTime time.Time
expr string
} }
func atModifierTestCases(exprStr string, evalTime time.Time) ([]atModifierTestCase, error) { func atModifierTestCases(exprStr string, evalTime time.Time) ([]atModifierTestCase, error) {
@ -1167,15 +1168,15 @@ func parseNumber(s string) (float64, error) {
// LazyLoader lazily loads samples into storage. // LazyLoader lazily loads samples into storage.
// This is specifically implemented for unit testing of rules. // This is specifically implemented for unit testing of rules.
type LazyLoader struct { type LazyLoader struct {
storage storage.Storage
context context.Context
loadCmd *loadCmd loadCmd *loadCmd
storage storage.Storage
SubqueryInterval time.Duration
queryEngine *promql.Engine queryEngine *promql.Engine
context context.Context
cancelCtx context.CancelFunc cancelCtx context.CancelFunc
SubqueryInterval time.Duration
opts LazyLoaderOpts opts LazyLoaderOpts
} }

View file

@ -31,10 +31,10 @@ import (
) )
type ActiveQueryTracker struct { type ActiveQueryTracker struct {
mmapedFile []byte
getNextIndex chan int
logger log.Logger logger log.Logger
closer io.Closer closer io.Closer
getNextIndex chan int
mmapedFile []byte
maxConcurrent int maxConcurrent int
} }

View file

@ -35,8 +35,8 @@ func (String) Type() parser.ValueType { return parser.ValueTypeString }
// String represents a string value. // String represents a string value.
type String struct { type String struct {
T int64
V string V string
T int64
} }
func (s String) String() string { func (s String) String() string {
@ -113,8 +113,8 @@ func (p FPoint) MarshalJSON() ([]byte, error) {
// HPoint represents a single histogram data point for a given timestamp. // HPoint represents a single histogram data point for a given timestamp.
// H must never be nil. // H must never be nil.
type HPoint struct { type HPoint struct {
T int64
H *histogram.FloatHistogram H *histogram.FloatHistogram
T int64
} }
func (p HPoint) String() string { func (p HPoint) String() string {
@ -189,11 +189,11 @@ func totalHPointSize(histograms []HPoint) int {
// sample or a histogram sample. If H is nil, it is a float sample. Otherwise, // sample or a histogram sample. If H is nil, it is a float sample. Otherwise,
// it is a histogram sample. // it is a histogram sample.
type Sample struct { type Sample struct {
T int64
F float64
H *histogram.FloatHistogram H *histogram.FloatHistogram
Metric labels.Labels Metric labels.Labels
T int64
F float64
} }
func (s Sample) String() string { func (s Sample) String() string {
@ -222,8 +222,8 @@ func (s Sample) MarshalJSON() ([]byte, error) {
return json.Marshal(f) return json.Marshal(f)
} }
h := struct { h := struct {
M labels.Labels `json:"metric"`
H HPoint `json:"histogram"` H HPoint `json:"histogram"`
M labels.Labels `json:"metric"`
}{ }{
M: s.Metric, M: s.Metric,
H: HPoint{T: s.T, H: s.H}, H: HPoint{T: s.T, H: s.H},
@ -417,12 +417,12 @@ func (ss *StorageSeries) Iterator(it chunkenc.Iterator) chunkenc.Iterator {
} }
type storageSeriesIterator struct { type storageSeriesIterator struct {
currH *histogram.FloatHistogram
floats []FPoint floats []FPoint
histograms []HPoint histograms []HPoint
iFloats, iHistograms int iFloats, iHistograms int
currT int64 currT int64
currF float64 currF float64
currH *histogram.FloatHistogram
} }
func newStorageSeriesIterator(series Series) *storageSeriesIterator { func newStorageSeriesIterator(series Series) *storageSeriesIterator {