Documentation ¶
Overview ¶
Package datasource provides utilities for creating and serving a data source plugin over gRPC.
Example ¶
package main import ( "context" "net/http" "os" "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/httpclient" "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" "github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter" ) type testDataSource struct { httpClient *http.Client backend.CallResourceHandler } func newDataSource(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { opts, err := settings.HTTPClientOptions(ctx) if err != nil { return nil, err } client, err := httpclient.New(opts) if err != nil { return nil, err } ds := &testDataSource{ httpClient: client, } mux := http.NewServeMux() mux.HandleFunc("/test", ds.handleTest) ds.CallResourceHandler = httpadapter.New(mux) return ds, nil } func (ds *testDataSource) Dispose() { // Cleanup } func (ds *testDataSource) CheckHealth(_ context.Context, _ *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { // Handle request resp, err := ds.httpClient.Get("http://") if err != nil { return nil, err } resp.Body.Close() return nil, nil } func (ds *testDataSource) QueryData(_ context.Context, _ *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { var resp *backend.QueryDataResponse // Handle request httpResp, err := ds.httpClient.Get("http://") if err != nil { return nil, err } httpResp.Body.Close() return resp, err } func (ds *testDataSource) handleTest(rw http.ResponseWriter, _ *http.Request) { // Handle request resp, err := ds.httpClient.Get("http://") if err != nil { rw.WriteHeader(500) return } resp.Body.Close() } func main() { err := datasource.Manage("myds-plugin-id", newDataSource, datasource.ManageOpts{}) if err != nil { backend.Logger.Error(err.Error()) os.Exit(1) } }
Output:
Index ¶
- func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpts) error
- func NewInstanceManager(fn InstanceFactoryFunc) instancemgmt.InstanceManager
- func NewInstanceProvider(fn InstanceFactoryFunc) instancemgmt.InstanceProvider
- func Serve(opts ServeOpts) errordeprecated
- type InstanceFactoryFunc
- type ManageOpts
- type QueryTypeMux
- type ServeOptsdeprecated
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Manage ¶ added in v0.97.0
func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpts) error
Manage starts serving the data source over gPRC with automatic instance management. pluginID should match the one from plugin.json.
func NewInstanceManager ¶ added in v0.61.0
func NewInstanceManager(fn InstanceFactoryFunc) instancemgmt.InstanceManager
NewInstanceManager creates a new data source instance manager,
This is a helper method for calling NewInstanceProvider and creating a new instancemgmt.InstanceProvider, and providing that to instancemgmt.New.
func NewInstanceProvider ¶ added in v0.61.0
func NewInstanceProvider(fn InstanceFactoryFunc) instancemgmt.InstanceProvider
NewInstanceProvider create a new data source instance provuder,
The instance provider is responsible for providing cache keys for data source instances, creating new instances when needed and invalidating cached instances when they have been updated in Grafana. Cache key is based on the numerical data source identifier. If fn is nil, NewInstanceProvider panics.
Types ¶
type InstanceFactoryFunc ¶ added in v0.61.0
type InstanceFactoryFunc func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error)
InstanceFactoryFunc factory method for creating data source instances.
type ManageOpts ¶ added in v0.97.0
type ManageOpts struct { // GRPCSettings settings for gPRC. GRPCSettings backend.GRPCSettings // TracingOpts contains settings for tracing setup. TracingOpts tracing.Opts // Stateless admission handler AdmissionHandler backend.AdmissionHandler // Stateless conversion handler ConversionHandler backend.ConversionHandler // Stateless query conversion handler QueryConversionHandler backend.QueryConversionHandler }
ManageOpts can modify Manage behavior.
type QueryTypeMux ¶
type QueryTypeMux struct {
// contains filtered or unexported fields
}
QueryTypeMux is a query type multiplexer.
Example ¶
package main import ( "context" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" ) func main() { mux := datasource.NewQueryTypeMux() mux.HandleFunc("queryTypeA", func(_ context.Context, _ *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { // handle queryTypeA return nil, nil }) mux.HandleFunc("queryTypeB", func(_ context.Context, _ *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { // handle queryTypeB return nil, nil }) _ = datasource.ServeOpts{ QueryDataHandler: mux, } }
Output:
func NewQueryTypeMux ¶
func NewQueryTypeMux() *QueryTypeMux
NewQueryTypeMux allocates and returns a new QueryTypeMux.
func (*QueryTypeMux) Handle ¶
func (mux *QueryTypeMux) Handle(queryType string, handler backend.QueryDataHandler)
Handle registers the handler for the given query type.
Providing an empty queryType registers the handler as a fallback handler that will be called when query type doesn't match any registered handlers. If handler is nil, Handle panics. If a handler already exists for queryType, Handle panics.
func (*QueryTypeMux) HandleFunc ¶
func (mux *QueryTypeMux) HandleFunc(queryType string, handler func(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error))
HandleFunc registers the handler function for the given query type.
Providing an empty queryType registers the handler as a fallback handler that will be called when query type doesn't match any registered handlers. If handler is nil, Handle panics. If a handler already exists for queryType, Handle panics.
func (*QueryTypeMux) QueryData ¶
func (mux *QueryTypeMux) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error)
QueryData dispatches the request to the handler(s) whose query type matches the request queries query type.
type ServeOpts
deprecated
added in
v0.61.0
type ServeOpts struct { // CheckHealthHandler handler for health checks. // Optional to implement. backend.CheckHealthHandler // CallResourceHandler handler for resource calls. // Optional to implement. backend.CallResourceHandler // QueryDataHandler handler for data queries. // Required to implement. backend.QueryDataHandler // StreamHandler for streaming queries. backend.StreamHandler // AdmissionHandler for processing storage requests backend.AdmissionHandler // ConversionHandler for converting objects between resource versions backend.ConversionHandler // GRPCSettings settings for gPRC. GRPCSettings backend.GRPCSettings }
ServeOpts options for serving a data source plugin.
Deprecated: ServeOpts exists for historical compatibility and might be removed in a future version. Please migrate to use Manage instead of Serve.