2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-06-03 08:07:03 -07:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
2018-08-16 00:41:34 -07:00
|
|
|
import (
|
|
|
|
"context"
|
2022-02-01 18:07:23 -08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-08-16 00:41:34 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-01-25 02:08:04 -08:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2018-08-16 00:41:34 -07:00
|
|
|
)
|
|
|
|
|
2013-06-03 08:07:03 -07:00
|
|
|
// QueryTiming identifies the code area or functionality in which time is spent
|
|
|
|
// during a query.
|
|
|
|
type QueryTiming int
|
|
|
|
|
|
|
|
// Query timings.
|
|
|
|
const (
|
2017-11-17 08:46:51 -08:00
|
|
|
EvalTotalTime QueryTiming = iota
|
2013-06-03 08:07:03 -07:00
|
|
|
ResultSortTime
|
2016-07-11 11:27:25 -07:00
|
|
|
QueryPreparationTime
|
2013-06-03 08:07:03 -07:00
|
|
|
InnerEvalTime
|
2015-04-30 15:49:19 -07:00
|
|
|
ExecQueueTime
|
2017-02-08 03:58:40 -08:00
|
|
|
ExecTotalTime
|
2013-06-03 08:07:03 -07:00
|
|
|
)
|
|
|
|
|
2016-02-09 18:47:00 -08:00
|
|
|
// Return a string representation of a QueryTiming identifier.
|
2013-06-03 08:07:03 -07:00
|
|
|
func (s QueryTiming) String() string {
|
|
|
|
switch s {
|
2017-11-17 08:46:51 -08:00
|
|
|
case EvalTotalTime:
|
|
|
|
return "Eval total time"
|
2013-06-03 08:07:03 -07:00
|
|
|
case ResultSortTime:
|
|
|
|
return "Result sorting time"
|
2016-07-11 11:27:25 -07:00
|
|
|
case QueryPreparationTime:
|
|
|
|
return "Query preparation time"
|
2013-06-03 08:07:03 -07:00
|
|
|
case InnerEvalTime:
|
|
|
|
return "Inner eval time"
|
2015-04-30 15:49:19 -07:00
|
|
|
case ExecQueueTime:
|
|
|
|
return "Exec queue wait time"
|
2017-02-08 03:58:40 -08:00
|
|
|
case ExecTotalTime:
|
|
|
|
return "Exec total time"
|
2013-06-03 08:07:03 -07:00
|
|
|
default:
|
|
|
|
return "Unknown query timing"
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 03:58:40 -08:00
|
|
|
|
2019-09-10 06:45:09 -07:00
|
|
|
// SpanOperation returns a string representation of a QueryTiming span operation.
|
2018-08-16 00:41:34 -07:00
|
|
|
func (s QueryTiming) SpanOperation() string {
|
|
|
|
switch s {
|
|
|
|
case EvalTotalTime:
|
|
|
|
return "promqlEval"
|
|
|
|
case ResultSortTime:
|
|
|
|
return "promqlSort"
|
|
|
|
case QueryPreparationTime:
|
|
|
|
return "promqlPrepare"
|
|
|
|
case InnerEvalTime:
|
|
|
|
return "promqlInnerEval"
|
|
|
|
case ExecQueueTime:
|
|
|
|
return "promqlExecQueue"
|
|
|
|
case ExecTotalTime:
|
|
|
|
return "promqlExec"
|
|
|
|
default:
|
|
|
|
return "Unknown query timing"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
// stepStat represents a single statistic for a given step timestamp.
|
|
|
|
type stepStat struct {
|
|
|
|
T int64
|
2022-03-23 07:26:58 -07:00
|
|
|
V int64
|
2022-02-01 18:07:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s stepStat) String() string {
|
2022-03-23 07:26:58 -07:00
|
|
|
return fmt.Sprintf("%v @[%v]", s.V, s.T)
|
2022-02-01 18:07:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (s stepStat) MarshalJSON() ([]byte, error) {
|
2022-03-23 07:26:58 -07:00
|
|
|
return json.Marshal([...]interface{}{float64(s.T) / 1000, s.V})
|
2022-02-01 18:07:23 -08:00
|
|
|
}
|
|
|
|
|
2017-11-17 13:33:24 -08:00
|
|
|
// queryTimings with all query timers mapped to durations.
|
2017-11-17 13:30:59 -08:00
|
|
|
type queryTimings struct {
|
2017-11-17 08:46:51 -08:00
|
|
|
EvalTotalTime float64 `json:"evalTotalTime"`
|
2017-02-08 03:58:40 -08:00
|
|
|
ResultSortTime float64 `json:"resultSortTime"`
|
|
|
|
QueryPreparationTime float64 `json:"queryPreparationTime"`
|
|
|
|
InnerEvalTime float64 `json:"innerEvalTime"`
|
|
|
|
ExecQueueTime float64 `json:"execQueueTime"`
|
|
|
|
ExecTotalTime float64 `json:"execTotalTime"`
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
type querySamples struct {
|
|
|
|
TotalQueryableSamplesPerStep []stepStat `json:"totalQueryableSamplesPerStep,omitempty"`
|
|
|
|
TotalQueryableSamples int `json:"totalQueryableSamples"`
|
2022-02-22 12:30:39 -08:00
|
|
|
PeakSamples int `json:"peakSamples"`
|
2022-02-01 18:07:23 -08:00
|
|
|
}
|
|
|
|
|
2022-02-10 06:17:05 -08:00
|
|
|
// BuiltinStats holds the statistics that Prometheus's core gathers.
|
|
|
|
type BuiltinStats struct {
|
2022-02-01 18:07:23 -08:00
|
|
|
Timings queryTimings `json:"timings,omitempty"`
|
|
|
|
Samples *querySamples `json:"samples,omitempty"`
|
2017-11-17 13:30:59 -08:00
|
|
|
}
|
|
|
|
|
2022-02-10 06:17:05 -08:00
|
|
|
// QueryStats holds BuiltinStats and any other stats the particular
|
|
|
|
// implementation wants to collect.
|
|
|
|
type QueryStats interface {
|
|
|
|
Builtin() BuiltinStats
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BuiltinStats) Builtin() BuiltinStats {
|
|
|
|
return *s
|
|
|
|
}
|
|
|
|
|
2017-11-16 07:30:48 -08:00
|
|
|
// NewQueryStats makes a QueryStats struct with all QueryTimings found in the
|
2017-02-08 03:58:40 -08:00
|
|
|
// given TimerGroup.
|
2022-02-10 06:17:05 -08:00
|
|
|
func NewQueryStats(s *Statistics) QueryStats {
|
2022-02-01 18:07:23 -08:00
|
|
|
var (
|
|
|
|
qt queryTimings
|
|
|
|
samples *querySamples
|
|
|
|
tg = s.Timers
|
|
|
|
sp = s.Samples
|
|
|
|
)
|
2017-02-08 03:58:40 -08:00
|
|
|
|
2018-08-16 00:41:34 -07:00
|
|
|
for s, timer := range tg.TimerGroup.timers {
|
2017-02-08 03:58:40 -08:00
|
|
|
switch s {
|
2017-11-17 08:46:51 -08:00
|
|
|
case EvalTotalTime:
|
2017-11-17 13:30:59 -08:00
|
|
|
qt.EvalTotalTime = timer.Duration()
|
2017-02-08 03:58:40 -08:00
|
|
|
case ResultSortTime:
|
2017-11-17 13:30:59 -08:00
|
|
|
qt.ResultSortTime = timer.Duration()
|
2017-02-08 03:58:40 -08:00
|
|
|
case QueryPreparationTime:
|
2017-11-17 13:30:59 -08:00
|
|
|
qt.QueryPreparationTime = timer.Duration()
|
2017-02-08 03:58:40 -08:00
|
|
|
case InnerEvalTime:
|
2017-11-17 13:30:59 -08:00
|
|
|
qt.InnerEvalTime = timer.Duration()
|
2017-02-08 03:58:40 -08:00
|
|
|
case ExecQueueTime:
|
2017-11-17 13:30:59 -08:00
|
|
|
qt.ExecQueueTime = timer.Duration()
|
2017-02-08 03:58:40 -08:00
|
|
|
case ExecTotalTime:
|
2017-11-17 13:30:59 -08:00
|
|
|
qt.ExecTotalTime = timer.Duration()
|
2017-02-08 03:58:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
if sp != nil {
|
|
|
|
samples = &querySamples{
|
|
|
|
TotalQueryableSamples: sp.TotalSamples,
|
2022-02-22 12:30:39 -08:00
|
|
|
PeakSamples: sp.PeakSamples,
|
2022-02-01 18:07:23 -08:00
|
|
|
}
|
|
|
|
samples.TotalQueryableSamplesPerStep = sp.totalSamplesPerStepPoints()
|
|
|
|
}
|
|
|
|
|
2022-02-10 06:17:05 -08:00
|
|
|
qs := BuiltinStats{Timings: qt, Samples: samples}
|
2017-02-08 03:58:40 -08:00
|
|
|
return &qs
|
|
|
|
}
|
2018-08-16 00:41:34 -07:00
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
func (qs *QuerySamples) TotalSamplesPerStepMap() *TotalSamplesPerStep {
|
|
|
|
if !qs.EnablePerStepStats {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := TotalSamplesPerStep{}
|
|
|
|
for _, s := range qs.totalSamplesPerStepPoints() {
|
|
|
|
ts[s.T] = int(s.V)
|
|
|
|
}
|
|
|
|
return &ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *QuerySamples) totalSamplesPerStepPoints() []stepStat {
|
|
|
|
if !qs.EnablePerStepStats {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := make([]stepStat, len(qs.TotalSamplesPerStep))
|
|
|
|
for i, c := range qs.TotalSamplesPerStep {
|
2022-03-23 07:26:58 -07:00
|
|
|
ts[i] = stepStat{T: qs.startTimestamp + int64(i)*qs.interval, V: int64(c)}
|
2022-02-01 18:07:23 -08:00
|
|
|
}
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:41:34 -07:00
|
|
|
// SpanTimer unifies tracing and timing, to reduce repetition.
|
|
|
|
type SpanTimer struct {
|
|
|
|
timer *Timer
|
|
|
|
observers []prometheus.Observer
|
|
|
|
|
2022-01-25 02:08:04 -08:00
|
|
|
span trace.Span
|
2018-08-16 00:41:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSpanTimer(ctx context.Context, operation string, timer *Timer, observers ...prometheus.Observer) (*SpanTimer, context.Context) {
|
2022-01-25 02:08:04 -08:00
|
|
|
ctx, span := otel.Tracer("").Start(ctx, operation)
|
2018-08-16 00:41:34 -07:00
|
|
|
timer.Start()
|
|
|
|
|
|
|
|
return &SpanTimer{
|
|
|
|
timer: timer,
|
|
|
|
observers: observers,
|
|
|
|
|
|
|
|
span: span,
|
|
|
|
}, ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SpanTimer) Finish() {
|
|
|
|
s.timer.Stop()
|
2022-01-25 02:08:04 -08:00
|
|
|
s.span.End()
|
2018-08-16 00:41:34 -07:00
|
|
|
|
|
|
|
for _, obs := range s.observers {
|
|
|
|
obs.Observe(s.timer.ElapsedTime().Seconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
type Statistics struct {
|
|
|
|
Timers *QueryTimers
|
|
|
|
Samples *QuerySamples
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:41:34 -07:00
|
|
|
type QueryTimers struct {
|
|
|
|
*TimerGroup
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
type TotalSamplesPerStep map[int64]int
|
|
|
|
|
|
|
|
type QuerySamples struct {
|
2022-02-22 12:30:39 -08:00
|
|
|
// PeakSamples represent the highest count of samples considered
|
|
|
|
// while evaluating a query. It corresponds to the peak value of
|
|
|
|
// currentSamples, which is in turn compared against the MaxSamples
|
|
|
|
// configured in the engine.
|
|
|
|
PeakSamples int
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
// TotalSamples represents the total number of samples scanned
|
|
|
|
// while evaluating a query.
|
|
|
|
TotalSamples int
|
|
|
|
|
|
|
|
// TotalSamplesPerStep represents the total number of samples scanned
|
|
|
|
// per step while evaluating a query. Each step should be identical to the
|
|
|
|
// TotalSamples when a step is run as an instant query, which means
|
|
|
|
// we intentionally do not account for optimizations that happen inside the
|
|
|
|
// range query engine that reduce the actual work that happens.
|
|
|
|
TotalSamplesPerStep []int
|
|
|
|
|
|
|
|
EnablePerStepStats bool
|
|
|
|
startTimestamp int64
|
|
|
|
interval int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stats struct {
|
|
|
|
TimerStats *QueryTimers
|
|
|
|
SampleStats *QuerySamples
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *QuerySamples) InitStepTracking(start, end, interval int64) {
|
|
|
|
if !qs.EnablePerStepStats {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
numSteps := int((end-start)/interval) + 1
|
|
|
|
qs.TotalSamplesPerStep = make([]int, numSteps)
|
|
|
|
qs.startTimestamp = start
|
|
|
|
qs.interval = interval
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementSamplesAtStep increments the total samples count. Use this if you know the step index.
|
|
|
|
func (qs *QuerySamples) IncrementSamplesAtStep(i, samples int) {
|
|
|
|
if qs == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
qs.TotalSamples += samples
|
|
|
|
|
|
|
|
if qs.TotalSamplesPerStep != nil {
|
|
|
|
qs.TotalSamplesPerStep[i] += samples
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementSamplesAtTimestamp increments the total samples count. Use this if you only have the corresponding step
|
|
|
|
// timestamp.
|
|
|
|
func (qs *QuerySamples) IncrementSamplesAtTimestamp(t int64, samples int) {
|
|
|
|
if qs == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
qs.TotalSamples += samples
|
|
|
|
|
|
|
|
if qs.TotalSamplesPerStep != nil {
|
|
|
|
i := int((t - qs.startTimestamp) / qs.interval)
|
|
|
|
qs.TotalSamplesPerStep[i] += samples
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:30:39 -08:00
|
|
|
// UpdatePeak updates the peak number of samples considered in
|
|
|
|
// the evaluation of a query as used with the MaxSamples limit.
|
|
|
|
func (qs *QuerySamples) UpdatePeak(samples int) {
|
|
|
|
if qs == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if samples > qs.PeakSamples {
|
|
|
|
qs.PeakSamples = samples
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePeakFromSubquery updates the peak number of samples considered
|
|
|
|
// in a query from its evaluation of a subquery.
|
|
|
|
func (qs *QuerySamples) UpdatePeakFromSubquery(other *QuerySamples) {
|
|
|
|
if qs == nil || other == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if other.PeakSamples > qs.PeakSamples {
|
|
|
|
qs.PeakSamples = other.PeakSamples
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:41:34 -07:00
|
|
|
func NewQueryTimers() *QueryTimers {
|
|
|
|
return &QueryTimers{NewTimerGroup()}
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
func NewQuerySamples(enablePerStepStats bool) *QuerySamples {
|
|
|
|
qs := QuerySamples{EnablePerStepStats: enablePerStepStats}
|
|
|
|
return &qs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *QuerySamples) NewChild() *QuerySamples {
|
|
|
|
return NewQuerySamples(false)
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:41:34 -07:00
|
|
|
func (qs *QueryTimers) GetSpanTimer(ctx context.Context, qt QueryTiming, observers ...prometheus.Observer) (*SpanTimer, context.Context) {
|
|
|
|
return NewSpanTimer(ctx, qt.SpanOperation(), qs.TimerGroup.GetTimer(qt), observers...)
|
|
|
|
}
|