mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
refactor promethueus package into sub packages (#43634)
This commit is contained in:
parent
567e2b5fd8
commit
c4c05a5b71
53
pkg/tsdb/prometheus/client/provider.go
Normal file
53
pkg/tsdb/prometheus/client/provider.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
|
||||||
|
|
||||||
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
|
"github.com/prometheus/client_golang/api"
|
||||||
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Create(url string, httpOpts sdkhttpclient.Options, clientProvider httpclient.Provider, jsonData map[string]interface{}, plog log.Logger) (apiv1.API, error) {
|
||||||
|
customParamsMiddleware := middleware.CustomQueryParameters(plog)
|
||||||
|
middlewares := []sdkhttpclient.Middleware{customParamsMiddleware}
|
||||||
|
if shouldForceGet(jsonData) {
|
||||||
|
middlewares = append(middlewares, middleware.ForceHttpGet(plog))
|
||||||
|
}
|
||||||
|
httpOpts.Middlewares = middlewares
|
||||||
|
|
||||||
|
roundTripper, err := clientProvider.GetTransport(httpOpts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := api.Config{
|
||||||
|
Address: url,
|
||||||
|
RoundTripper: roundTripper,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := api.NewClient(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return apiv1.NewAPI(client), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func shouldForceGet(settingsJson map[string]interface{}) bool {
|
||||||
|
methodInterface, exists := settingsJson["httpMethod"]
|
||||||
|
if !exists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
method, ok := methodInterface.(string)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.ToLower(method) == "get"
|
||||||
|
}
|
47
pkg/tsdb/prometheus/client/provider_test.go
Normal file
47
pkg/tsdb/prometheus/client/provider_test.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestForceGet(t *testing.T) {
|
||||||
|
t.Run("With nil jsonOpts, should not force get-method", func(t *testing.T) {
|
||||||
|
var jsonOpts map[string]interface{}
|
||||||
|
require.False(t, shouldForceGet(jsonOpts))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With empty jsonOpts, should not force get-method", func(t *testing.T) {
|
||||||
|
jsonOpts := make(map[string]interface{})
|
||||||
|
require.False(t, shouldForceGet(jsonOpts))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With httpMethod=nil, should not not force get-method", func(t *testing.T) {
|
||||||
|
jsonOpts := map[string]interface{}{
|
||||||
|
"httpMethod": nil,
|
||||||
|
}
|
||||||
|
require.False(t, shouldForceGet(jsonOpts))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With httpMethod=post, should not force get-method", func(t *testing.T) {
|
||||||
|
jsonOpts := map[string]interface{}{
|
||||||
|
"httpMethod": "POST",
|
||||||
|
}
|
||||||
|
require.False(t, shouldForceGet(jsonOpts))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With httpMethod=get, should force get-method", func(t *testing.T) {
|
||||||
|
jsonOpts := map[string]interface{}{
|
||||||
|
"httpMethod": "get",
|
||||||
|
}
|
||||||
|
require.True(t, shouldForceGet(jsonOpts))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With httpMethod=GET, should force get-method", func(t *testing.T) {
|
||||||
|
jsonOpts := map[string]interface{}{
|
||||||
|
"httpMethod": "GET",
|
||||||
|
}
|
||||||
|
require.True(t, shouldForceGet(jsonOpts))
|
||||||
|
})
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package prometheus
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -14,7 +14,7 @@ const (
|
|||||||
grafanaDataKey = "grafanaData"
|
grafanaDataKey = "grafanaData"
|
||||||
)
|
)
|
||||||
|
|
||||||
func customQueryParametersMiddleware(logger log.Logger) sdkhttpclient.Middleware {
|
func CustomQueryParameters(logger log.Logger) sdkhttpclient.Middleware {
|
||||||
return sdkhttpclient.NamedMiddlewareFunc(customQueryParametersMiddlewareName, func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
|
return sdkhttpclient.NamedMiddlewareFunc(customQueryParametersMiddlewareName, func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
|
||||||
grafanaData, exists := opts.CustomOptions[grafanaDataKey]
|
grafanaData, exists := opts.CustomOptions[grafanaDataKey]
|
||||||
if !exists {
|
if !exists {
|
@ -1,4 +1,4 @@
|
|||||||
package prometheus
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -19,7 +19,7 @@ func TestCustomQueryParametersMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Without custom query parameters set should not apply middleware", func(t *testing.T) {
|
t.Run("Without custom query parameters set should not apply middleware", func(t *testing.T) {
|
||||||
mw := customQueryParametersMiddleware(log.New("test"))
|
mw := CustomQueryParameters(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
||||||
require.NotNil(t, rt)
|
require.NotNil(t, rt)
|
||||||
middlewareName, ok := mw.(httpclient.MiddlewareName)
|
middlewareName, ok := mw.(httpclient.MiddlewareName)
|
||||||
@ -39,7 +39,7 @@ func TestCustomQueryParametersMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Without custom query parameters set as string should not apply middleware", func(t *testing.T) {
|
t.Run("Without custom query parameters set as string should not apply middleware", func(t *testing.T) {
|
||||||
mw := customQueryParametersMiddleware(log.New("test"))
|
mw := CustomQueryParameters(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{
|
rt := mw.CreateMiddleware(httpclient.Options{
|
||||||
CustomOptions: map[string]interface{}{
|
CustomOptions: map[string]interface{}{
|
||||||
customQueryParametersKey: 64,
|
customQueryParametersKey: 64,
|
||||||
@ -63,7 +63,7 @@ func TestCustomQueryParametersMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("With custom query parameters set as empty string should not apply middleware", func(t *testing.T) {
|
t.Run("With custom query parameters set as empty string should not apply middleware", func(t *testing.T) {
|
||||||
mw := customQueryParametersMiddleware(log.New("test"))
|
mw := CustomQueryParameters(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{
|
rt := mw.CreateMiddleware(httpclient.Options{
|
||||||
CustomOptions: map[string]interface{}{
|
CustomOptions: map[string]interface{}{
|
||||||
customQueryParametersKey: "",
|
customQueryParametersKey: "",
|
||||||
@ -87,7 +87,7 @@ func TestCustomQueryParametersMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("With custom query parameters set as invalid query string should not apply middleware", func(t *testing.T) {
|
t.Run("With custom query parameters set as invalid query string should not apply middleware", func(t *testing.T) {
|
||||||
mw := customQueryParametersMiddleware(log.New("test"))
|
mw := CustomQueryParameters(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{
|
rt := mw.CreateMiddleware(httpclient.Options{
|
||||||
CustomOptions: map[string]interface{}{
|
CustomOptions: map[string]interface{}{
|
||||||
customQueryParametersKey: "custom=%%abc&test=abc",
|
customQueryParametersKey: "custom=%%abc&test=abc",
|
||||||
@ -111,7 +111,7 @@ func TestCustomQueryParametersMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("With custom query parameters set should apply middleware for request URL containing query parameters ", func(t *testing.T) {
|
t.Run("With custom query parameters set should apply middleware for request URL containing query parameters ", func(t *testing.T) {
|
||||||
mw := customQueryParametersMiddleware(log.New("test"))
|
mw := CustomQueryParameters(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{
|
rt := mw.CreateMiddleware(httpclient.Options{
|
||||||
CustomOptions: map[string]interface{}{
|
CustomOptions: map[string]interface{}{
|
||||||
grafanaDataKey: map[string]interface{}{
|
grafanaDataKey: map[string]interface{}{
|
||||||
@ -143,7 +143,7 @@ func TestCustomQueryParametersMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("With custom query parameters set should apply middleware for request URL not containing query parameters", func(t *testing.T) {
|
t.Run("With custom query parameters set should apply middleware for request URL not containing query parameters", func(t *testing.T) {
|
||||||
mw := customQueryParametersMiddleware(log.New("test"))
|
mw := CustomQueryParameters(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{
|
rt := mw.CreateMiddleware(httpclient.Options{
|
||||||
CustomOptions: map[string]interface{}{
|
CustomOptions: map[string]interface{}{
|
||||||
grafanaDataKey: map[string]interface{}{
|
grafanaDataKey: map[string]interface{}{
|
@ -1,4 +1,4 @@
|
|||||||
package prometheus
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func forceHttpGetMiddleware(logger log.Logger) sdkhttpclient.Middleware {
|
func ForceHttpGet(logger log.Logger) sdkhttpclient.Middleware {
|
||||||
return sdkhttpclient.NamedMiddlewareFunc("force-http-get", func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
|
return sdkhttpclient.NamedMiddlewareFunc("force-http-get", func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
|
||||||
// the prometheus library we use does not allow us to set the http method.
|
// the prometheus library we use does not allow us to set the http method.
|
||||||
// it's behavior is to first try POST, and if it fails in certain ways
|
// it's behavior is to first try POST, and if it fails in certain ways
|
@ -1,4 +1,4 @@
|
|||||||
package prometheus
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -9,52 +9,12 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestForceGet(t *testing.T) {
|
|
||||||
t.Run("With nil jsonOpts, should not force get-method", func(t *testing.T) {
|
|
||||||
var jsonOpts map[string]interface{}
|
|
||||||
require.False(t, forceHttpGet(jsonOpts))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("With empty jsonOpts, should not force get-method", func(t *testing.T) {
|
|
||||||
jsonOpts := make(map[string]interface{})
|
|
||||||
require.False(t, forceHttpGet(jsonOpts))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("With httpMethod=nil, should not not force get-method", func(t *testing.T) {
|
|
||||||
jsonOpts := map[string]interface{}{
|
|
||||||
"httpMethod": nil,
|
|
||||||
}
|
|
||||||
require.False(t, forceHttpGet(jsonOpts))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("With httpMethod=post, should not force get-method", func(t *testing.T) {
|
|
||||||
jsonOpts := map[string]interface{}{
|
|
||||||
"httpMethod": "POST",
|
|
||||||
}
|
|
||||||
require.False(t, forceHttpGet(jsonOpts))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("With httpMethod=get, should force get-method", func(t *testing.T) {
|
|
||||||
jsonOpts := map[string]interface{}{
|
|
||||||
"httpMethod": "get",
|
|
||||||
}
|
|
||||||
require.True(t, forceHttpGet(jsonOpts))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("With httpMethod=GET, should force get-method", func(t *testing.T) {
|
|
||||||
jsonOpts := map[string]interface{}{
|
|
||||||
"httpMethod": "GET",
|
|
||||||
}
|
|
||||||
require.True(t, forceHttpGet(jsonOpts))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEnsureHttpMethodMiddleware(t *testing.T) {
|
func TestEnsureHttpMethodMiddleware(t *testing.T) {
|
||||||
t.Run("Name should be correct", func(t *testing.T) {
|
t.Run("Name should be correct", func(t *testing.T) {
|
||||||
finalRoundTripper := httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
finalRoundTripper := httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||||
})
|
})
|
||||||
mw := forceHttpGetMiddleware(log.New("test"))
|
mw := ForceHttpGet(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
||||||
require.NotNil(t, rt)
|
require.NotNil(t, rt)
|
||||||
middlewareName, ok := mw.(httpclient.MiddlewareName)
|
middlewareName, ok := mw.(httpclient.MiddlewareName)
|
||||||
@ -67,7 +27,7 @@ func TestEnsureHttpMethodMiddleware(t *testing.T) {
|
|||||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
mw := forceHttpGetMiddleware(log.New("test"))
|
mw := ForceHttpGet(log.New("test"))
|
||||||
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
||||||
require.NotNil(t, rt)
|
require.NotNil(t, rt)
|
||||||
|
|
@ -6,11 +6,11 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
|
||||||
|
|
||||||
"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"
|
||||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
||||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
@ -18,7 +18,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
|
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
|
||||||
"github.com/prometheus/client_golang/api"
|
|
||||||
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,24 +55,6 @@ func ProvideService(cfg *setting.Cfg, httpClientProvider httpclient.Provider, pl
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func forceHttpGet(settingsJson map[string]interface{}) bool {
|
|
||||||
methodInterface, exists := settingsJson["httpMethod"]
|
|
||||||
if !exists {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
method, ok := methodInterface.(string)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.ToLower(method) != "get" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.InstanceFactoryFunc {
|
func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.InstanceFactoryFunc {
|
||||||
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||||
jsonData := map[string]interface{}{}
|
jsonData := map[string]interface{}{}
|
||||||
@ -105,7 +86,7 @@ func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.Inst
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := createClient(settings.URL, httpCliOpts, httpClientProvider, forceHttpGet(jsonData))
|
client, err := client.Create(settings.URL, httpCliOpts, httpClientProvider, jsonData, plog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -143,32 +124,6 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createClient(url string, httpOpts sdkhttpclient.Options, clientProvider httpclient.Provider, forceHttpGet bool) (apiv1.API, error) {
|
|
||||||
customParamsMiddleware := customQueryParametersMiddleware(plog)
|
|
||||||
middlewares := []sdkhttpclient.Middleware{customParamsMiddleware}
|
|
||||||
if forceHttpGet {
|
|
||||||
middlewares = append(middlewares, forceHttpGetMiddleware(plog))
|
|
||||||
}
|
|
||||||
httpOpts.Middlewares = middlewares
|
|
||||||
|
|
||||||
roundTripper, err := clientProvider.GetTransport(httpOpts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg := api.Config{
|
|
||||||
Address: url,
|
|
||||||
RoundTripper: roundTripper,
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := api.NewClient(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return apiv1.NewAPI(client), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) getDSInfo(pluginCtx backend.PluginContext) (*DatasourceInfo, error) {
|
func (s *Service) getDSInfo(pluginCtx backend.PluginContext) (*DatasourceInfo, error) {
|
||||||
i, err := s.im.Get(pluginCtx)
|
i, err := s.im.Get(pluginCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user