Share azureauth between prometheus clients (#58122)
* Move azureauth to upper package * Refactor http transport options
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
sdkHTTPClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
sdkHTTPClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
|
||||||
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
@@ -68,7 +69,7 @@ type Buffered struct {
|
|||||||
// New creates and object capable of executing and parsing a Prometheus queries. It's "buffered" because there is
|
// New creates and object capable of executing and parsing a Prometheus queries. It's "buffered" because there is
|
||||||
// another implementation capable of streaming parse the response.
|
// another implementation capable of streaming parse the response.
|
||||||
func New(roundTripper http.RoundTripper, tracer tracing.Tracer, settings backend.DataSourceInstanceSettings, plog log.Logger) (*Buffered, error) {
|
func New(roundTripper http.RoundTripper, tracer tracing.Tracer, settings backend.DataSourceInstanceSettings, plog log.Logger) (*Buffered, error) {
|
||||||
promClient, err := CreateClient(roundTripper, settings.URL)
|
promClient, err := client.CreateAPIClient(roundTripper, settings.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating prom client: %v", err)
|
return nil, fmt.Errorf("error creating prom client: %v", err)
|
||||||
}
|
}
|
||||||
@@ -232,7 +233,7 @@ func (b *Buffered) parseTimeSeriesQuery(req *backend.QueryDataRequest) ([]*Prome
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error unmarshaling query model: %v", err)
|
return nil, fmt.Errorf("error unmarshaling query model: %v", err)
|
||||||
}
|
}
|
||||||
//Final interval value
|
// Final interval value
|
||||||
interval, err := calculatePrometheusInterval(model, b.TimeInterval, query, b.intervalCalculator)
|
interval, err := calculatePrometheusInterval(model, b.TimeInterval, query, b.intervalCalculator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error calculating interval: %v", err)
|
return nil, fmt.Errorf("error calculating interval: %v", err)
|
||||||
@@ -301,7 +302,7 @@ func parseTimeSeriesResponse(value map[TimeSeriesQueryType]interface{}, query *P
|
|||||||
func calculatePrometheusInterval(model *QueryModel, timeInterval string, query backend.DataQuery, intervalCalculator intervalv2.Calculator) (time.Duration, error) {
|
func calculatePrometheusInterval(model *QueryModel, timeInterval string, query backend.DataQuery, intervalCalculator intervalv2.Calculator) (time.Duration, error) {
|
||||||
queryInterval := model.Interval
|
queryInterval := model.Interval
|
||||||
|
|
||||||
//If we are using variable for interval/step, we will replace it with calculated interval
|
// If we are using variable for interval/step, we will replace it with calculated interval
|
||||||
if isVariableInterval(queryInterval) {
|
if isVariableInterval(queryInterval) {
|
||||||
queryInterval = ""
|
queryInterval = ""
|
||||||
}
|
}
|
||||||
@@ -656,7 +657,7 @@ func isVariableInterval(interval string) bool {
|
|||||||
if interval == varInterval || interval == varIntervalMs || interval == varRateInterval {
|
if interval == varInterval || interval == varIntervalMs || interval == varRateInterval {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
//Repetitive code, we should have functionality to unify these
|
// Repetitive code, we should have functionality to unify these
|
||||||
if interval == varIntervalAlt || interval == varIntervalMsAlt || interval == varRateIntervalAlt {
|
if interval == varIntervalAlt || interval == varIntervalMsAlt || interval == varRateIntervalAlt {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package buffered
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/buffered/azureauth"
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
|
||||||
"github.com/grafana/grafana/pkg/util/maputil"
|
"github.com/grafana/grafana/pkg/util/maputil"
|
||||||
@@ -49,7 +49,7 @@ func CreateTransportOptions(settings backend.DataSourceInstanceSettings, cfg *se
|
|||||||
return &opts, nil
|
return &opts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateClient(roundTripper http.RoundTripper, url string) (apiv1.API, error) {
|
func CreateAPIClient(roundTripper http.RoundTripper, url string) (apiv1.API, error) {
|
||||||
cfg := api.Config{
|
cfg := api.Config{
|
||||||
Address: url,
|
Address: url,
|
||||||
RoundTripper: roundTripper,
|
RoundTripper: roundTripper,
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package buffered
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||||
"github.com/yudai/gojsondiff"
|
"github.com/yudai/gojsondiff"
|
||||||
@@ -53,7 +54,7 @@ func ProvideService(httpClientProvider httpclient.Provider, cfg *setting.Cfg, fe
|
|||||||
func newInstanceSettings(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) datasource.InstanceFactoryFunc {
|
func newInstanceSettings(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) datasource.InstanceFactoryFunc {
|
||||||
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||||
// Creates a http roundTripper. Probably should be used for both buffered and streaming/querydata instances.
|
// Creates a http roundTripper. Probably should be used for both buffered and streaming/querydata instances.
|
||||||
opts, err := buffered.CreateTransportOptions(settings, cfg, plog)
|
opts, err := client.CreateTransportOptions(settings, cfg, plog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating transport options: %v", err)
|
return nil, fmt.Errorf("error creating transport options: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
|
||||||
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||||
p "github.com/prometheus/common/model"
|
p "github.com/prometheus/common/model"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -21,7 +22,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/infra/log/logtest"
|
"github.com/grafana/grafana/pkg/infra/log/logtest"
|
||||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/buffered"
|
|
||||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/models"
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/models"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata"
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata"
|
||||||
)
|
)
|
||||||
@@ -417,7 +417,7 @@ func setup(wideFrames bool) (*testContext, error) {
|
|||||||
|
|
||||||
features := &fakeFeatureToggles{flags: map[string]bool{"prometheusStreamingJSONParser": true, "prometheusWideSeries": wideFrames}}
|
features := &fakeFeatureToggles{flags: map[string]bool{"prometheusStreamingJSONParser": true, "prometheusWideSeries": wideFrames}}
|
||||||
|
|
||||||
opts, err := buffered.CreateTransportOptions(settings, &setting.Cfg{}, &logtest.Fake{})
|
opts, err := client.CreateTransportOptions(settings, &setting.Cfg{}, &logtest.Fake{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user