Skip to content

Commit fb3b302

Browse files
committed
DataSet api usage improvements
1 parent 7958a6c commit fb3b302

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.0.6
4+
5+
DataSet api usage improvements
6+
37
## 3.0.5
48

59
- Update yarn dependency @grafana/toolkit to 8.5.0

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go 1.16
55
require (
66
github.com/grafana/grafana-plugin-sdk-go v0.126.0
77
github.com/magefile/mage v1.12.1 // indirect
8+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
89
moul.io/http2curl v1.0.0 // indirect
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
446446
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
447447
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
448448
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
449+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af h1:Yx9k8YCG3dvF87UAn2tu2HQLf2dt/eR1bXxpLMWeH+Y=
450+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
449451
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
450452
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
451453
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sentinelone-dataset-datasource",
3-
"version": "3.0.5",
3+
"version": "3.0.6",
44
"description": "Scalyr Observability Platform",
55
"scripts": {
66
"build": "grafana-toolkit plugin:build",

pkg/plugin/client.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package plugin
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
89
"net/http"
910
"net/url"
1011
"time"
1112

13+
"golang.org/x/time/rate"
14+
1215
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
1316
)
1417

@@ -20,27 +23,38 @@ type FacetRequest struct {
2023
}
2124

2225
type DataSetClient struct {
23-
dataSetUrl string
24-
apiKey string
25-
netClient *http.Client
26+
dataSetUrl string
27+
apiKey string
28+
netClient *http.Client
29+
rateLimiter *rate.Limiter
2630
}
2731

2832
func NewDataSetClient(dataSetUrl string, apiKey string) *DataSetClient {
2933
// Consider using the backend.httpclient package provided by the Grafana SDK.
3034
// This would allow a per-instance configurable timeout, rather than the hardcoded value here.
31-
var netClient = &http.Client{
35+
netClient := &http.Client{
3236
Timeout: time.Second * 10,
3337
}
3438

39+
// TODO Are there alternate approaches to implementing rate limits via the Grafana SDK?
40+
// Consult with Grafana support about this, potentially there's a simplier option.
41+
rateLimiter := rate.NewLimiter(rate.Every(1 * time.Minute), 100) // 100 requests / minute
42+
3543
return &DataSetClient{
36-
dataSetUrl: dataSetUrl,
37-
apiKey: apiKey,
38-
netClient: netClient,
44+
dataSetUrl: dataSetUrl,
45+
apiKey: apiKey,
46+
netClient: netClient,
47+
rateLimiter: rateLimiter,
3948
}
4049
}
4150

4251
func (d *DataSetClient) newRequest(method, url string, body io.Reader) (*http.Request, error) {
43-
const VERSION = "3.0.5"
52+
const VERSION = "3.0.6"
53+
54+
if err := d.rateLimiter.Wait(context.Background()); err != nil {
55+
log.DefaultLogger.Error("error applying rate limiter", "err", err)
56+
return nil, err
57+
}
4458

4559
request, err := http.NewRequest(method, url, body)
4660
if err != nil {
@@ -83,6 +97,10 @@ func (d *DataSetClient) doPingRequest(req interface{}) (*LRQResult, error) {
8397

8498
var respBody LRQResult
8599
var token string
100+
101+
delay := 250 * time.Millisecond
102+
const maxDelay = 1 * time.Second
103+
86104
for i := 0; ; i++ {
87105
resp, err := d.netClient.Do(request)
88106
if err != nil {
@@ -115,7 +133,10 @@ func (d *DataSetClient) doPingRequest(req interface{}) (*LRQResult, error) {
115133
token = resp.Header.Get(TOKEN_HEADER)
116134
}
117135

118-
time.Sleep(100 * time.Millisecond)
136+
time.Sleep(delay)
137+
if delay < maxDelay {
138+
delay *= 2
139+
}
119140

120141
u := fmt.Sprintf("%s/v2/api/queries/%s?lastStepSeen=%d", d.dataSetUrl, respBody.Id, respBody.StepsCompleted)
121142
request, err = d.newRequest("GET", u, nil)

src/plugin.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"path": "img/DatasetConfig.png"
4444
}
4545
],
46-
"version": "3.0.5",
47-
"updated": "2022-08-02"
46+
"version": "3.0.6",
47+
"updated": "2022-10-04"
4848
},
4949
"dependencies": {
5050
"grafanaDependency": ">=8.2.0",

0 commit comments

Comments
 (0)