2021-03-08 00:02:49 -06:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2021-06-03 07:16:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2021-07-07 01:54:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/oauthtoken"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudmonitoring"
|
2021-09-07 02:35:37 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/tsdb/postgres"
|
2021-03-08 00:02:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewService returns a new Service.
|
2021-08-25 08:11:22 -05:00
|
|
|
func NewService(
|
2021-09-10 09:44:47 -05:00
|
|
|
cfg *setting.Cfg,
|
|
|
|
pluginManager plugins.Manager,
|
|
|
|
backendPluginManager backendplugin.Manager,
|
|
|
|
oauthTokenService *oauthtoken.Service,
|
|
|
|
cloudMonitoringService *cloudmonitoring.Service,
|
2021-08-25 08:11:22 -05:00
|
|
|
) *Service {
|
|
|
|
s := newService(cfg, pluginManager, backendPluginManager, oauthTokenService)
|
|
|
|
|
|
|
|
// register backend data sources using legacy plugin
|
|
|
|
// contracts/non-SDK contracts
|
|
|
|
s.registry["stackdriver"] = cloudMonitoringService.NewExecutor
|
|
|
|
|
|
|
|
return s
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
func newService(cfg *setting.Cfg, manager plugins.Manager, backendPluginManager backendplugin.Manager,
|
|
|
|
oauthTokenService oauthtoken.OAuthTokenService) *Service {
|
|
|
|
return &Service{
|
|
|
|
Cfg: cfg,
|
|
|
|
PluginManager: manager,
|
|
|
|
BackendPluginManager: backendPluginManager,
|
|
|
|
// nolint:staticcheck // plugins.DataPlugin deprecated
|
|
|
|
registry: map[string]func(*models.DataSource) (plugins.DataPlugin, error){},
|
|
|
|
OAuthTokenService: oauthTokenService,
|
|
|
|
}
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Service handles data requests to data sources.
|
|
|
|
type Service struct {
|
2021-08-25 08:11:22 -05:00
|
|
|
Cfg *setting.Cfg
|
|
|
|
PluginManager plugins.Manager
|
|
|
|
BackendPluginManager backendplugin.Manager
|
|
|
|
OAuthTokenService oauthtoken.OAuthTokenService
|
2021-04-22 20:03:11 -05:00
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
2021-03-08 00:02:49 -06:00
|
|
|
registry map[string]func(*models.DataSource) (plugins.DataPlugin, error)
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:03:11 -05:00
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
2021-06-03 07:16:58 -05:00
|
|
|
func (s *Service) HandleRequest(ctx context.Context, ds *models.DataSource, query plugins.DataQuery) (plugins.DataResponse, error) {
|
|
|
|
if factory, exists := s.registry[ds.Type]; exists {
|
2021-03-08 00:02:49 -06:00
|
|
|
var err error
|
2021-06-03 07:16:58 -05:00
|
|
|
plugin, err := factory(ds)
|
2021-03-08 00:02:49 -06:00
|
|
|
if err != nil {
|
2021-08-25 08:11:22 -05:00
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
2021-03-08 00:02:49 -06:00
|
|
|
return plugins.DataResponse{}, fmt.Errorf("could not instantiate endpoint for data plugin %q: %w",
|
|
|
|
ds.Type, err)
|
|
|
|
}
|
2021-06-03 07:16:58 -05:00
|
|
|
|
|
|
|
return plugin.DataQuery(ctx, ds, query)
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|
2021-07-07 01:54:17 -05:00
|
|
|
return dataPluginQueryAdapter(ds.Type, s.BackendPluginManager, s.OAuthTokenService).DataQuery(ctx, ds, query)
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterQueryHandler registers a query handler factory.
|
|
|
|
// This is only exposed for tests!
|
2021-04-22 20:03:11 -05:00
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
2021-03-08 00:02:49 -06:00
|
|
|
func (s *Service) RegisterQueryHandler(name string, factory func(*models.DataSource) (plugins.DataPlugin, error)) {
|
|
|
|
s.registry[name] = factory
|
|
|
|
}
|