From 3fbe4801b76a9d79bd50d60f47d6ce2c9e3e5c63 Mon Sep 17 00:00:00 2001 From: Will Browne Date: Wed, 9 Feb 2022 17:36:53 +0100 Subject: [PATCH] Plugins: Update collect metrics handler to latest signature (#45166) * use updated collect metrics handler * use proto helper --- go.mod | 4 ++-- go.sum | 4 ++++ pkg/api/plugins.go | 7 +------ pkg/plugins/backendplugin/coreplugin/core_plugin.go | 2 +- .../backendplugin/coreplugin/core_plugin_test.go | 4 ++-- pkg/plugins/backendplugin/grpcplugin/client_v2.go | 4 ++-- pkg/plugins/backendplugin/grpcplugin/grpc_plugin.go | 4 ++-- pkg/plugins/ifaces.go | 4 +--- pkg/plugins/manager/manager.go | 8 ++++---- pkg/plugins/manager/manager_test.go | 10 +++++----- pkg/plugins/plugins.go | 4 ++-- 11 files changed, 26 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index a234cc23020..9c845aa5380 100644 --- a/go.mod +++ b/go.mod @@ -54,7 +54,7 @@ require ( github.com/gosimple/slug v1.9.0 github.com/grafana/cuetsy v0.0.0-20211119211437-8c25464cc9bf github.com/grafana/grafana-aws-sdk v0.10.0 - github.com/grafana/grafana-plugin-sdk-go v0.125.0 + github.com/grafana/grafana-plugin-sdk-go v0.126.0 github.com/grafana/loki v1.6.2-0.20211015002020-7832783b1caa github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/hashicorp/go-hclog v0.16.1 @@ -69,7 +69,7 @@ require ( github.com/lib/pq v1.10.0 github.com/linkedin/goavro/v2 v2.10.0 github.com/m3db/prometheus_remote_client_golang v0.4.4 - github.com/magefile/mage v1.11.0 + github.com/magefile/mage v1.12.1 github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-sqlite3 v1.14.7 github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 diff --git a/go.sum b/go.sum index 5c4d1ce5899..97486d7d58c 100644 --- a/go.sum +++ b/go.sum @@ -1250,6 +1250,8 @@ github.com/grafana/grafana-plugin-sdk-go v0.94.0/go.mod h1:3VXz4nCv6wH5SfgB3mlW3 github.com/grafana/grafana-plugin-sdk-go v0.114.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk= github.com/grafana/grafana-plugin-sdk-go v0.125.0 h1:wK2zopAaKhVIMkXzgbExKqZtt+x2ZTGfcY+3wvOuyYQ= github.com/grafana/grafana-plugin-sdk-go v0.125.0/go.mod h1:9YiJ5GUxIsIEUC0qR9+BJVP5M7mCSP6uc6Ne62YKkgc= +github.com/grafana/grafana-plugin-sdk-go v0.126.0 h1:GFstod7B/r5Ls9QiYV18fnOVtpWAtfR8aYSXfBvbCjE= +github.com/grafana/grafana-plugin-sdk-go v0.126.0/go.mod h1:9YiJ5GUxIsIEUC0qR9+BJVP5M7mCSP6uc6Ne62YKkgc= github.com/grafana/loki v1.6.2-0.20211015002020-7832783b1caa h1:+pXjAxavVR2FKKNsuuCXGCWEj8XGc1Af6SPiyBpzU2A= github.com/grafana/loki v1.6.2-0.20211015002020-7832783b1caa/go.mod h1:0O8o/juxNSKN/e+DzWDTRkl7Zm8CkZcz0NDqEdojlrk= github.com/grafana/saml v0.0.0-20211007135653-aed1b2edd86b h1:YiSGp34F4V0G08HHx1cJBf2GVgwYAkXQjzuVs1t8jYk= @@ -1643,6 +1645,8 @@ github.com/m3db/prometheus_remote_client_golang v0.4.4 h1:DsAIjVKoCp7Ym35tAOFL1O github.com/m3db/prometheus_remote_client_golang v0.4.4/go.mod h1:wHfVbA3eAK6dQvKjCkHhusWYegCk3bDGkA15zymSHdc= github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/magefile/mage v1.12.1 h1:oGdAbhIUd6iKamKlDGVtU6XGdy5SgNuCWn7gCTgHDtU= +github.com/magefile/mage v1.12.1/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= diff --git a/pkg/api/plugins.go b/pkg/api/plugins.go index 3744affae29..e621cc44163 100644 --- a/pkg/api/plugins.go +++ b/pkg/api/plugins.go @@ -230,12 +230,7 @@ func (hs *HTTPServer) GetPluginMarkdown(c *models.ReqContext) response.Response // /api/plugins/:pluginId/metrics func (hs *HTTPServer) CollectPluginMetrics(c *models.ReqContext) response.Response { pluginID := web.Params(c.Req)[":pluginId"] - plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID) - if !exists { - return response.Error(404, "Plugin not found", nil) - } - - resp, err := hs.pluginClient.CollectMetrics(c.Req.Context(), plugin.ID) + resp, err := hs.pluginClient.CollectMetrics(c.Req.Context(), &backend.CollectMetricsRequest{PluginContext: backend.PluginContext{PluginID: pluginID}}) if err != nil { return translatePluginRequestErrorToAPIError(err) } diff --git a/pkg/plugins/backendplugin/coreplugin/core_plugin.go b/pkg/plugins/backendplugin/coreplugin/core_plugin.go index 8a056540974..c59bdf92549 100644 --- a/pkg/plugins/backendplugin/coreplugin/core_plugin.go +++ b/pkg/plugins/backendplugin/coreplugin/core_plugin.go @@ -64,7 +64,7 @@ func (cp *corePlugin) IsDecommissioned() bool { return false } -func (cp *corePlugin) CollectMetrics(ctx context.Context) (*backend.CollectMetricsResult, error) { +func (cp *corePlugin) CollectMetrics(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { return nil, backendplugin.ErrMethodNotImplemented } diff --git a/pkg/plugins/backendplugin/coreplugin/core_plugin_test.go b/pkg/plugins/backendplugin/coreplugin/core_plugin_test.go index 2bd0a86a067..22ae85fba59 100644 --- a/pkg/plugins/backendplugin/coreplugin/core_plugin_test.go +++ b/pkg/plugins/backendplugin/coreplugin/core_plugin_test.go @@ -22,7 +22,7 @@ func TestCorePlugin(t *testing.T) { require.True(t, p.IsManaged()) require.False(t, p.Exited()) - _, err = p.CollectMetrics(context.Background()) + _, err = p.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{}) require.Equal(t, backendplugin.ErrMethodNotImplemented, err) _, err = p.CheckHealth(context.Background(), nil) @@ -55,7 +55,7 @@ func TestCorePlugin(t *testing.T) { require.True(t, p.IsManaged()) require.False(t, p.Exited()) - _, err = p.CollectMetrics(context.Background()) + _, err = p.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{}) require.Equal(t, backendplugin.ErrMethodNotImplemented, err) _, err = p.CheckHealth(context.Background(), &backend.CheckHealthRequest{}) diff --git a/pkg/plugins/backendplugin/grpcplugin/client_v2.go b/pkg/plugins/backendplugin/grpcplugin/client_v2.go index 30f6399bca0..6d44516bda3 100644 --- a/pkg/plugins/backendplugin/grpcplugin/client_v2.go +++ b/pkg/plugins/backendplugin/grpcplugin/client_v2.go @@ -92,12 +92,12 @@ func newClientV2(descriptor PluginDescriptor, logger log.Logger, rpcClient plugi return &c, nil } -func (c *ClientV2) CollectMetrics(ctx context.Context) (*backend.CollectMetricsResult, error) { +func (c *ClientV2) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { if c.DiagnosticsClient == nil { return &backend.CollectMetricsResult{}, nil } - protoResp, err := c.DiagnosticsClient.CollectMetrics(ctx, &pluginv2.CollectMetricsRequest{}) + protoResp, err := c.DiagnosticsClient.CollectMetrics(ctx, backend.ToProto().CollectMetricsRequest(req)) if err != nil { if status.Code(err) == codes.Unimplemented { return &backend.CollectMetricsResult{}, nil diff --git a/pkg/plugins/backendplugin/grpcplugin/grpc_plugin.go b/pkg/plugins/backendplugin/grpcplugin/grpc_plugin.go index a2af94132aa..f489be62795 100644 --- a/pkg/plugins/backendplugin/grpcplugin/grpc_plugin.go +++ b/pkg/plugins/backendplugin/grpcplugin/grpc_plugin.go @@ -131,12 +131,12 @@ func (p *grpcPlugin) getPluginClient() (pluginClient, bool) { return pluginClient, true } -func (p *grpcPlugin) CollectMetrics(ctx context.Context) (*backend.CollectMetricsResult, error) { +func (p *grpcPlugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { pluginClient, ok := p.getPluginClient() if !ok { return nil, backendplugin.ErrPluginUnavailable } - return pluginClient.CollectMetrics(ctx) + return pluginClient.CollectMetrics(ctx, req) } func (p *grpcPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { diff --git a/pkg/plugins/ifaces.go b/pkg/plugins/ifaces.go index 3e106fb34a8..105bdfb9205 100644 --- a/pkg/plugins/ifaces.go +++ b/pkg/plugins/ifaces.go @@ -47,9 +47,7 @@ type Client interface { backend.CheckHealthHandler backend.StreamHandler backend.CallResourceHandler - - // CollectMetrics collects metrics from a plugin. - CollectMetrics(ctx context.Context, pluginID string) (*backend.CollectMetricsResult, error) + backend.CollectMetricsHandler } // BackendFactoryProvider provides a backend factory for a provided plugin. diff --git a/pkg/plugins/manager/manager.go b/pkg/plugins/manager/manager.go index d795e7806c9..616f862d97b 100644 --- a/pkg/plugins/manager/manager.go +++ b/pkg/plugins/manager/manager.go @@ -207,15 +207,15 @@ func (m *PluginManager) CallResource(ctx context.Context, req *backend.CallResou return nil } -func (m *PluginManager) CollectMetrics(ctx context.Context, pluginID string) (*backend.CollectMetricsResult, error) { - p, exists := m.plugin(pluginID) +func (m *PluginManager) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { + p, exists := m.plugin(req.PluginContext.PluginID) if !exists { return nil, backendplugin.ErrPluginNotRegistered } var resp *backend.CollectMetricsResult err := instrumentation.InstrumentCollectMetrics(p.PluginID(), func() (innerErr error) { - resp, innerErr = p.CollectMetrics(ctx) + resp, innerErr = p.CollectMetrics(ctx, req) return }) if err != nil { @@ -233,7 +233,7 @@ func (m *PluginManager) CheckHealth(ctx context.Context, req *backend.CheckHealt var resp *backend.CheckHealthResult err := instrumentation.InstrumentCheckHealthRequest(p.PluginID(), func() (innerErr error) { - resp, innerErr = p.CheckHealth(ctx, &backend.CheckHealthRequest{PluginContext: req.PluginContext}) + resp, innerErr = p.CheckHealth(ctx, req) return }) diff --git a/pkg/plugins/manager/manager_test.go b/pkg/plugins/manager/manager_test.go index e7d550d4165..d0b5d9dc813 100644 --- a/pkg/plugins/manager/manager_test.go +++ b/pkg/plugins/manager/manager_test.go @@ -356,7 +356,7 @@ func TestPluginManager_lifecycle_managed(t *testing.T) { t.Run("Unimplemented handlers", func(t *testing.T) { t.Run("Collect metrics should return method not implemented error", func(t *testing.T) { - _, err = ctx.manager.CollectMetrics(context.Background(), testPluginID) + _, err = ctx.manager.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{PluginContext: backend.PluginContext{PluginID: testPluginID}}) require.Equal(t, backendplugin.ErrMethodNotImplemented, err) }) @@ -368,13 +368,13 @@ func TestPluginManager_lifecycle_managed(t *testing.T) { t.Run("Implemented handlers", func(t *testing.T) { t.Run("Collect metrics should return expected result", func(t *testing.T) { - ctx.pluginClient.CollectMetricsHandlerFunc = func(ctx context.Context) (*backend.CollectMetricsResult, error) { + ctx.pluginClient.CollectMetricsHandlerFunc = func(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { return &backend.CollectMetricsResult{ PrometheusMetrics: []byte("hello"), }, nil } - res, err := ctx.manager.CollectMetrics(context.Background(), testPluginID) + res, err := ctx.manager.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{PluginContext: backend.PluginContext{PluginID: testPluginID}}) require.NoError(t, err) require.NotNil(t, res) require.Equal(t, "hello", string(res.PrometheusMetrics)) @@ -651,9 +651,9 @@ func (pc *fakePluginClient) kill() { pc.exited = true } -func (pc *fakePluginClient) CollectMetrics(ctx context.Context) (*backend.CollectMetricsResult, error) { +func (pc *fakePluginClient) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { if pc.CollectMetricsHandlerFunc != nil { - return pc.CollectMetricsHandlerFunc(ctx) + return pc.CollectMetricsHandlerFunc(ctx, req) } return nil, backendplugin.ErrMethodNotImplemented diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 2a1f3042009..04cd731ddf4 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -263,12 +263,12 @@ func (p *Plugin) CheckHealth(ctx context.Context, req *backend.CheckHealthReques return pluginClient.CheckHealth(ctx, req) } -func (p *Plugin) CollectMetrics(ctx context.Context) (*backend.CollectMetricsResult, error) { +func (p *Plugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { pluginClient, ok := p.Client() if !ok { return nil, backendplugin.ErrPluginUnavailable } - return pluginClient.CollectMetrics(ctx) + return pluginClient.CollectMetrics(ctx, req) } func (p *Plugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {