mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 11:52:30 +08:00
SQL Expression: Add instrumentation for sql expressions (#103758)
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/errutil"
|
||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||
"github.com/grafana/grafana/pkg/expr/metrics"
|
||||
"github.com/grafana/grafana/pkg/expr/sql"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
)
|
||||
@ -99,9 +100,22 @@ func (gr *SQLCommand) NeedsVars() []string {
|
||||
|
||||
// Execute runs the command and returns the results or an error if the command
|
||||
// failed to execute.
|
||||
func (gr *SQLCommand) Execute(ctx context.Context, now time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error) {
|
||||
func (gr *SQLCommand) Execute(ctx context.Context, now time.Time, vars mathexp.Vars, tracer tracing.Tracer, metrics *metrics.ExprMetrics) (mathExprResult mathexp.Results, resultError error) {
|
||||
_, span := tracer.Start(ctx, "SSE.ExecuteSQL")
|
||||
defer span.End()
|
||||
start := time.Now()
|
||||
tc := int64(0)
|
||||
|
||||
defer func() {
|
||||
span.End()
|
||||
statusLabel := "ok"
|
||||
duration := float64(time.Since(start).Nanoseconds()) / float64(time.Millisecond)
|
||||
if resultError != nil {
|
||||
statusLabel = "error"
|
||||
metrics.SqlCommandErrorCount.WithLabelValues().Inc()
|
||||
}
|
||||
metrics.SqlCommandDuration.WithLabelValues(statusLabel).Observe(duration)
|
||||
metrics.SqlCommandCellCount.WithLabelValues(statusLabel).Observe(float64(tc))
|
||||
}()
|
||||
|
||||
allFrames := []*data.Frame{}
|
||||
for _, ref := range gr.varsToQuery {
|
||||
@ -114,14 +128,15 @@ func (gr *SQLCommand) Execute(ctx context.Context, now time.Time, vars mathexp.V
|
||||
allFrames = append(allFrames, frames...)
|
||||
}
|
||||
|
||||
totalCells := totalCells(allFrames)
|
||||
tc = totalCells(allFrames)
|
||||
|
||||
// limit of 0 or less means no limit (following convention)
|
||||
if gr.limit > 0 && totalCells > gr.limit {
|
||||
if gr.limit > 0 && tc > gr.limit {
|
||||
return mathexp.Results{},
|
||||
fmt.Errorf(
|
||||
"SQL expression: total cell count across all input tables exceeds limit of %d. Total cells: %d",
|
||||
gr.limit,
|
||||
totalCells,
|
||||
tc,
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user