This repository was archived by the owner on Jul 13, 2026. It is now read-only.
forked from grafana/sqlds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
390 lines (338 loc) · 12 KB
/
Copy pathquery.go
File metadata and controls
390 lines (338 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package sqlds
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"runtime/debug"
"github.com/grafana/dataplane/sdata/timeseries"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
)
// FormatQueryOption defines how the user has chosen to represent the data
// Deprecated: use sqlutil.FormatQueryOption directly instead
type FormatQueryOption = sqlutil.FormatQueryOption
// Deprecated: use the values in sqlutil directly instead
const (
// FormatOptionTimeSeries formats the query results as a timeseries using "LongToWide"
FormatOptionTimeSeries = sqlutil.FormatOptionTimeSeries
// FormatOptionTable formats the query results as a table using "LongToWide"
FormatOptionTable = sqlutil.FormatOptionTable
// FormatOptionLogs sets the preferred visualization to logs
FormatOptionLogs = sqlutil.FormatOptionLogs
// FormatOptionsTrace sets the preferred visualization to trace
FormatOptionTrace = sqlutil.FormatOptionTrace
// FormatOptionMulti formats the query results as a timeseries using "LongToMulti"
FormatOptionMulti = sqlutil.FormatOptionMulti
)
// Deprecated: use sqlutil.Query directly instead
type Query = sqlutil.Query
// GetQuery wraps sqlutil's GetQuery to add headers if needed
func GetQuery(query backend.DataQuery, headers http.Header, setHeaders bool) (*Query, error) {
model, err := sqlutil.GetQuery(query)
if err != nil {
return nil, backend.PluginError(err)
}
if setHeaders {
applyHeaders(model, headers)
}
return model, nil
}
type DBQuery struct {
DB Connection
fillMode *data.FillMissing
Settings backend.DataSourceInstanceSettings
metrics Metrics
DSName string
converters []sqlutil.Converter
rowLimit int64
}
func NewQuery(db Connection, settings backend.DataSourceInstanceSettings, converters []sqlutil.Converter, fillMode *data.FillMissing, rowLimit int64) *DBQuery {
return &DBQuery{
DB: db,
DSName: settings.Name,
converters: converters,
fillMode: fillMode,
metrics: NewMetrics(settings.Name, settings.Type, EndpointQuery),
rowLimit: rowLimit,
}
}
// Run sends the query to the connection and converts the rows to a dataframe.
func (q *DBQuery) Run(ctx context.Context, query *Query, queryErrorMutator QueryErrorMutator, args ...interface{}) (data.Frames, error) {
start := time.Now()
rows, err := q.DB.QueryContext(ctx, query.RawSQL, args...)
if err != nil {
var errWithSource backend.ErrorWithSource
defer func() {
q.metrics.CollectDuration(Source(errWithSource.ErrorSource()), StatusError, time.Since(start).Seconds())
}()
if errors.Is(err, context.Canceled) {
errWithSource := backend.NewErrorWithSource(err, backend.ErrorSourceDownstream)
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
// Wrap with ErrorQuery to enable retry logic in datasource
queryErr := fmt.Errorf("%w: %w", ErrorQuery, err)
// Handle driver specific errors
if queryErrorMutator != nil {
errWithSource = queryErrorMutator.MutateQueryError(queryErr)
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
// If we get to this point, assume the error is from the plugin
errWithSource = backend.NewErrorWithSource(queryErr, backend.DefaultErrorSource)
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
q.metrics.CollectDuration(SourceDownstream, StatusOK, time.Since(start).Seconds())
// Check for an error response
if err := rows.Err(); err != nil {
queryErr := fmt.Errorf("%w: %w", ErrorQuery, err)
errWithSource := backend.NewErrorWithSource(queryErr, backend.DefaultErrorSource)
if errors.Is(err, sql.ErrNoRows) {
// Should we even response with an error here?
// The panel will simply show "no data"
errWithSource = backend.NewErrorWithSource(fmt.Errorf("%w: %s", err, "Error response from database"), backend.ErrorSourceDownstream)
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
if queryErrorMutator != nil {
errWithSource = queryErrorMutator.MutateQueryError(queryErr)
}
q.metrics.CollectDuration(Source(errWithSource.ErrorSource()), StatusError, time.Since(start).Seconds())
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}
defer func() {
if err := rows.Close(); err != nil {
backend.Logger.Error(err.Error())
}
}()
return q.convertRowsToFrames(rows, query, queryErrorMutator)
}
func (q *DBQuery) convertRowsToFrames(rows *sql.Rows, query *Query, queryErrorMutator QueryErrorMutator) (data.Frames, error) {
source := SourcePlugin
status := StatusOK
start := time.Now()
defer func() {
q.metrics.CollectDuration(source, status, time.Since(start).Seconds())
}()
res, err := getFrames(rows, q.rowLimit, q.converters, q.fillMode, query)
if err != nil {
status = StatusError
// Additional checks for processing errors
if backend.IsDownstreamHTTPError(err) {
source = SourceDownstream
} else if queryErrorMutator != nil {
errWithSource := queryErrorMutator.MutateQueryError(err)
source = Source(errWithSource.ErrorSource())
}
return sqlutil.ErrorFrameFromQuery(query), backend.NewErrorWithSource(
fmt.Errorf("%w: %s", err, "Could not process SQL results"),
backend.ErrorSource(source),
)
}
q.observeResponseSize(res)
return res, nil
}
// observeResponseSize records rows + cells (rows × fields) across all returned frames.
// Skips emission entirely if any frame has inconsistent field lengths, since partial
// totals would mislead operators investigating large responses.
func (q *DBQuery) observeResponseSize(frames data.Frames) {
var totalRows, totalCells int64
for _, frame := range frames {
if frame == nil {
continue
}
rowLen, err := frame.RowLen()
if err != nil {
backend.Logger.Debug("skipping response size observation", "error", err.Error())
return
}
totalRows += int64(rowLen)
totalCells += int64(rowLen) * int64(len(frame.Fields))
}
q.metrics.CollectResponseSize(totalRows, totalCells)
}
// getFrames converts rows to dataframes
func getFrames(rows *sql.Rows, limit int64, converters []sqlutil.Converter, fillMode *data.FillMissing, query *Query) (data.Frames, error) {
// Validate rows before processing to prevent panics
if err := validateRows(rows); err != nil {
backend.Logger.Error("Invalid SQL rows", "error", err.Error())
return nil, err
}
frame, err := sqlutil.FrameFromRows(rows, limit, converters...)
if err != nil {
return nil, err
}
frame.Name = query.RefID
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
count, err := frame.RowLen()
if err != nil {
return nil, err
}
// the handling of zero-rows differs between various "format"s.
zeroRows := count == 0
frame.Meta.ExecutedQueryString = query.RawSQL
frame.Meta.PreferredVisualization = data.VisTypeGraph
switch query.Format {
case FormatOptionMulti:
if zeroRows {
return nil, ErrorNoResults
}
if frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong {
err = fixFrameForLongToMulti(frame)
if err != nil {
return nil, err
}
frames, err := timeseries.LongToMulti(×eries.LongFrame{frame})
if err != nil {
return nil, err
}
return frames.Frames(), nil
}
case FormatOptionTable:
frame.Meta.PreferredVisualization = data.VisTypeTable
case FormatOptionLogs:
frame.Meta.PreferredVisualization = data.VisTypeLogs
case FormatOptionTrace:
frame.Meta.PreferredVisualization = data.VisTypeTrace
// Format as timeSeries
default:
if zeroRows {
return nil, ErrorNoResults
}
if frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong {
frame, err = data.LongToWide(frame, fillMode)
if err != nil {
return nil, err
}
}
}
return data.Frames{frame}, nil
}
// accessColumns checks whether we can access rows.Columns, checking
// for error or panic. In the case of panic, logs the stack trace at debug level
// for security
func accessColumns(rows *sql.Rows) (columnErr error) {
defer func() {
if r := recover(); r != nil {
columnErr = fmt.Errorf("panic accessing columns: %v", r)
stack := string(debug.Stack())
backend.Logger.Debug("accessColumns panic stack trace", "stack", stack)
}
}()
_, columnErr = rows.Columns()
return columnErr
}
// validateRows performs safety checks on SQL rows to prevent panics
func validateRows(rows *sql.Rows) error {
if rows == nil {
return fmt.Errorf("%w: rows is nil", ErrorRowValidation)
}
err := accessColumns(rows)
if err != nil {
return fmt.Errorf("%w: %w", ErrorRowValidation, err)
}
return nil
}
// fixFrameForLongToMulti edits the passed in frame so that it's first time field isn't nullable and has the correct meta
func fixFrameForLongToMulti(frame *data.Frame) error {
if frame == nil {
return fmt.Errorf("can not convert to wide series, input is nil")
}
timeFields := frame.TypeIndices(data.FieldTypeTime, data.FieldTypeNullableTime)
if len(timeFields) == 0 {
return fmt.Errorf("can not convert to wide series, input is missing a time field")
}
// the timeseries package expects the first time field in the frame to be non-nullable and ignores the rest
timeField := frame.Fields[timeFields[0]]
if timeField.Type() == data.FieldTypeNullableTime {
n := timeField.Len()
newValues := make([]time.Time, n)
for i := 0; i < n; i++ {
val, ok := timeField.ConcreteAt(i)
if !ok {
return fmt.Errorf("can not convert to wide series, input has null time values")
}
newValues[i] = val.(time.Time)
}
newField := data.NewField(timeField.Name, timeField.Labels, newValues)
newField.Config = timeField.Config
frame.Fields[timeFields[0]] = newField
// LongToMulti requires the meta to be set for the frame
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
frame.Meta.Type = data.FrameTypeTimeSeriesLong
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
}
return nil
}
func applyHeaders(query *Query, headers http.Header) *Query {
headerBytes, err := json.Marshal(headers)
if err != nil {
backend.Logger.Warn(fmt.Sprintf("Failed to apply headers: %s", err.Error()))
return query
}
if injected, ok := injectJSONKey(query.ConnectionArgs, HeaderKey, headerBytes); ok {
query.ConnectionArgs = injected
return query
}
return applyHeadersSlow(query, headerBytes)
}
// injectJSONKey appends `"key":<value>` into the trailing JSON object in `in`
// without decoding the rest of the object. It returns (newBytes, true) on
// success. It bails to (nil, false) when `in` isn't a plain JSON object or
// already contains `"key"` as a substring — callers should fall back to the
// slow path in those cases to preserve overwrite semantics.
func injectJSONKey(in []byte, key string, value []byte) ([]byte, bool) {
trimmed := bytes.TrimSpace(in)
if len(trimmed) == 0 {
trimmed = []byte(`{}`)
}
if len(trimmed) < 2 || trimmed[0] != '{' || trimmed[len(trimmed)-1] != '}' {
return nil, false
}
keyToken := make([]byte, 0, len(key)+2)
keyToken = append(keyToken, '"')
keyToken = append(keyToken, key...)
keyToken = append(keyToken, '"')
if bytes.Contains(trimmed, keyToken) {
return nil, false
}
body := bytes.TrimSpace(trimmed[1 : len(trimmed)-1])
buf := make([]byte, 0, len(body)+len(keyToken)+len(value)+4)
buf = append(buf, '{')
if len(body) > 0 {
buf = append(buf, body...)
buf = append(buf, ',')
}
buf = append(buf, keyToken...)
buf = append(buf, ':')
buf = append(buf, value...)
buf = append(buf, '}')
return buf, true
}
// applyHeadersSlow preserves the original decode/encode behaviour for edge
// cases the fast path rejects (malformed input or an existing HeaderKey).
func applyHeadersSlow(query *Query, headerBytes []byte) *Query {
var args map[string]any
if query.ConnectionArgs == nil {
query.ConnectionArgs = []byte("{}")
}
if err := json.Unmarshal(query.ConnectionArgs, &args); err != nil {
backend.Logger.Warn(fmt.Sprintf("Failed to apply headers: %s", err.Error()))
return query
}
args[HeaderKey] = json.RawMessage(headerBytes)
raw, err := json.Marshal(args)
if err != nil {
backend.Logger.Warn(fmt.Sprintf("Failed to apply headers: %s", err.Error()))
return query
}
query.ConnectionArgs = raw
return query
}