mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
b12b5ed92f
* Renames * Rename to phlareql * Go renames
78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
package parca
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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"
|
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
)
|
|
|
|
// Make sure ParcaDatasource implements required interfaces. This is important to do
|
|
// since otherwise we will only get a not implemented error response from plugin in
|
|
// runtime. In this example datasource instance implements backend.QueryDataHandler,
|
|
// backend.CheckHealthHandler, backend.StreamHandler interfaces. Plugin should not
|
|
// implement all these interfaces - only those which are required for a particular task.
|
|
// For example if plugin does not need streaming functionality then you are free to remove
|
|
// methods that implement backend.StreamHandler. Implementing instancemgmt.InstanceDisposer
|
|
// is useful to clean up resources used by previous datasource instance when a new datasource
|
|
// instance created upon datasource settings changed.
|
|
var (
|
|
_ backend.QueryDataHandler = (*Service)(nil)
|
|
_ backend.CallResourceHandler = (*Service)(nil)
|
|
_ backend.CheckHealthHandler = (*Service)(nil)
|
|
)
|
|
|
|
var logger = log.New("tsdb.parca")
|
|
|
|
type Service struct {
|
|
im instancemgmt.InstanceManager
|
|
}
|
|
|
|
func (s *Service) getInstance(pluginCtx backend.PluginContext) (*ParcaDatasource, error) {
|
|
i, err := s.im.Get(pluginCtx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
in := i.(*ParcaDatasource)
|
|
return in, nil
|
|
}
|
|
|
|
func ProvideService(httpClientProvider httpclient.Provider) *Service {
|
|
return &Service{
|
|
im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider)),
|
|
}
|
|
}
|
|
|
|
func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.InstanceFactoryFunc {
|
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
return NewParcaDatasource(httpClientProvider, settings)
|
|
}
|
|
}
|
|
|
|
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
|
i, err := s.getInstance(req.PluginContext)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return i.QueryData(ctx, req)
|
|
}
|
|
|
|
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
|
|
}
|
|
return i.CallResource(ctx, req, sender)
|
|
}
|
|
|
|
func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
|
i, err := s.getInstance(req.PluginContext)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return i.CheckHealth(ctx, req)
|
|
}
|