diff --git a/pkg/infra/usagestats/statscollector/service.go b/pkg/infra/usagestats/statscollector/service.go index 20b0d186f98..cb30bd7f030 100644 --- a/pkg/infra/usagestats/statscollector/service.go +++ b/pkg/infra/usagestats/statscollector/service.go @@ -20,6 +20,7 @@ import ( "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" + "github.com/grafana/grafana/pkg/services/pluginsintegration/sandbox" "github.com/grafana/grafana/pkg/services/stats" "github.com/grafana/grafana/pkg/setting" ) @@ -39,6 +40,7 @@ type Service struct { features *featuremgmt.FeatureManager datasources datasources.DataSourceService httpClientProvider httpclient.Provider + sandbox sandbox.Sandbox log log.Logger @@ -58,6 +60,7 @@ func ProvideService( features *featuremgmt.FeatureManager, datasourceService datasources.DataSourceService, httpClientProvider httpclient.Provider, + sandbox sandbox.Sandbox, ) *Service { s := &Service{ cfg: cfg, @@ -69,6 +72,7 @@ func ProvideService( features: features, datasources: datasourceService, httpClientProvider: httpClientProvider, + sandbox: sandbox, startTime: time.Now(), log: log.New("infra.usagestats.collector"), @@ -146,6 +150,7 @@ func (s *Service) collectSystemStats(ctx context.Context) (map[string]any, error m["stats.plugins.apps.count"] = s.appCount(ctx) m["stats.plugins.panels.count"] = s.panelCount(ctx) m["stats.plugins.datasources.count"] = s.dataSourceCount(ctx) + m["stats.plugins.sandboxed_plugins.count"] = s.sandboxCount() m["stats.alerts.count"] = statsResult.Alerts m["stats.active_users.count"] = statsResult.ActiveUsers m["stats.active_admins.count"] = statsResult.ActiveAdmins @@ -361,3 +366,12 @@ func (s *Service) panelCount(ctx context.Context) int { func (s *Service) dataSourceCount(ctx context.Context) int { return len(s.plugins.Plugins(ctx, plugins.TypeDataSource)) } + +func (s *Service) sandboxCount() int { + ps, err := s.sandbox.Plugins() + if err != nil { + s.log.Error("Failed to get sandboxed plugin count", "error", err) + return 0 + } + return len(ps) +} diff --git a/pkg/infra/usagestats/statscollector/service_test.go b/pkg/infra/usagestats/statscollector/service_test.go index 0d5f955ce47..f8c4f2a5f7f 100644 --- a/pkg/infra/usagestats/statscollector/service_test.go +++ b/pkg/infra/usagestats/statscollector/service_test.go @@ -24,6 +24,7 @@ import ( "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" + "github.com/grafana/grafana/pkg/services/pluginsintegration/sandbox" "github.com/grafana/grafana/pkg/services/stats" "github.com/grafana/grafana/pkg/services/stats/statstest" "github.com/grafana/grafana/pkg/setting" @@ -148,6 +149,7 @@ func TestCollectingUsageStats(t *testing.T) { RemoteCacheOptions: &setting.RemoteCacheSettings{ Name: "database", }, + EnableFrontendSandboxForPlugins: []string{"grafana-worldmap-panel"}, }, sqlStore, statsService, withDatasources(mockDatasourceService{datasources: expectedDataSources})) @@ -178,6 +180,8 @@ func TestCollectingUsageStats(t *testing.T) { assert.EqualValues(t, 3, metrics["stats.correlations.count"]) assert.InDelta(t, int64(65), metrics["stats.uptime"], 6) + + assert.EqualValues(t, 1, metrics["stats.plugins.sandboxed_plugins.count"]) } func TestDatasourceStats(t *testing.T) { @@ -382,6 +386,7 @@ func createService(t testing.TB, cfg *setting.Cfg, store db.DB, statsService sta featuremgmt.WithManager("feature1", "feature2"), o.datasources, httpclient.NewProvider(sdkhttpclient.ProviderOptions{Middlewares: []sdkhttpclient.Middleware{}}), + sandbox.ProvideService(cfg), ) } diff --git a/pkg/server/wireexts_oss.go b/pkg/server/wireexts_oss.go index d8990795049..2ace4123b30 100644 --- a/pkg/server/wireexts_oss.go +++ b/pkg/server/wireexts_oss.go @@ -39,6 +39,7 @@ import ( "github.com/grafana/grafana/pkg/services/login/authinfoimpl" "github.com/grafana/grafana/pkg/services/pluginsintegration" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginaccesscontrol" + "github.com/grafana/grafana/pkg/services/pluginsintegration/sandbox" "github.com/grafana/grafana/pkg/services/provisioning" "github.com/grafana/grafana/pkg/services/publicdashboards" publicdashboardsApi "github.com/grafana/grafana/pkg/services/publicdashboards/api" @@ -113,6 +114,8 @@ var wireExtsBasicSet = wire.NewSet( search2.ProvideDashboardStats, wire.Bind(new(search2.DashboardStats), new(*search2.OssDashboardStats)), search2.ProvideDocumentBuilders, + sandbox.ProvideService, + wire.Bind(new(sandbox.Sandbox), new(*sandbox.Service)), ) var wireExtsSet = wire.NewSet( diff --git a/pkg/services/pluginsintegration/sandbox/sandbox.go b/pkg/services/pluginsintegration/sandbox/sandbox.go new file mode 100644 index 00000000000..4ec5ac27c69 --- /dev/null +++ b/pkg/services/pluginsintegration/sandbox/sandbox.go @@ -0,0 +1,21 @@ +package sandbox + +import "github.com/grafana/grafana/pkg/setting" + +type Sandbox interface { + Plugins() ([]string, error) +} + +type Service struct { + cfg *setting.Cfg +} + +func ProvideService(cfg *setting.Cfg) *Service { + return &Service{ + cfg: cfg, + } +} + +func (s *Service) Plugins() ([]string, error) { + return s.cfg.EnableFrontendSandboxForPlugins, nil +} diff --git a/pkg/services/pluginsintegration/sandbox/sandbox_test.go b/pkg/services/pluginsintegration/sandbox/sandbox_test.go new file mode 100644 index 00000000000..b318ae19978 --- /dev/null +++ b/pkg/services/pluginsintegration/sandbox/sandbox_test.go @@ -0,0 +1,19 @@ +package sandbox + +import ( + "testing" + + "github.com/grafana/grafana/pkg/setting" + "github.com/stretchr/testify/assert" +) + +func TestService_Plugins(t *testing.T) { + cfg := &setting.Cfg{ + EnableFrontendSandboxForPlugins: []string{"plugin1", "plugin2"}, + } + service := ProvideService(cfg) + + plugins, err := service.Plugins() + assert.NoError(t, err) + assert.Equal(t, []string{"plugin1", "plugin2"}, plugins) +}