SQL Expressions: Add Exemplars to instrumentation (#110481)

follow up to #109633
This commit is contained in:
Kyle Brandt 2025-09-03 13:25:44 -04:00 committed by GitHub
parent bbc401a6be
commit 00ab80a2f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 3 deletions

View File

@ -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{}

View File

@ -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{}
}