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
// Sample stats for the query execution.
sampleStats *stats.QuerySamples
// Result matrix for reuse.
matrix Matrix
// Cancellation function for the query.
cancel func()
// The engine against which the query is executed.
ng *Engine
// Result matrix for reuse.
matrix Matrix
}
type QueryOrigin struct{}
@ -287,17 +287,18 @@ type QueryTracker interface {
type EngineOpts struct {
Logger log.Logger
Reg prometheus.Registerer
MaxSamples int
Timeout time.Duration
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
// a subquery in milliseconds if no step in range vector was specified `[30m:<step>]`.
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
// 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
@ -319,14 +320,14 @@ type EngineOpts struct {
// It is connected to a querier.
type Engine struct {
logger log.Logger
metrics *engineMetrics
timeout time.Duration
maxSamplesPerQuery int
activeQueryTracker QueryTracker
queryLogger QueryLogger
queryLoggerLock sync.RWMutex
lookbackDelta time.Duration
metrics *engineMetrics
noStepSubqueryIntervalFn func(rangeMillis int64) int64
timeout time.Duration
maxSamplesPerQuery int
lookbackDelta time.Duration
queryLoggerLock sync.RWMutex
enableAtModifier bool
enableNegativeOffset bool
enablePerStepStats bool
@ -1022,16 +1023,17 @@ func (e errWithWarnings) Error() string { return e.err.Error() }
type evaluator struct {
ctx context.Context
logger log.Logger
samplesStats *stats.QuerySamples
noStepSubqueryIntervalFn func(rangeMillis int64) int64
startTimestamp int64 // Start time in milliseconds.
endTimestamp int64 // End time in milliseconds.
interval int64 // Interval in milliseconds.
maxSamples int
currentSamples int
logger log.Logger
lookbackDelta time.Duration
samplesStats *stats.QuerySamples
noStepSubqueryIntervalFn func(rangeMillis int64) int64
}
// 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.
type EvalNodeHelper struct {
// Evaluation timestamp.
Ts int64
// Vector that can be used for output.
Out Vector
// Caches.
// funcHistogramQuantile for classic histograms.
signatureToMetricWithBuckets map[string]*metricWithBuckets
lb *labels.Builder
lblBuf []byte
lblResultBuf []byte
// For binary vector matching.
rightSigs map[string]Sample
matchedSigs map[string]map[uint64]struct{}
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) {
@ -2773,15 +2776,15 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64, hlhs, hrhs *histogram
}
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.
hasFloat bool // Has at least 1 float64 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
heap vectorByValueHeap
}
// 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.
type AggregateExpr struct {
Op ItemType // The used aggregation operation.
Expr Expr // The Vector expression over which is aggregated.
Param Expr // Parameter used by some aggregators.
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
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.
type BinaryExpr struct {
Op ItemType // The operation of the expression.
LHS, RHS Expr // The operands on the respective sides of the operator.
// The matching behavior for the operation if both operands are Vectors.
// If they are not this field is nil.
VectorMatching *VectorMatching
Op ItemType // The operation of the expression.
// If a comparison operator, return 0/1 rather than filtering.
ReturnBool bool
}
@ -132,6 +133,7 @@ type MatrixSelector struct {
// SubqueryExpr represents a subquery.
type SubqueryExpr struct {
Expr Expr
Timestamp *int64
Range time.Duration
// OriginalOffset is the actual offset that was set in the query.
// This never changes.
@ -140,7 +142,6 @@ type SubqueryExpr struct {
// which is calculated using the original offset, at modifier time,
// eval time, and subquery offsets in the AST tree.
Offset time.Duration
Timestamp *int64
StartOrEnd ItemType // Set when @ is used with start() or end()
Step time.Duration
@ -170,9 +171,10 @@ type StringLiteral struct {
// UnaryExpr represents a unary operation on another expression.
// Currently unary operations are only supported for Scalars.
type UnaryExpr struct {
Op ItemType
Expr Expr
Op ItemType
StartPos posrange.Pos
}
@ -191,7 +193,15 @@ func (e *StepInvariantExpr) PositionRange() posrange.PositionRange {
// VectorSelector represents a Vector selection.
type VectorSelector struct {
// The unexpanded seriesSet populated at query preparation time.
UnexpandedSeriesSet storage.SeriesSet
Timestamp *int64
Name string
LabelMatchers []*labels.Matcher
Series []storage.Series
PosRange posrange.PositionRange
// OriginalOffset is the actual offset that was set in the query.
// This never changes.
OriginalOffset time.Duration
@ -199,16 +209,8 @@ type VectorSelector struct {
// which is calculated using the original offset, at modifier time,
// eval time, and subquery offsets in the AST tree.
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()
LabelMatchers []*labels.Matcher
// The unexpanded seriesSet populated at query preparation time.
UnexpandedSeriesSet storage.SeriesSet
Series []storage.Series
PosRange posrange.PositionRange
SkipHistogramBuckets bool // Set when decoding native histogram buckets is not needed for query evaluation.
}
// 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
// operation are supposed to be matched.
type VectorMatching struct {
// The cardinality of the two Vectors.
Card VectorMatchCardinality
// MatchingLabels contains the labels which define equality of a pair of
// elements from the Vectors.
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
// the result from the side with the lower cardinality.
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

View file

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

View file

@ -24,9 +24,9 @@ import (
// Item represents a token or text string returned from the scanner.
type Item struct {
Val string // The value 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.
Val string // The value of this Item.
}
// String returns a descriptive string for the Item.
@ -250,25 +250,27 @@ const (
// Lexer holds the state of the scanner.
type Lexer struct {
input string // The string being scanned.
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.
start posrange.Pos // Start position of this Item.
width posrange.Pos // Width of last rune read from input.
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.
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.
bracketOpen bool // Whether a [ is 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.
// see https://prometheus.io/docs/prometheus/latest/configuration/unit_testing_rules/#series
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.
@ -297,7 +299,7 @@ func (l *Lexer) backup() {
// emit passes an Item back to the client.
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.scannedItem = true
}
@ -332,7 +334,7 @@ func (l *Lexer) acceptRun(valid string) {
// errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.NextItem.
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
return nil

File diff suppressed because it is too large Load diff

View file

@ -45,22 +45,23 @@ type Parser interface {
}
type parser struct {
lex Lexer
inject ItemType
injecting bool
generatedParserResult interface{}
// functions contains all functions supported by the parser instance.
functions map[string]*Function
parseErrors ParseErrors
lex Lexer
yyParser yyParserImpl
inject ItemType
// Everytime an Item is lexed that could be the end
// of certain expressions its end position is stored here.
lastClosing posrange.Pos
yyParser yyParserImpl
generatedParserResult interface{}
parseErrors ParseErrors
injecting bool
}
type Opt func(p *parser)
@ -121,10 +122,11 @@ func (p *parser) Close() {
// ParseErr wraps a parsing error with line and position context.
type ParseErr struct {
PositionRange posrange.PositionRange
Err error
Query string
PositionRange posrange.PositionRange
// LineOffset is an additional line offset to be added. Only used inside unit tests.
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.
type SequenceValue struct {
Histogram *histogram.FloatHistogram
Value float64
Omitted bool
Histogram *histogram.FloatHistogram
}
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 {
testutil.T
cmds []testCommand
context context.Context
storage *teststorage.TestStorage
context context.Context
cancelCtx context.CancelFunc
cmds []testCommand
}
// 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
// metrics into the storage.
type loadCmd struct {
gap time.Duration
metrics map[uint64]labels.Labels
defs map[uint64][]promql.Sample
exemplars map[uint64][]exemplar.Exemplar
gap time.Duration
withNHCB bool
}
@ -476,9 +477,9 @@ func getHistogramMetricBase(m labels.Labels, suffix string) (labels.Labels, uint
}
type tempHistogramWrapper struct {
histogramByTs map[int64]tempHistogram
metric labels.Labels
upperBounds []float64
histogramByTs map[int64]tempHistogram
}
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)
// and expects a specific result.
type evalCmd struct {
expr string
start time.Time
end time.Time
expectedFailRegexp *regexp.Regexp
metrics map[uint64]labels.Labels
expected map[uint64]entry
expr string
expectedFailMessage string
step time.Duration
line int
isRange bool // if false, instant query
fail, warn, ordered bool
expectedFailMessage string
expectedFailRegexp *regexp.Regexp
metrics map[uint64]labels.Labels
expected map[uint64]entry
}
type entry struct {
pos int
vals []parser.SequenceValue
pos int
}
func (e entry) String() string {
@ -886,8 +887,8 @@ func (cmd clearCmd) String() string {
}
type atModifierTestCase struct {
expr string
evalTime time.Time
expr string
}
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.
// This is specifically implemented for unit testing of rules.
type LazyLoader struct {
storage storage.Storage
context context.Context
loadCmd *loadCmd
storage storage.Storage
SubqueryInterval time.Duration
queryEngine *promql.Engine
context context.Context
cancelCtx context.CancelFunc
SubqueryInterval time.Duration
opts LazyLoaderOpts
}

View file

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

View file

@ -35,8 +35,8 @@ func (String) Type() parser.ValueType { return parser.ValueTypeString }
// String represents a string value.
type String struct {
T int64
V string
T int64
}
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.
// H must never be nil.
type HPoint struct {
T int64
H *histogram.FloatHistogram
T int64
}
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,
// it is a histogram sample.
type Sample struct {
T int64
F float64
H *histogram.FloatHistogram
Metric labels.Labels
T int64
F float64
}
func (s Sample) String() string {
@ -222,8 +222,8 @@ func (s Sample) MarshalJSON() ([]byte, error) {
return json.Marshal(f)
}
h := struct {
M labels.Labels `json:"metric"`
H HPoint `json:"histogram"`
M labels.Labels `json:"metric"`
}{
M: s.Metric,
H: HPoint{T: s.T, H: s.H},
@ -417,12 +417,12 @@ func (ss *StorageSeries) Iterator(it chunkenc.Iterator) chunkenc.Iterator {
}
type storageSeriesIterator struct {
currH *histogram.FloatHistogram
floats []FPoint
histograms []HPoint
iFloats, iHistograms int
currT int64
currF float64
currH *histogram.FloatHistogram
}
func newStorageSeriesIterator(series Series) *storageSeriesIterator {