From be6efd9518a1cc03456b2b1084a183c554a0690c Mon Sep 17 00:00:00 2001 From: Alyssa Bull <58453566+alyssabull@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:52:44 -0700 Subject: [PATCH] Cloud Monitoring: Add standalone files and modify plugin.json (#81596) --- pkg/plugins/pfs/corelist/corelist_load_gen.go | 2 +- pkg/plugins/plugindef/plugindef.cue | 2 +- .../plugins_integration_test.go | 46 +++++++++---------- .../api/plugins/data/expectedListResp.json | 4 +- .../cloud-monitoring/standalone/datasource.go | 38 +++++++++++++++ pkg/tsdb/cloud-monitoring/standalone/main.go | 23 ++++++++++ .../datasource/cloud-monitoring/plugin.json | 7 +-- 7 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 pkg/tsdb/cloud-monitoring/standalone/datasource.go create mode 100644 pkg/tsdb/cloud-monitoring/standalone/main.go diff --git a/pkg/plugins/pfs/corelist/corelist_load_gen.go b/pkg/plugins/pfs/corelist/corelist_load_gen.go index 3394d9fe03d..35be65e201f 100644 --- a/pkg/plugins/pfs/corelist/corelist_load_gen.go +++ b/pkg/plugins/pfs/corelist/corelist_load_gen.go @@ -34,7 +34,7 @@ func corePlugins(rt *thema.Runtime) []pfs.ParsedPlugin { return []pfs.ParsedPlugin{ parsePluginOrPanic("public/app/plugins/datasource/alertmanager", "alertmanager", rt), parsePluginOrPanic("public/app/plugins/datasource/azuremonitor", "grafana_azure_monitor_datasource", rt), - parsePluginOrPanic("public/app/plugins/datasource/cloud-monitoring", "stackdriver", rt), + parsePluginOrPanic("public/app/plugins/datasource/cloud-monitoring", "grafana_cloud_monitoring_datasource", rt), parsePluginOrPanic("public/app/plugins/datasource/cloudwatch", "cloudwatch", rt), parsePluginOrPanic("public/app/plugins/datasource/dashboard", "dashboard", rt), parsePluginOrPanic("public/app/plugins/datasource/elasticsearch", "elasticsearch", rt), diff --git a/pkg/plugins/plugindef/plugindef.cue b/pkg/plugins/plugindef/plugindef.cue index e2865a0f200..1ddcd19d251 100644 --- a/pkg/plugins/plugindef/plugindef.cue +++ b/pkg/plugins/plugindef/plugindef.cue @@ -16,7 +16,7 @@ schemas: [{ // grafana.com, then the plugin `id` has to follow the naming // conventions. id: string & strings.MinRunes(1) - id: =~"^([0-9a-z]+\\-([0-9a-z]+\\-)?(\(strings.Join([ for t in _types {t}], "|"))))|(alertGroups|alertlist|annolist|barchart|bargauge|candlestick|canvas|dashlist|debug|datagrid|gauge|geomap|gettingstarted|graph|heatmap|histogram|icon|live|logs|news|nodeGraph|piechart|pluginlist|stat|state-timeline|status-history|table|table-old|text|timeseries|trend|traces|welcome|xychart|alertmanager|cloudwatch|dashboard|elasticsearch|grafana|grafana-azure-monitor-datasource|graphite|influxdb|jaeger|loki|mixed|mssql|mysql|opentsdb|postgres|prometheus|stackdriver|tempo|grafana-testdata-datasource|zipkin|phlare|parca)$" + id: =~"^([0-9a-z]+\\-([0-9a-z]+\\-)?(\(strings.Join([ for t in _types {t}], "|"))))|(alertGroups|alertlist|annolist|barchart|bargauge|candlestick|canvas|dashlist|debug|datagrid|gauge|geomap|gettingstarted|graph|heatmap|histogram|icon|live|logs|news|nodeGraph|piechart|pluginlist|stat|state-timeline|status-history|table|table-old|text|timeseries|trend|traces|welcome|xychart|alertmanager|cloudwatch|dashboard|elasticsearch|grafana|grafana-azure-monitor-datasource|grafana-cloud-monitoring-datasource|graphite|influxdb|jaeger|loki|mixed|mssql|mysql|opentsdb|postgres|prometheus|stackdriver|tempo|grafana-testdata-datasource|zipkin|phlare|parca)$" // An alias is useful when migrating from one plugin id to another (rebranding etc) // This should be used sparingly, and is currently only supported though a hardcoded checklist diff --git a/pkg/services/pluginsintegration/plugins_integration_test.go b/pkg/services/pluginsintegration/plugins_integration_test.go index 214f160730e..14d3fbb835c 100644 --- a/pkg/services/pluginsintegration/plugins_integration_test.go +++ b/pkg/services/pluginsintegration/plugins_integration_test.go @@ -165,29 +165,29 @@ func verifyCorePluginCatalogue(t *testing.T, ctx context.Context, ps *pluginstor } expDataSources := map[string]struct{}{ - "cloudwatch": {}, - "stackdriver": {}, - "grafana-azure-monitor-datasource": {}, - "elasticsearch": {}, - "graphite": {}, - "influxdb": {}, - "loki": {}, - "opentsdb": {}, - "prometheus": {}, - "tempo": {}, - "grafana-testdata-datasource": {}, - "grafana-postgresql-datasource": {}, - "mysql": {}, - "mssql": {}, - "grafana": {}, - "alertmanager": {}, - "dashboard": {}, - "input": {}, - "jaeger": {}, - "mixed": {}, - "zipkin": {}, - "grafana-pyroscope-datasource": {}, - "parca": {}, + "cloudwatch": {}, + "grafana-azure-monitor-datasource": {}, + "grafana-cloud-monitoring-datasource": {}, + "elasticsearch": {}, + "graphite": {}, + "influxdb": {}, + "loki": {}, + "opentsdb": {}, + "prometheus": {}, + "tempo": {}, + "grafana-testdata-datasource": {}, + "grafana-postgresql-datasource": {}, + "mysql": {}, + "mssql": {}, + "grafana": {}, + "alertmanager": {}, + "dashboard": {}, + "input": {}, + "jaeger": {}, + "mixed": {}, + "zipkin": {}, + "grafana-pyroscope-datasource": {}, + "parca": {}, } expApps := map[string]struct{}{ diff --git a/pkg/tests/api/plugins/data/expectedListResp.json b/pkg/tests/api/plugins/data/expectedListResp.json index 3abaf4caf8d..ea8682fe3f4 100644 --- a/pkg/tests/api/plugins/data/expectedListResp.json +++ b/pkg/tests/api/plugins/data/expectedListResp.json @@ -626,7 +626,7 @@ { "name": "Google Cloud Monitoring", "type": "datasource", - "id": "stackdriver", + "id": "grafana-cloud-monitoring-datasource", "enabled": true, "pinned": false, "info": { @@ -652,7 +652,7 @@ }, "latestVersion": "", "hasUpdate": false, - "defaultNavUrl": "/plugins/stackdriver/", + "defaultNavUrl": "/plugins/grafana-cloud-monitoring-datasource/", "category": "cloud", "state": "", "signature": "internal", diff --git a/pkg/tsdb/cloud-monitoring/standalone/datasource.go b/pkg/tsdb/cloud-monitoring/standalone/datasource.go new file mode 100644 index 00000000000..169f73cd310 --- /dev/null +++ b/pkg/tsdb/cloud-monitoring/standalone/datasource.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + + "github.com/grafana/grafana-plugin-sdk-go/backend" + "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" + cloudmonitoring "github.com/grafana/grafana/pkg/tsdb/cloud-monitoring" +) + +var ( + _ backend.QueryDataHandler = (*Datasource)(nil) + _ backend.CheckHealthHandler = (*Datasource)(nil) + _ backend.CallResourceHandler = (*Datasource)(nil) +) + +func NewDatasource(context.Context, backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { + return &Datasource{ + Service: cloudmonitoring.ProvideService(httpclient.NewProvider()), + }, nil +} + +type Datasource struct { + Service *cloudmonitoring.Service +} + +func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { + return d.Service.QueryData(ctx, req) +} + +func (d *Datasource) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { + return d.Service.CallResource(ctx, req, sender) +} + +func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { + return d.Service.CheckHealth(ctx, req) +} diff --git a/pkg/tsdb/cloud-monitoring/standalone/main.go b/pkg/tsdb/cloud-monitoring/standalone/main.go new file mode 100644 index 00000000000..9d85afc8fb4 --- /dev/null +++ b/pkg/tsdb/cloud-monitoring/standalone/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "os" + + "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" + "github.com/grafana/grafana-plugin-sdk-go/backend/log" +) + +func main() { + // Start listening to requests sent from Grafana. This call is blocking so + // it won't finish until Grafana shuts down the process or the plugin choose + // to exit by itself using os.Exit. Manage automatically manages life cycle + // of datasource instances. It accepts datasource instance factory as first + // argument. This factory will be automatically called on incoming request + // from Grafana to create different instances of SampleDatasource (per datasource + // ID). When datasource configuration changed Dispose method will be called and + // new datasource instance created using NewSampleDatasource factory. + if err := datasource.Manage("grafana-cloud-monitoring-datasource", NewDatasource, datasource.ManageOpts{}); err != nil { + log.DefaultLogger.Error(err.Error()) + os.Exit(1) + } +} diff --git a/public/app/plugins/datasource/cloud-monitoring/plugin.json b/public/app/plugins/datasource/cloud-monitoring/plugin.json index 562c7779ede..2d2908edd05 100644 --- a/public/app/plugins/datasource/cloud-monitoring/plugin.json +++ b/public/app/plugins/datasource/cloud-monitoring/plugin.json @@ -1,13 +1,14 @@ { - "name": "Google Cloud Monitoring", "type": "datasource", - "id": "stackdriver", + "name": "Google Cloud Monitoring", + "id": "grafana-cloud-monitoring-datasource", "category": "cloud", "metrics": true, "alerting": true, "annotations": true, "backend": true, + "logs": true, "includes": [ { "type": "dashboard", @@ -79,7 +80,7 @@ "maxDataPoints": true, "cacheTimeout": true }, - + "executable": "gpx_cloudmonitoring", "info": { "description": "Data source for Google's monitoring service (formerly named Stackdriver)", "version": "%VERSION%",