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"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2017-11-17 13:33:24 -08:00
|
|
|
// QueryStats currently only holding query timings.
|
2017-11-17 13:30:59 -08:00
|
|
|
type QueryStats struct {
|
|
|
|
Timings queryTimings `json:"timings,omitempty"`
|
|
|
|
}
|
|
|
|
|
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.
|
2018-08-16 00:41:34 -07:00
|
|
|
func NewQueryStats(tg *QueryTimers) *QueryStats {
|
2017-11-17 13:30:59 -08:00
|
|
|
var qt queryTimings
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 13:30:59 -08:00
|
|
|
qs := QueryStats{Timings: qt}
|
2017-02-08 03:58:40 -08:00
|
|
|
return &qs
|
|
|
|
}
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryTimers struct {
|
|
|
|
*TimerGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewQueryTimers() *QueryTimers {
|
|
|
|
return &QueryTimers{NewTimerGroup()}
|
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|