@@ -7,19 +7,23 @@ import (
7
7
"errors"
8
8
"fmt"
9
9
"net/http"
10
+ "os"
11
+ "strconv"
10
12
"sync"
11
13
"time"
12
14
13
15
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
14
16
15
17
"github.com/grafana/grafana-plugin-sdk-go/backend"
16
18
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
19
+ "github.com/grafana/grafana-plugin-sdk-go/backend/log"
17
20
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
18
21
"github.com/grafana/grafana-plugin-sdk-go/data"
19
22
)
20
23
21
24
const defaultKeySuffix = "default"
22
25
const defaultRowLimit = int64 (- 1 )
26
+ const envRowLimit = "GF_DATAPROXY_ROW_LIMIT"
23
27
24
28
var (
25
29
ErrorMissingMultipleConnectionsConfig = backend .PluginError (errors .New ("received connection arguments but the feature is not enabled" ))
@@ -68,6 +72,7 @@ func (ds *SQLDatasource) NewDatasource(ctx context.Context, settings backend.Dat
68
72
if err != nil {
69
73
return nil , backend .DownstreamError (err )
70
74
}
75
+
71
76
ds .connector = conn
72
77
mux := http .NewServeMux ()
73
78
err = ds .registerRoutes (mux )
@@ -78,16 +83,7 @@ func (ds *SQLDatasource) NewDatasource(ctx context.Context, settings backend.Dat
78
83
ds .CallResourceHandler = httpadapter .New (mux )
79
84
ds .metrics = NewMetrics (settings .Name , settings .Type , EndpointQuery )
80
85
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
- }
86
+ ds .rowLimit = ds .newRowLimit (ctx , conn )
91
87
92
88
return ds , nil
93
89
}
@@ -309,3 +305,54 @@ func (ds *SQLDatasource) errors(response *Response) error {
309
305
func (ds * SQLDatasource ) GetRowLimit () int64 {
310
306
return ds .rowLimit
311
307
}
308
+
309
+ func (ds * SQLDatasource ) SetDefaultRowLimit (limit int64 ) {
310
+ ds .EnableRowLimit = true
311
+ ds .rowLimit = limit
312
+ }
313
+
314
+ // newRowLimit returns the row limit for the datasource
315
+ // It checks in the following order:
316
+ // 1. set in the datasource configuration page
317
+ // 2. set via the environment variable
318
+ // 3. set is set on grafana_ini and passed via grafana context
319
+ // 4. default row limit set by SetDefaultRowLimit
320
+ func (ds * SQLDatasource ) newRowLimit (ctx context.Context , conn * Connector ) int64 {
321
+ if ! ds .EnableRowLimit {
322
+ return defaultRowLimit
323
+ }
324
+
325
+ // Handles when row limit is set in the datasource configuration page
326
+ settingsLimit := conn .driverSettings .RowLimit
327
+ if settingsLimit != 0 {
328
+ return settingsLimit
329
+ }
330
+
331
+ // Handles when row limit is set via environment variable
332
+ envLimit := os .Getenv (envRowLimit )
333
+ if envLimit != "" {
334
+ l , err := strconv .ParseInt (envLimit , 10 , 64 )
335
+ if err == nil && l >= 0 {
336
+ return l
337
+ }
338
+ log .DefaultLogger .Error (fmt .Sprintf ("failed setting row limit from environment variable: %s" , err ))
339
+ }
340
+
341
+ // Handles row limit from sql config from grafana instance
342
+ config := backend .GrafanaConfigFromContext (ctx )
343
+ if ds .EnableRowLimit && config != nil {
344
+ sqlConfig , err := config .SQL ()
345
+ if err != nil {
346
+ backend .Logger .Error (fmt .Sprintf ("failed setting row limit from sql config: %s" , err ))
347
+ } else {
348
+ return sqlConfig .RowLimit
349
+ }
350
+ }
351
+
352
+ // handles SetDefaultRowLimit where it is set before the datasource is initialized
353
+ if ds .rowLimit != 0 {
354
+ return ds .rowLimit
355
+ }
356
+
357
+ return defaultRowLimit
358
+ }
0 commit comments