mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Add backend target to instrumentation (#61980)
* add target * fix test
This commit is contained in:
parent
6e126596e0
commit
958eea2f78
@ -64,6 +64,10 @@ func (cp *corePlugin) IsDecommissioned() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (cp *corePlugin) Target() backendplugin.Target {
|
||||
return backendplugin.TargetInMemory
|
||||
}
|
||||
|
||||
func (cp *corePlugin) CollectMetrics(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
}
|
||||
|
@ -120,6 +120,10 @@ func (p *grpcPlugin) IsDecommissioned() bool {
|
||||
return p.decommissioned
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) Target() backendplugin.Target {
|
||||
return backendplugin.TargetLocal
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) getPluginClient() (pluginClient, bool) {
|
||||
p.mutex.RLock()
|
||||
if p.client == nil || p.client.Exited() || p.pluginClient == nil {
|
||||
|
@ -17,9 +17,19 @@ type Plugin interface {
|
||||
Exited() bool
|
||||
Decommission() error
|
||||
IsDecommissioned() bool
|
||||
Target() Target
|
||||
backend.CollectMetricsHandler
|
||||
backend.CheckHealthHandler
|
||||
backend.QueryDataHandler
|
||||
backend.CallResourceHandler
|
||||
backend.StreamHandler
|
||||
}
|
||||
|
||||
type Target string
|
||||
|
||||
const (
|
||||
TargetNone Target = "none"
|
||||
TargetUnknown Target = "unknown"
|
||||
TargetInMemory Target = "in_memory"
|
||||
TargetLocal Target = "local"
|
||||
)
|
||||
|
@ -6,11 +6,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -18,20 +19,20 @@ var (
|
||||
Namespace: "grafana",
|
||||
Name: "plugin_request_total",
|
||||
Help: "The total amount of plugin requests",
|
||||
}, []string{"plugin_id", "endpoint", "status"})
|
||||
}, []string{"plugin_id", "endpoint", "status", "target"})
|
||||
|
||||
pluginRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: "grafana",
|
||||
Name: "plugin_request_duration_milliseconds",
|
||||
Help: "Plugin request duration",
|
||||
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25, 50, 100},
|
||||
}, []string{"plugin_id", "endpoint"})
|
||||
}, []string{"plugin_id", "endpoint", "target"})
|
||||
)
|
||||
|
||||
var logger log.Logger = log.New("plugin.instrumentation")
|
||||
|
||||
// instrumentPluginRequest instruments success rate and latency of `fn`
|
||||
func instrumentPluginRequest(ctx context.Context, cfg *config.Cfg, pluginCtx *backend.PluginContext, endpoint string, fn func() error) error {
|
||||
func instrumentPluginRequest(ctx context.Context, cfg Cfg, pluginCtx *backend.PluginContext, endpoint string, fn func() error) error {
|
||||
status := "ok"
|
||||
|
||||
start := time.Now()
|
||||
@ -44,8 +45,8 @@ func instrumentPluginRequest(ctx context.Context, cfg *config.Cfg, pluginCtx *ba
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
pluginRequestDuration.WithLabelValues(pluginCtx.PluginID, endpoint).Observe(float64(elapsed / time.Millisecond))
|
||||
pluginRequestCounter.WithLabelValues(pluginCtx.PluginID, endpoint, status).Inc()
|
||||
pluginRequestDuration.WithLabelValues(pluginCtx.PluginID, endpoint, string(cfg.Target)).Observe(float64(elapsed / time.Millisecond))
|
||||
pluginRequestCounter.WithLabelValues(pluginCtx.PluginID, endpoint, status, string(cfg.Target)).Inc()
|
||||
|
||||
if cfg.LogDatasourceRequests {
|
||||
logParams := []interface{}{
|
||||
@ -77,22 +78,27 @@ func instrumentPluginRequest(ctx context.Context, cfg *config.Cfg, pluginCtx *ba
|
||||
return err
|
||||
}
|
||||
|
||||
type Cfg struct {
|
||||
LogDatasourceRequests bool
|
||||
Target backendplugin.Target
|
||||
}
|
||||
|
||||
// InstrumentCollectMetrics instruments collectMetrics.
|
||||
func InstrumentCollectMetrics(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error {
|
||||
func InstrumentCollectMetrics(ctx context.Context, req *backend.PluginContext, cfg Cfg, fn func() error) error {
|
||||
return instrumentPluginRequest(ctx, cfg, req, "collectMetrics", fn)
|
||||
}
|
||||
|
||||
// InstrumentCheckHealthRequest instruments checkHealth.
|
||||
func InstrumentCheckHealthRequest(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error {
|
||||
func InstrumentCheckHealthRequest(ctx context.Context, req *backend.PluginContext, cfg Cfg, fn func() error) error {
|
||||
return instrumentPluginRequest(ctx, cfg, req, "checkHealth", fn)
|
||||
}
|
||||
|
||||
// InstrumentCallResourceRequest instruments callResource.
|
||||
func InstrumentCallResourceRequest(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error {
|
||||
func InstrumentCallResourceRequest(ctx context.Context, req *backend.PluginContext, cfg Cfg, fn func() error) error {
|
||||
return instrumentPluginRequest(ctx, cfg, req, "callResource", fn)
|
||||
}
|
||||
|
||||
// InstrumentQueryDataRequest instruments success rate and latency of query data requests.
|
||||
func InstrumentQueryDataRequest(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error {
|
||||
func InstrumentQueryDataRequest(ctx context.Context, req *backend.PluginContext, cfg Cfg, fn func() error) error {
|
||||
return instrumentPluginRequest(ctx, cfg, req, "queryData", fn)
|
||||
}
|
||||
|
@ -35,14 +35,17 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
|
||||
return nil, fmt.Errorf("req cannot be nil")
|
||||
}
|
||||
|
||||
plugin, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
p, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return nil, plugins.ErrPluginNotRegistered.Errorf("%w", backendplugin.ErrPluginNotRegistered)
|
||||
}
|
||||
|
||||
var resp *backend.QueryDataResponse
|
||||
err := instrumentation.InstrumentQueryDataRequest(ctx, &req.PluginContext, s.cfg, func() (innerErr error) {
|
||||
resp, innerErr = plugin.QueryData(ctx, req)
|
||||
err := instrumentation.InstrumentQueryDataRequest(ctx, &req.PluginContext, instrumentation.Cfg{
|
||||
LogDatasourceRequests: s.cfg.LogDatasourceRequests,
|
||||
Target: p.Target(),
|
||||
}, func() (innerErr error) {
|
||||
resp, innerErr = p.QueryData(ctx, req)
|
||||
return
|
||||
})
|
||||
|
||||
@ -83,7 +86,10 @@ func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceReq
|
||||
if !exists {
|
||||
return backendplugin.ErrPluginNotRegistered
|
||||
}
|
||||
err := instrumentation.InstrumentCallResourceRequest(ctx, &req.PluginContext, s.cfg, func() error {
|
||||
err := instrumentation.InstrumentCallResourceRequest(ctx, &req.PluginContext, instrumentation.Cfg{
|
||||
LogDatasourceRequests: s.cfg.LogDatasourceRequests,
|
||||
Target: p.Target(),
|
||||
}, func() error {
|
||||
removeConnectionHeaders(req.Headers)
|
||||
removeHopByHopHeaders(req.Headers)
|
||||
|
||||
@ -120,7 +126,10 @@ func (s *Service) CollectMetrics(ctx context.Context, req *backend.CollectMetric
|
||||
}
|
||||
|
||||
var resp *backend.CollectMetricsResult
|
||||
err := instrumentation.InstrumentCollectMetrics(ctx, &req.PluginContext, s.cfg, func() (innerErr error) {
|
||||
err := instrumentation.InstrumentCollectMetrics(ctx, &req.PluginContext, instrumentation.Cfg{
|
||||
LogDatasourceRequests: s.cfg.LogDatasourceRequests,
|
||||
Target: p.Target(),
|
||||
}, func() (innerErr error) {
|
||||
resp, innerErr = p.CollectMetrics(ctx, req)
|
||||
return
|
||||
})
|
||||
@ -142,7 +151,10 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
|
||||
}
|
||||
|
||||
var resp *backend.CheckHealthResult
|
||||
err := instrumentation.InstrumentCheckHealthRequest(ctx, &req.PluginContext, s.cfg, func() (innerErr error) {
|
||||
err := instrumentation.InstrumentCheckHealthRequest(ctx, &req.PluginContext, instrumentation.Cfg{
|
||||
LogDatasourceRequests: s.cfg.LogDatasourceRequests,
|
||||
Target: p.Target(),
|
||||
}, func() (innerErr error) {
|
||||
resp, innerErr = p.CheckHealth(ctx, req)
|
||||
return
|
||||
})
|
||||
|
@ -267,6 +267,16 @@ func (p *Plugin) Exited() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Plugin) Target() backendplugin.Target {
|
||||
if !p.Backend {
|
||||
return backendplugin.TargetNone
|
||||
}
|
||||
if p.client == nil {
|
||||
return backendplugin.TargetUnknown
|
||||
}
|
||||
return p.client.Target()
|
||||
}
|
||||
|
||||
func (p *Plugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
pluginClient, ok := p.Client()
|
||||
if !ok {
|
||||
|
@ -634,6 +634,10 @@ func (tp *testPlugin) IsDecommissioned() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (tp *testPlugin) Target() backendplugin.Target {
|
||||
return backendplugin.TargetNone
|
||||
}
|
||||
|
||||
func (tp *testPlugin) CollectMetrics(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user