use count_bytes_reader from plugin-sdk-go (#92553)

* use count_bytes_reader from plugin-sdk-go

* run `make update-workspace`

* update postgres tests

* update mysql tests

* time back to utc

* make update-workspace done

---------

Co-authored-by: Kyle Brandt <kyle@grafana.com>
This commit is contained in:
Gábor Farkas
2024-08-28 16:44:13 +02:00
committed by GitHub
co-authored by Kyle Brandt
parent 09f102b72e
commit abca0380a8
24 changed files with 49 additions and 116 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ go 1.23.0
require (
github.com/emicklei/go-restful/v3 v3.11.0
github.com/grafana/grafana-plugin-sdk-go v0.244.0
github.com/grafana/grafana-plugin-sdk-go v0.245.0
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20240808213237-f4d2e064f435
github.com/grafana/grafana/pkg/semconv v0.0.0-20240808213237-f4d2e064f435
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38
+2 -2
View File
@@ -130,8 +130,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/grafana-plugin-sdk-go v0.244.0 h1:ZZxHbiiF6QcsnlbPFyZGmzNDoTC1pLeHXUQYoskWt5c=
github.com/grafana/grafana-plugin-sdk-go v0.244.0/go.mod h1:H3FXrJMUlwocQ6UYj8Ds5I9EzRAVOcdRcgaRE3mXQqk=
github.com/grafana/grafana-plugin-sdk-go v0.245.0 h1:2KCKA86//O20ffL6WKzHGx5scBbdV7GyEFGnH8Hdv7M=
github.com/grafana/grafana-plugin-sdk-go v0.245.0/go.mod h1:1X8Kgo/SK91Qo1WBCKjPSKrfgjpQys1OkQsHhA78TLg=
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20240808213237-f4d2e064f435 h1:lmw60EW7JWlAEvgggktOyVkH4hF1m/+LSF/Ap0NCyi8=
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20240808213237-f4d2e064f435/go.mod h1:ORVFiW/KNRY52lNjkGwnFWCxNVfE97bJG2jr2fetq0I=
github.com/grafana/grafana/pkg/semconv v0.0.0-20240808213237-f4d2e064f435 h1:SNEeqY22DrGr5E9kGF1mKSqlOom14W9+b1u4XEGJowA=
@@ -1,39 +0,0 @@
package httpclient
import (
"io"
)
type CloseCallbackFunc func(bytesRead int64)
// CountBytesReader counts the total amount of bytes read from the underlying reader.
//
// The provided callback func will be called before the underlying reader is closed.
func CountBytesReader(reader io.ReadCloser, callback CloseCallbackFunc) io.ReadCloser {
if reader == nil {
panic("reader cannot be nil")
}
if callback == nil {
panic("callback cannot be nil")
}
return &countBytesReader{reader: reader, callback: callback}
}
type countBytesReader struct {
reader io.ReadCloser
callback CloseCallbackFunc
counter int64
}
func (r *countBytesReader) Read(p []byte) (int, error) {
n, err := r.reader.Read(p)
r.counter += int64(n)
return n, err
}
func (r *countBytesReader) Close() error {
r.callback(r.counter)
return r.reader.Close()
}
@@ -1,38 +0,0 @@
package httpclient
import (
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestCountBytesReader(t *testing.T) {
tcs := []struct {
body string
expectedBytesCount int64
}{
{body: "d", expectedBytesCount: 1},
{body: "dummy", expectedBytesCount: 5},
}
for index, tc := range tcs {
t.Run(fmt.Sprintf("Test CountBytesReader %d", index), func(t *testing.T) {
body := io.NopCloser(strings.NewReader(tc.body))
var actualBytesRead int64
readCloser := CountBytesReader(body, func(bytesRead int64) {
actualBytesRead = bytesRead
})
bodyBytes, err := io.ReadAll(readCloser)
require.NoError(t, err)
err = readCloser.Close()
require.NoError(t, err)
require.Equal(t, tc.expectedBytesCount, actualBytesRead)
require.Equal(t, string(bodyBytes), tc.body)
})
}
}
@@ -6,7 +6,6 @@ import (
"time"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/infra/metrics/metricutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -113,7 +112,7 @@ func executeMiddleware(next http.RoundTripper, labels prometheus.Labels) http.Ro
}
if res != nil && res.StatusCode != http.StatusSwitchingProtocols {
res.Body = httpclient.CountBytesReader(res.Body, func(bytesRead int64) {
res.Body = sdkhttpclient.CountBytesReader(res.Body, func(bytesRead int64) {
responseSizeHistogram.Observe(float64(bytesRead))
})
}
+1 -1
View File
@@ -4,7 +4,7 @@ go 1.23.0
require (
github.com/grafana/dskit v0.0.0-20240805174438-dfa83b4ed2d3
github.com/grafana/grafana-plugin-sdk-go v0.244.0
github.com/grafana/grafana-plugin-sdk-go v0.245.0
github.com/json-iterator/go v1.1.12
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.20.0
+2 -2
View File
@@ -98,8 +98,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/grafana/dskit v0.0.0-20240805174438-dfa83b4ed2d3 h1:as4PmrFoYI1byS5JjsgPC7uSGTMh+SgS0ePv6hOyDGU=
github.com/grafana/dskit v0.0.0-20240805174438-dfa83b4ed2d3/go.mod h1:lcjGB6SuaZ2o44A9nD6p/tR4QXSPbzViRY520Gy6pTQ=
github.com/grafana/grafana-plugin-sdk-go v0.244.0 h1:ZZxHbiiF6QcsnlbPFyZGmzNDoTC1pLeHXUQYoskWt5c=
github.com/grafana/grafana-plugin-sdk-go v0.244.0/go.mod h1:H3FXrJMUlwocQ6UYj8Ds5I9EzRAVOcdRcgaRE3mXQqk=
github.com/grafana/grafana-plugin-sdk-go v0.245.0 h1:2KCKA86//O20ffL6WKzHGx5scBbdV7GyEFGnH8Hdv7M=
github.com/grafana/grafana-plugin-sdk-go v0.245.0/go.mod h1:1X8Kgo/SK91Qo1WBCKjPSKrfgjpQys1OkQsHhA78TLg=
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=
github.com/grafana/otel-profiling-go v0.5.1/go.mod h1:ftN/t5A/4gQI19/8MoWurBEtC6gFw8Dns1sJZ9W4Tls=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg=
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -33,7 +33,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -30,7 +30,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -30,7 +30,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT floor(extract(epoch from \"time\")/300)*300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
// }
@@ -35,7 +35,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT floor(extract(epoch from \"time\")/300)*300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT floor(extract(epoch from \"time\")/300)*300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
// }
@@ -35,7 +35,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT floor(extract(epoch from \"time\")/300)*300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT floor(extract(epoch from \"time\")/300)*300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
// }
@@ -35,7 +35,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT floor(extract(epoch from \"time\")/300)*300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -33,7 +33,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -33,7 +33,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -30,7 +30,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -30,7 +30,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},
+2 -2
View File
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT UNIX_TIMESTAMP(\"time\") DIV 300 * 300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
// }
@@ -35,7 +35,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT UNIX_TIMESTAMP(\"time\") DIV 300 * 300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT UNIX_TIMESTAMP(\"time\") DIV 300 * 300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
// }
@@ -35,7 +35,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT UNIX_TIMESTAMP(\"time\") DIV 300 * 300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
},
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT UNIX_TIMESTAMP(\"time\") DIV 300 * 300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
// }
@@ -35,7 +35,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT UNIX_TIMESTAMP(\"time\") DIV 300 * 300 AS \"time\",c,avg(v) AS \"v\" FROM tbl GROUP BY 1,2 ORDER BY 1,2"
},
+2 -2
View File
@@ -4,7 +4,7 @@
// "type": "timeseries-wide",
// "typeVersion": [
// 0,
// 0
// 1
// ],
// "executedQueryString": "SELECT * FROM tbl"
// }
@@ -33,7 +33,7 @@
"type": "timeseries-wide",
"typeVersion": [
0,
0
1
],
"executedQueryString": "SELECT * FROM tbl"
},