2016-09-21 00:01:53 -05:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2020-11-26 06:38:36 -06:00
|
|
|
"errors"
|
2016-09-21 06:30:41 -05:00
|
|
|
"fmt"
|
2022-10-24 14:53:11 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-04 08:22:33 -06:00
|
|
|
|
2021-08-09 04:11:19 -05:00
|
|
|
"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/instancemgmt"
|
2021-05-19 16:53:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2022-04-01 06:26:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-05-12 04:34:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/buffered"
|
2022-11-07 12:20:00 -06:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
|
2022-05-13 13:28:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata"
|
2022-06-03 13:56:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/resource"
|
2022-11-07 12:20:00 -06:00
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
2021-10-15 06:37:27 -05:00
|
|
|
)
|
|
|
|
|
2022-05-12 04:34:09 -05:00
|
|
|
var plog = log.New("tsdb.prometheus")
|
2021-05-12 12:30:41 -05:00
|
|
|
|
2021-08-09 04:11:19 -05:00
|
|
|
type Service struct {
|
2022-05-13 13:28:54 -05:00
|
|
|
im instancemgmt.InstanceManager
|
|
|
|
features featuremgmt.FeatureToggles
|
2022-05-12 04:34:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type instance struct {
|
2022-10-24 14:53:11 -05:00
|
|
|
buffered *buffered.Buffered
|
|
|
|
queryData *querydata.QueryData
|
|
|
|
resource *resource.Resource
|
|
|
|
versionCache *cache.Cache
|
2021-08-09 04:11:19 -05:00
|
|
|
}
|
|
|
|
|
2022-04-01 06:26:49 -05:00
|
|
|
func ProvideService(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) *Service {
|
2021-08-09 04:11:19 -05:00
|
|
|
plog.Debug("initializing")
|
2022-01-20 11:16:22 -06:00
|
|
|
return &Service{
|
2022-05-13 13:28:54 -05:00
|
|
|
im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, cfg, features, tracer)),
|
|
|
|
features: features,
|
2021-08-25 08:11:22 -05:00
|
|
|
}
|
2017-02-06 08:22:09 -06:00
|
|
|
}
|
|
|
|
|
2022-05-12 04:34:09 -05:00
|
|
|
func newInstanceSettings(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) datasource.InstanceFactoryFunc {
|
2021-08-09 04:11:19 -05:00
|
|
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
2022-06-23 07:48:16 -05:00
|
|
|
// Creates a http roundTripper. Probably should be used for both buffered and streaming/querydata instances.
|
2022-11-03 13:44:37 -05:00
|
|
|
opts, err := client.CreateTransportOptions(settings, cfg, plog)
|
2021-05-27 05:43:21 -05:00
|
|
|
if err != nil {
|
2022-06-23 07:48:16 -05:00
|
|
|
return nil, fmt.Errorf("error creating transport options: %v", err)
|
|
|
|
}
|
2022-07-04 04:18:45 -05:00
|
|
|
httpClient, err := httpClientProvider.New(*opts)
|
2022-06-23 07:48:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating http client: %v", err)
|
2021-08-09 04:11:19 -05:00
|
|
|
}
|
2022-07-04 04:18:45 -05:00
|
|
|
// Older version using standard Go Prometheus client
|
|
|
|
b, err := buffered.New(httpClient.Transport, tracer, settings, plog)
|
2022-05-13 13:28:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:18:45 -05:00
|
|
|
// New version using custom client and better response parsing
|
|
|
|
qd, err := querydata.New(httpClient, features, tracer, settings, plog)
|
2022-02-03 10:06:31 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:18:45 -05:00
|
|
|
// Resource call management using new custom client same as querydata
|
|
|
|
r, err := resource.New(httpClient, settings, plog)
|
2022-06-03 13:56:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-12 04:34:09 -05:00
|
|
|
return instance{
|
2022-10-24 14:53:11 -05:00
|
|
|
buffered: b,
|
|
|
|
queryData: qd,
|
|
|
|
resource: r,
|
|
|
|
versionCache: cache.New(time.Minute*1, time.Minute*5),
|
2022-05-12 04:34:09 -05:00
|
|
|
}, nil
|
2021-08-09 04:11:19 -05:00
|
|
|
}
|
|
|
|
}
|
2021-05-19 16:53:41 -05:00
|
|
|
|
2021-08-09 04:11:19 -05:00
|
|
|
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
|
|
|
if len(req.Queries) == 0 {
|
|
|
|
return &backend.QueryDataResponse{}, fmt.Errorf("query contains no queries")
|
|
|
|
}
|
|
|
|
|
2022-05-12 04:34:09 -05:00
|
|
|
i, err := s.getInstance(req.PluginContext)
|
2021-08-09 04:11:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-13 03:22:26 -05:00
|
|
|
|
2022-11-11 10:53:12 -06:00
|
|
|
if s.features.IsEnabled(featuremgmt.FlagPrometheusBufferedClient) {
|
|
|
|
return i.buffered.ExecuteTimeSeriesQuery(ctx, req)
|
2022-05-13 13:28:54 -05:00
|
|
|
}
|
|
|
|
|
2022-11-11 10:53:12 -06:00
|
|
|
return i.queryData.Execute(ctx, req)
|
2021-08-09 04:11:19 -05:00
|
|
|
}
|
|
|
|
|
2022-06-03 13:56:13 -05:00
|
|
|
func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
|
|
|
i, err := s.getInstance(req.PluginContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-24 14:53:11 -05:00
|
|
|
if strings.EqualFold(req.Path, "version-detect") {
|
|
|
|
versionObj, found := i.versionCache.Get("version")
|
|
|
|
if found {
|
|
|
|
return sender.Send(versionObj.(*backend.CallResourceResponse))
|
|
|
|
}
|
|
|
|
|
|
|
|
vResp, err := i.resource.DetectVersion(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.versionCache.Set("version", vResp, cache.DefaultExpiration)
|
|
|
|
return sender.Send(vResp)
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:18:45 -05:00
|
|
|
resp, err := i.resource.Execute(ctx, req)
|
2022-06-03 13:56:13 -05:00
|
|
|
if err != nil {
|
2022-07-04 04:18:45 -05:00
|
|
|
return err
|
2022-06-03 13:56:13 -05:00
|
|
|
}
|
|
|
|
|
2022-07-04 04:18:45 -05:00
|
|
|
return sender.Send(resp)
|
2022-06-03 13:56:13 -05:00
|
|
|
}
|
|
|
|
|
2022-05-12 04:34:09 -05:00
|
|
|
func (s *Service) getInstance(pluginCtx backend.PluginContext) (*instance, error) {
|
2021-08-09 04:11:19 -05:00
|
|
|
i, err := s.im.Get(pluginCtx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-12 04:34:09 -05:00
|
|
|
in := i.(instance)
|
|
|
|
return &in, nil
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:05:49 -06:00
|
|
|
// IsAPIError returns whether err is or wraps a Prometheus error.
|
2020-11-26 06:38:36 -06:00
|
|
|
func IsAPIError(err error) bool {
|
2020-11-27 05:05:49 -06:00
|
|
|
// Check if the right error type is in err's chain.
|
2020-11-26 06:38:36 -06:00
|
|
|
var e *apiv1.Error
|
|
|
|
return errors.As(err, &e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertAPIError(err error) error {
|
|
|
|
var e *apiv1.Error
|
|
|
|
if errors.As(err, &e) {
|
|
|
|
return fmt.Errorf("%s: %s", e.Msg, e.Detail)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|