@@ -19,6 +19,7 @@ import (
19
19
)
20
20
21
21
const defaultKeySuffix = "default"
22
+ const defaultRowLimit = int64 (- 1 )
22
23
23
24
var (
24
25
ErrorMissingMultipleConnectionsConfig = backend .PluginError (errors .New ("received connection arguments but the feature is not enabled" ))
@@ -50,6 +51,10 @@ type SQLDatasource struct {
50
51
CustomRoutes map [string ]func (http.ResponseWriter , * http.Request )
51
52
metrics Metrics
52
53
EnableMultipleConnections bool
54
+ // EnableRowLimit: enables using the dataproxy.row_limit setting to limit the number of rows returned by the query
55
+ // https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/#row_limit
56
+ EnableRowLimit bool
57
+ rowLimit int64
53
58
// PreCheckHealth (optional). Performs custom health check before the Connect method
54
59
PreCheckHealth func (ctx context.Context , req * backend.CheckHealthRequest ) * backend.CheckHealthResult
55
60
// PostCheckHealth (optional).Performs custom health check after the Connect method
@@ -73,6 +78,17 @@ func (ds *SQLDatasource) NewDatasource(ctx context.Context, settings backend.Dat
73
78
ds .CallResourceHandler = httpadapter .New (mux )
74
79
ds .metrics = NewMetrics (settings .Name , settings .Type , EndpointQuery )
75
80
81
+ config := backend .GrafanaConfigFromContext (ctx )
82
+ ds .rowLimit = defaultRowLimit
83
+ if ds .EnableRowLimit && config != nil {
84
+ sqlConfig , err := config .SQL ()
85
+ if err != nil {
86
+ backend .Logger .Error (fmt .Sprintf ("failed setting row limit from sql config: %s" , err ))
87
+ } else {
88
+ ds .rowLimit = sqlConfig .RowLimit
89
+ }
90
+ }
91
+
76
92
return ds , nil
77
93
}
78
94
@@ -191,7 +207,7 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
191
207
// * Some datasources (snowflake) expire connections or have an authentication token that expires if not used in 1 or 4 hours.
192
208
// Because the datasource driver does not include an option for permanent connections, we retry the connection
193
209
// if the query fails. NOTE: this does not include some errors like "ErrNoRows"
194
- dbQuery := NewQuery (dbConn .db , dbConn .settings , ds .driver ().Converters (), fillMode )
210
+ dbQuery := NewQuery (dbConn .db , dbConn .settings , ds .driver ().Converters (), fillMode , ds . rowLimit )
195
211
res , err := dbQuery .Run (ctx , q , args ... )
196
212
if err == nil {
197
213
return res , nil
@@ -217,7 +233,7 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
217
233
time .Sleep (time .Duration (ds .DriverSettings ().Pause * int (time .Second )))
218
234
}
219
235
220
- dbQuery := NewQuery (db , dbConn .settings , ds .driver ().Converters (), fillMode )
236
+ dbQuery := NewQuery (db , dbConn .settings , ds .driver ().Converters (), fillMode , ds . rowLimit )
221
237
res , err = dbQuery .Run (ctx , q , args ... )
222
238
if err == nil {
223
239
return res , err
@@ -239,7 +255,7 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
239
255
continue
240
256
}
241
257
242
- dbQuery := NewQuery (db , dbConn .settings , ds .driver ().Converters (), fillMode )
258
+ dbQuery := NewQuery (db , dbConn .settings , ds .driver ().Converters (), fillMode , ds . rowLimit )
243
259
res , err = dbQuery .Run (ctx , q , args ... )
244
260
if err == nil {
245
261
return res , err
@@ -289,3 +305,7 @@ func (ds *SQLDatasource) errors(response *Response) error {
289
305
}
290
306
return err
291
307
}
308
+
309
+ func (ds * SQLDatasource ) GetRowLimit () int64 {
310
+ return ds .rowLimit
311
+ }
0 commit comments