Add some basic stats for plugin preinstalls (#92220)

This commit is contained in:
Andres Martinez Gotor 2024-08-22 15:17:27 +02:00 committed by GitHub
parent dfbddd8262
commit c20ba5b09d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 3 deletions

View File

@ -5,13 +5,32 @@ import (
"errors"
"fmt"
"runtime"
"sync"
"time"
"cuelang.org/go/pkg/time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus"
)
var (
installRequestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "plugins",
Name: "preinstall_total",
Help: "The total amount of plugin preinstallations",
}, []string{"plugin_id", "version"})
installRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "plugins",
Name: "preinstall_duration_seconds",
Help: "Plugin preinstallation duration",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25, 50, 100},
}, []string{"plugin_id", "version"})
once sync.Once
)
type Service struct {
@ -23,7 +42,12 @@ type Service struct {
failOnErr bool
}
func ProvideService(cfg *setting.Cfg, features featuremgmt.FeatureToggles, pluginStore pluginstore.Store, pluginInstaller plugins.Installer) (*Service, error) {
func ProvideService(cfg *setting.Cfg, features featuremgmt.FeatureToggles, pluginStore pluginstore.Store, pluginInstaller plugins.Installer, promReg prometheus.Registerer) (*Service, error) {
once.Do(func() {
promReg.MustRegister(installRequestCounter)
promReg.MustRegister(installRequestDuration)
})
s := &Service{
features: features,
log: log.New("plugin.backgroundinstaller"),
@ -82,6 +106,7 @@ func (s *Service) installPlugins(ctx context.Context) error {
}
s.log.Info("Installing plugin", "pluginId", installPlugin.ID, "version", installPlugin.Version)
start := time.Now()
err := s.pluginInstaller.Add(ctx, installPlugin.ID, installPlugin.Version, compatOpts)
if err != nil {
var dupeErr plugins.DuplicateError
@ -96,7 +121,10 @@ func (s *Service) installPlugins(ctx context.Context) error {
s.log.Error("Failed to install plugin", "pluginId", installPlugin.ID, "version", installPlugin.Version, "error", err)
continue
}
s.log.Info("Plugin successfully installed", "pluginId", installPlugin.ID, "version", installPlugin.Version)
elapsed := time.Since(start)
s.log.Info("Plugin successfully installed", "pluginId", installPlugin.ID, "version", installPlugin.Version, "duration", elapsed)
installRequestDuration.WithLabelValues(installPlugin.ID, installPlugin.Version).Observe(elapsed.Seconds())
installRequestCounter.WithLabelValues(installPlugin.ID, installPlugin.Version).Inc()
}
return nil

View File

@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)
@ -24,6 +25,7 @@ func TestService_IsDisabled(t *testing.T) {
featuremgmt.WithFeatures(featuremgmt.FlagBackgroundPluginInstaller),
pluginstore.New(registry.NewInMemory(), &fakes.FakeLoader{}),
&fakes.FakePluginInstaller{},
prometheus.NewRegistry(),
)
require.NoError(t, err)
@ -48,6 +50,7 @@ func TestService_Run(t *testing.T) {
return nil
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
@ -73,6 +76,7 @@ func TestService_Run(t *testing.T) {
return nil
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
@ -102,6 +106,7 @@ func TestService_Run(t *testing.T) {
return plugins.DuplicateError{}
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
@ -134,6 +139,7 @@ func TestService_Run(t *testing.T) {
return nil
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
@ -157,6 +163,7 @@ func TestService_Run(t *testing.T) {
return nil
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
@ -183,6 +190,7 @@ func TestService_Run(t *testing.T) {
return nil
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
err = s.Run(context.Background())
@ -205,6 +213,7 @@ func TestService_Run(t *testing.T) {
return nil
},
},
prometheus.NewRegistry(),
)
require.NoError(t, err)
require.True(t, installed)
@ -223,6 +232,7 @@ func TestService_Run(t *testing.T) {
return plugins.NotFoundError{}
},
},
prometheus.NewRegistry(),
)
require.ErrorAs(t, err, &plugins.NotFoundError{})
})