Frontend Sandbox: Add to statscollector (#99735)

This commit is contained in:
Andres Martinez Gotor
2025-01-30 16:17:36 +01:00
committed by GitHub
parent 64e9c38b66
commit a066659e11
5 changed files with 62 additions and 0 deletions
@@ -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)
}
@@ -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),
)
}
+3
View File
@@ -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(
@@ -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
}
@@ -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)
}