Skip to content

Commit 0367f61

Browse files
authored
Share azureauth between prometheus clients (grafana#58122)
* Move azureauth to upper package * Refactor http transport options
1 parent 1722000 commit 0367f61

File tree

7 files changed

+13
-11
lines changed

7 files changed

+13
-11
lines changed

pkg/tsdb/prometheus/buffered/time_series_query.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/grafana/grafana-plugin-sdk-go/backend"
1616
sdkHTTPClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
1717
"github.com/grafana/grafana-plugin-sdk-go/data"
18+
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
1819
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
1920
"github.com/prometheus/common/model"
2021
"go.opentelemetry.io/otel/attribute"
@@ -68,7 +69,7 @@ type Buffered struct {
6869
// New creates and object capable of executing and parsing a Prometheus queries. It's "buffered" because there is
6970
// another implementation capable of streaming parse the response.
7071
func New(roundTripper http.RoundTripper, tracer tracing.Tracer, settings backend.DataSourceInstanceSettings, plog log.Logger) (*Buffered, error) {
71-
promClient, err := CreateClient(roundTripper, settings.URL)
72+
promClient, err := client.CreateAPIClient(roundTripper, settings.URL)
7273
if err != nil {
7374
return nil, fmt.Errorf("error creating prom client: %v", err)
7475
}
@@ -232,7 +233,7 @@ func (b *Buffered) parseTimeSeriesQuery(req *backend.QueryDataRequest) ([]*Prome
232233
if err != nil {
233234
return nil, fmt.Errorf("error unmarshaling query model: %v", err)
234235
}
235-
//Final interval value
236+
// Final interval value
236237
interval, err := calculatePrometheusInterval(model, b.TimeInterval, query, b.intervalCalculator)
237238
if err != nil {
238239
return nil, fmt.Errorf("error calculating interval: %v", err)
@@ -301,7 +302,7 @@ func parseTimeSeriesResponse(value map[TimeSeriesQueryType]interface{}, query *P
301302
func calculatePrometheusInterval(model *QueryModel, timeInterval string, query backend.DataQuery, intervalCalculator intervalv2.Calculator) (time.Duration, error) {
302303
queryInterval := model.Interval
303304

304-
//If we are using variable for interval/step, we will replace it with calculated interval
305+
// If we are using variable for interval/step, we will replace it with calculated interval
305306
if isVariableInterval(queryInterval) {
306307
queryInterval = ""
307308
}
@@ -656,7 +657,7 @@ func isVariableInterval(interval string) bool {
656657
if interval == varInterval || interval == varIntervalMs || interval == varRateInterval {
657658
return true
658659
}
659-
//Repetitive code, we should have functionality to unify these
660+
// Repetitive code, we should have functionality to unify these
660661
if interval == varIntervalAlt || interval == varIntervalMsAlt || interval == varRateIntervalAlt {
661662
return true
662663
}

pkg/tsdb/prometheus/buffered/client.go renamed to pkg/tsdb/prometheus/client/transport.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package buffered
1+
package client
22

33
import (
44
"fmt"
@@ -9,7 +9,7 @@ import (
99
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
1010
"github.com/grafana/grafana/pkg/infra/log"
1111
"github.com/grafana/grafana/pkg/setting"
12-
"github.com/grafana/grafana/pkg/tsdb/prometheus/buffered/azureauth"
12+
"github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth"
1313
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
1414
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
1515
"github.com/grafana/grafana/pkg/util/maputil"
@@ -49,7 +49,7 @@ func CreateTransportOptions(settings backend.DataSourceInstanceSettings, cfg *se
4949
return &opts, nil
5050
}
5151

52-
func CreateClient(roundTripper http.RoundTripper, url string) (apiv1.API, error) {
52+
func CreateAPIClient(roundTripper http.RoundTripper, url string) (apiv1.API, error) {
5353
cfg := api.Config{
5454
Address: url,
5555
RoundTripper: roundTripper,

pkg/tsdb/prometheus/buffered/client_test.go renamed to pkg/tsdb/prometheus/client/transport_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package buffered
1+
package client
22

33
import (
44
"testing"

pkg/tsdb/prometheus/prometheus.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/grafana/grafana-plugin-sdk-go/backend"
1414
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
1515
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
16+
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
1617
"github.com/patrickmn/go-cache"
1718
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
1819
"github.com/yudai/gojsondiff"
@@ -53,7 +54,7 @@ func ProvideService(httpClientProvider httpclient.Provider, cfg *setting.Cfg, fe
5354
func newInstanceSettings(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) datasource.InstanceFactoryFunc {
5455
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
5556
// Creates a http roundTripper. Probably should be used for both buffered and streaming/querydata instances.
56-
opts, err := buffered.CreateTransportOptions(settings, cfg, plog)
57+
opts, err := client.CreateTransportOptions(settings, cfg, plog)
5758
if err != nil {
5859
return nil, fmt.Errorf("error creating transport options: %v", err)
5960
}

pkg/tsdb/prometheus/querydata/request_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/grafana/grafana-plugin-sdk-go/backend"
1414
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
1515
"github.com/grafana/grafana-plugin-sdk-go/data"
16+
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
1617
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
1718
p "github.com/prometheus/common/model"
1819
"github.com/stretchr/testify/require"
@@ -21,7 +22,6 @@ import (
2122
"github.com/grafana/grafana/pkg/infra/log/logtest"
2223
"github.com/grafana/grafana/pkg/infra/tracing"
2324
"github.com/grafana/grafana/pkg/setting"
24-
"github.com/grafana/grafana/pkg/tsdb/prometheus/buffered"
2525
"github.com/grafana/grafana/pkg/tsdb/prometheus/models"
2626
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata"
2727
)
@@ -417,7 +417,7 @@ func setup(wideFrames bool) (*testContext, error) {
417417

418418
features := &fakeFeatureToggles{flags: map[string]bool{"prometheusStreamingJSONParser": true, "prometheusWideSeries": wideFrames}}
419419

420-
opts, err := buffered.CreateTransportOptions(settings, &setting.Cfg{}, &logtest.Fake{})
420+
opts, err := client.CreateTransportOptions(settings, &setting.Cfg{}, &logtest.Fake{})
421421
if err != nil {
422422
return nil, err
423423
}

0 commit comments

Comments
 (0)