diff --git a/pkg/expr/sql_command.go b/pkg/expr/sql_command.go index 9f2042e7656..61e0431798e 100644 --- a/pkg/expr/sql_command.go +++ b/pkg/expr/sql_command.go @@ -8,6 +8,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/data" + "github.com/prometheus/client_golang/prometheus" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" @@ -132,9 +133,51 @@ func (gr *SQLCommand) Execute(ctx context.Context, now time.Time, vars mathexp.V } span.End() - metrics.SqlCommandCount.WithLabelValues(statusLabel, errorType).Inc() - metrics.SqlCommandDuration.WithLabelValues(statusLabel).Observe(duration) - metrics.SqlCommandCellCount.WithLabelValues(statusLabel).Observe(float64(tc)) + // --- Exemplar labels from the current span --- + sc := span.SpanContext() + var ex prometheus.Labels + if sc.IsValid() { + ex = prometheus.Labels{ + "trace_id": sc.TraceID().String(), + "span_id": sc.SpanID().String(), + } + } + + // --- Counter with exemplar (if supported) --- + cnt := metrics.SqlCommandCount.WithLabelValues(statusLabel, errorType) + if ex != nil { + if ce, ok := cnt.(prometheus.ExemplarAdder); ok { + ce.AddWithExemplar(1, ex) + } else { + cnt.Inc() + } + } else { + cnt.Inc() + } + + // --- Duration histogram with exemplar (if supported) --- + obs := metrics.SqlCommandDuration.WithLabelValues(statusLabel) + if ex != nil { + if eo, ok := obs.(prometheus.ExemplarObserver); ok { + eo.ObserveWithExemplar(duration, ex) + } else { + obs.Observe(duration) + } + } else { + obs.Observe(duration) + } + + // --- Cell count histogram with exemplar (if supported) --- + obsCells := metrics.SqlCommandCellCount.WithLabelValues(statusLabel) + if ex != nil { + if eo, ok := obsCells.(prometheus.ExemplarObserver); ok { + eo.ObserveWithExemplar(float64(tc), ex) + } else { + obsCells.Observe(float64(tc)) + } + } else { + obsCells.Observe(float64(tc)) + } }() allFrames := []*data.Frame{} diff --git a/pkg/expr/sql_command_test.go b/pkg/expr/sql_command_test.go index 58b2e11fcb6..d15d36f874c 100644 --- a/pkg/expr/sql_command_test.go +++ b/pkg/expr/sql_command_test.go @@ -294,3 +294,7 @@ func (ts *testSpan) SetStatus(code codes.Code, msg string) {} func (ts *testSpan) AddEvent(name string, opts ...trace.EventOption) {} func (ts *testSpan) SetAttributes(kv ...attribute.KeyValue) {} + +func (ts *testSpan) SpanContext() trace.SpanContext { + return trace.SpanContext{} +}