promql: remove global metrics
This commit is contained in:
parent
2ec5965b75
commit
f8fccc73d8
137
promql/engine.go
137
promql/engine.go
|
@ -47,64 +47,13 @@ const (
|
||||||
minInt64 = -9223372036854775808
|
minInt64 = -9223372036854775808
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type engineMetrics struct {
|
||||||
currentQueries = prometheus.NewGauge(prometheus.GaugeOpts{
|
currentQueries prometheus.Gauge
|
||||||
Namespace: namespace,
|
maxConcurrentQueries prometheus.Gauge
|
||||||
Subsystem: subsystem,
|
queryPrepareTime prometheus.Summary
|
||||||
Name: "queries",
|
queryInnerEval prometheus.Summary
|
||||||
Help: "The current number of queries being executed or waiting.",
|
queryResultAppend prometheus.Summary
|
||||||
})
|
queryResultSort prometheus.Summary
|
||||||
maxConcurrentQueries = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
||||||
Namespace: namespace,
|
|
||||||
Subsystem: subsystem,
|
|
||||||
Name: "queries_concurrent_max",
|
|
||||||
Help: "The max number of concurrent queries.",
|
|
||||||
})
|
|
||||||
queryPrepareTime = prometheus.NewSummary(
|
|
||||||
prometheus.SummaryOpts{
|
|
||||||
Namespace: namespace,
|
|
||||||
Subsystem: subsystem,
|
|
||||||
Name: "query_duration_seconds",
|
|
||||||
Help: "Query timings",
|
|
||||||
ConstLabels: prometheus.Labels{"slice": "prepare_time"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
queryInnerEval = prometheus.NewSummary(
|
|
||||||
prometheus.SummaryOpts{
|
|
||||||
Namespace: namespace,
|
|
||||||
Subsystem: subsystem,
|
|
||||||
Name: "query_duration_seconds",
|
|
||||||
Help: "Query timings",
|
|
||||||
ConstLabels: prometheus.Labels{"slice": "inner_eval"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
queryResultAppend = prometheus.NewSummary(
|
|
||||||
prometheus.SummaryOpts{
|
|
||||||
Namespace: namespace,
|
|
||||||
Subsystem: subsystem,
|
|
||||||
Name: "query_duration_seconds",
|
|
||||||
Help: "Query timings",
|
|
||||||
ConstLabels: prometheus.Labels{"slice": "result_append"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
queryResultSort = prometheus.NewSummary(
|
|
||||||
prometheus.SummaryOpts{
|
|
||||||
Namespace: namespace,
|
|
||||||
Subsystem: subsystem,
|
|
||||||
Name: "query_duration_seconds",
|
|
||||||
Help: "Query timings",
|
|
||||||
ConstLabels: prometheus.Labels{"slice": "result_sort"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
prometheus.MustRegister(currentQueries)
|
|
||||||
prometheus.MustRegister(maxConcurrentQueries)
|
|
||||||
prometheus.MustRegister(queryPrepareTime)
|
|
||||||
prometheus.MustRegister(queryInnerEval)
|
|
||||||
prometheus.MustRegister(queryResultAppend)
|
|
||||||
prometheus.MustRegister(queryResultSort)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertibleToInt64 returns true if v does not over-/underflow an int64.
|
// convertibleToInt64 returns true if v does not over-/underflow an int64.
|
||||||
|
@ -203,6 +152,7 @@ func contextDone(ctx context.Context, env string) error {
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
// A Querier constructor against an underlying storage.
|
// A Querier constructor against an underlying storage.
|
||||||
queryable Queryable
|
queryable Queryable
|
||||||
|
metrics *engineMetrics
|
||||||
// The gate limiting the maximum number of concurrent and waiting queries.
|
// The gate limiting the maximum number of concurrent and waiting queries.
|
||||||
gate *queryGate
|
gate *queryGate
|
||||||
options *EngineOptions
|
options *EngineOptions
|
||||||
|
@ -220,12 +170,66 @@ func NewEngine(queryable Queryable, o *EngineOptions) *Engine {
|
||||||
if o == nil {
|
if o == nil {
|
||||||
o = DefaultEngineOptions
|
o = DefaultEngineOptions
|
||||||
}
|
}
|
||||||
maxConcurrentQueries.Set(float64(o.MaxConcurrentQueries))
|
metrics := &engineMetrics{
|
||||||
|
currentQueries: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "queries",
|
||||||
|
Help: "The current number of queries being executed or waiting.",
|
||||||
|
}),
|
||||||
|
maxConcurrentQueries: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "queries_concurrent_max",
|
||||||
|
Help: "The max number of concurrent queries.",
|
||||||
|
}),
|
||||||
|
queryPrepareTime: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "query_duration_seconds",
|
||||||
|
Help: "Query timings",
|
||||||
|
ConstLabels: prometheus.Labels{"slice": "prepare_time"},
|
||||||
|
}),
|
||||||
|
queryInnerEval: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "query_duration_seconds",
|
||||||
|
Help: "Query timings",
|
||||||
|
ConstLabels: prometheus.Labels{"slice": "inner_eval"},
|
||||||
|
}),
|
||||||
|
queryResultAppend: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "query_duration_seconds",
|
||||||
|
Help: "Query timings",
|
||||||
|
ConstLabels: prometheus.Labels{"slice": "result_append"},
|
||||||
|
}),
|
||||||
|
queryResultSort: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "query_duration_seconds",
|
||||||
|
Help: "Query timings",
|
||||||
|
ConstLabels: prometheus.Labels{"slice": "result_sort"},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
metrics.maxConcurrentQueries.Set(float64(o.MaxConcurrentQueries))
|
||||||
|
|
||||||
|
if o.Metrics != nil {
|
||||||
|
o.Metrics.MustRegister(
|
||||||
|
metrics.currentQueries,
|
||||||
|
metrics.maxConcurrentQueries,
|
||||||
|
metrics.queryInnerEval,
|
||||||
|
metrics.queryPrepareTime,
|
||||||
|
metrics.queryResultAppend,
|
||||||
|
metrics.queryResultSort,
|
||||||
|
)
|
||||||
|
}
|
||||||
return &Engine{
|
return &Engine{
|
||||||
queryable: queryable,
|
queryable: queryable,
|
||||||
gate: newQueryGate(o.MaxConcurrentQueries),
|
gate: newQueryGate(o.MaxConcurrentQueries),
|
||||||
options: o,
|
options: o,
|
||||||
logger: o.Logger,
|
logger: o.Logger,
|
||||||
|
metrics: metrics,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,6 +238,7 @@ type EngineOptions struct {
|
||||||
MaxConcurrentQueries int
|
MaxConcurrentQueries int
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Logger log.Logger
|
Logger log.Logger
|
||||||
|
Metrics prometheus.Registerer
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultEngineOptions are the default engine options.
|
// DefaultEngineOptions are the default engine options.
|
||||||
|
@ -308,8 +313,8 @@ func (ng *Engine) newTestQuery(f func(context.Context) error) Query {
|
||||||
// At this point per query only one EvalStmt is evaluated. Alert and record
|
// At this point per query only one EvalStmt is evaluated. Alert and record
|
||||||
// statements are not handled by the Engine.
|
// statements are not handled by the Engine.
|
||||||
func (ng *Engine) exec(ctx context.Context, q *query) (Value, error) {
|
func (ng *Engine) exec(ctx context.Context, q *query) (Value, error) {
|
||||||
currentQueries.Inc()
|
ng.metrics.currentQueries.Inc()
|
||||||
defer currentQueries.Dec()
|
defer ng.metrics.currentQueries.Dec()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, ng.options.Timeout)
|
ctx, cancel := context.WithTimeout(ctx, ng.options.Timeout)
|
||||||
q.cancel = cancel
|
q.cancel = cancel
|
||||||
|
@ -362,7 +367,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
prepareTimer := query.stats.GetTimer(stats.QueryPreparationTime).Start()
|
prepareTimer := query.stats.GetTimer(stats.QueryPreparationTime).Start()
|
||||||
querier, err := ng.populateIterators(ctx, s)
|
querier, err := ng.populateIterators(ctx, s)
|
||||||
prepareTimer.Stop()
|
prepareTimer.Stop()
|
||||||
queryPrepareTime.Observe(prepareTimer.ElapsedTime().Seconds())
|
ng.metrics.queryPrepareTime.Observe(prepareTimer.ElapsedTime().Seconds())
|
||||||
|
|
||||||
// XXX(fabxc): the querier returned by populateIterators might be instantiated
|
// XXX(fabxc): the querier returned by populateIterators might be instantiated
|
||||||
// we must not return without closing irrespective of the error.
|
// we must not return without closing irrespective of the error.
|
||||||
|
@ -390,7 +395,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
}
|
}
|
||||||
|
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
queryInnerEval.Observe(evalTimer.ElapsedTime().Seconds())
|
ng.metrics.queryInnerEval.Observe(evalTimer.ElapsedTime().Seconds())
|
||||||
// Point might have a different timestamp, force it to the evaluation
|
// Point might have a different timestamp, force it to the evaluation
|
||||||
// timestamp as that is when we ran the evaluation.
|
// timestamp as that is when we ran the evaluation.
|
||||||
switch v := val.(type) {
|
switch v := val.(type) {
|
||||||
|
@ -456,7 +461,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
queryInnerEval.Observe(evalTimer.ElapsedTime().Seconds())
|
ng.metrics.queryInnerEval.Observe(evalTimer.ElapsedTime().Seconds())
|
||||||
|
|
||||||
if err := contextDone(ctx, "expression evaluation"); err != nil {
|
if err := contextDone(ctx, "expression evaluation"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -468,7 +473,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
mat = append(mat, ss)
|
mat = append(mat, ss)
|
||||||
}
|
}
|
||||||
appendTimer.Stop()
|
appendTimer.Stop()
|
||||||
queryResultAppend.Observe(appendTimer.ElapsedTime().Seconds())
|
ng.metrics.queryResultAppend.Observe(appendTimer.ElapsedTime().Seconds())
|
||||||
|
|
||||||
if err := contextDone(ctx, "expression evaluation"); err != nil {
|
if err := contextDone(ctx, "expression evaluation"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -480,7 +485,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
sort.Sort(mat)
|
sort.Sort(mat)
|
||||||
sortTimer.Stop()
|
sortTimer.Stop()
|
||||||
|
|
||||||
queryResultSort.Observe(sortTimer.ElapsedTime().Seconds())
|
ng.metrics.queryResultSort.Observe(sortTimer.ElapsedTime().Seconds())
|
||||||
return mat, nil
|
return mat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue