From b68be999f7e0da95616e5d9b1ce9d0b7339ba90a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Tue, 11 Apr 2023 08:53:34 +0200 Subject: [PATCH] Correlations: Add usage stats about correlations (#66021) * Add usage stats about correlations * Add stats.correlations.count to collected stats * Expose grafana_stat_totals_correlations metric * Organize imports --- pkg/infra/metrics/metrics.go | 10 ++++++++ .../usagestats/statscollector/service.go | 3 +++ .../usagestats/statscollector/service_test.go | 2 ++ .../correlations/correlationstest/fake.go | 23 +++++++++++++++++++ pkg/services/stats/models.go | 1 + pkg/services/stats/statsimpl/stats.go | 1 + pkg/services/stats/statsimpl/stats_test.go | 22 ++++++++++++++++++ 7 files changed, 62 insertions(+) create mode 100644 pkg/services/correlations/correlationstest/fake.go diff --git a/pkg/infra/metrics/metrics.go b/pkg/infra/metrics/metrics.go index 152733c1405..c90c2177c66 100644 --- a/pkg/infra/metrics/metrics.go +++ b/pkg/infra/metrics/metrics.go @@ -201,6 +201,9 @@ var ( // MStatTotalPublicDashboards is a metric total amount of public dashboards MStatTotalPublicDashboards prometheus.Gauge + + // MStatTotalCorrelations is a metric total amount of correlations + MStatTotalCorrelations prometheus.Gauge ) func init() { @@ -592,6 +595,12 @@ func init() { Help: "total amount of public dashboards", Namespace: ExporterName, }) + + MStatTotalCorrelations = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "stat_totals_correlations", + Help: "total amount of correlations", + Namespace: ExporterName, + }) } // SetBuildInformation sets the build information for this binary @@ -705,5 +714,6 @@ func initMetricVars() { MStatTotalPublicDashboards, MPublicDashboardRequestCount, MPublicDashboardDatasourceQuerySuccess, + MStatTotalCorrelations, ) } diff --git a/pkg/infra/usagestats/statscollector/service.go b/pkg/infra/usagestats/statscollector/service.go index 0845468d067..2d0c79e5fc2 100644 --- a/pkg/infra/usagestats/statscollector/service.go +++ b/pkg/infra/usagestats/statscollector/service.go @@ -158,6 +158,7 @@ func (s *Service) collectSystemStats(ctx context.Context) (map[string]interface{ m["stats.data_keys.count"] = statsResult.DataKeys m["stats.active_data_keys.count"] = statsResult.ActiveDataKeys m["stats.public_dashboards.count"] = statsResult.PublicDashboards + m["stats.correlations.count"] = statsResult.Correlations ossEditionCount := 1 enterpriseEditionCount := 0 @@ -314,6 +315,8 @@ func (s *Service) updateTotalStats(ctx context.Context) bool { metrics.MStatTotalPublicDashboards.Set(float64(statsResult.PublicDashboards)) + metrics.MStatTotalCorrelations.Set(float64(statsResult.Correlations)) + dsResult, err := s.statsService.GetDataSourceStats(ctx, &stats.GetDataSourceStatsQuery{}) if err != nil { s.log.Error("Failed to get datasource stats", "error", err) diff --git a/pkg/infra/usagestats/statscollector/service_test.go b/pkg/infra/usagestats/statscollector/service_test.go index 98812cf14c8..b7c5299f13e 100644 --- a/pkg/infra/usagestats/statscollector/service_test.go +++ b/pkg/infra/usagestats/statscollector/service_test.go @@ -177,6 +177,7 @@ func TestCollectingUsageStats(t *testing.T) { assert.EqualValues(t, 11, metrics["stats.data_keys.count"]) assert.EqualValues(t, 3, metrics["stats.active_data_keys.count"]) assert.EqualValues(t, 5, metrics["stats.public_dashboards.count"]) + assert.EqualValues(t, 3, metrics["stats.correlations.count"]) assert.InDelta(t, int64(65), metrics["stats.uptime"], 6) } @@ -336,6 +337,7 @@ func mockSystemStats(statsService *statstest.FakeService) { DataKeys: 11, ActiveDataKeys: 3, PublicDashboards: 5, + Correlations: 3, } } diff --git a/pkg/services/correlations/correlationstest/fake.go b/pkg/services/correlations/correlationstest/fake.go new file mode 100644 index 00000000000..8180f35b9e5 --- /dev/null +++ b/pkg/services/correlations/correlationstest/fake.go @@ -0,0 +1,23 @@ +package correlationstest + +import ( + "github.com/grafana/grafana/pkg/api/routing" + "github.com/grafana/grafana/pkg/services/accesscontrol/acimpl" + "github.com/grafana/grafana/pkg/services/correlations" + "github.com/grafana/grafana/pkg/services/datasources" + fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes" + "github.com/grafana/grafana/pkg/services/quota/quotatest" + "github.com/grafana/grafana/pkg/services/sqlstore" + "github.com/grafana/grafana/pkg/setting" +) + +func New(sqlStore *sqlstore.SQLStore) *correlations.CorrelationsService { + ds := &fakeDatasources.FakeDataSourceService{ + DataSources: []*datasources.DataSource{ + {ID: 1, UID: "graphite", Type: datasources.DS_GRAPHITE}, + }, + } + + correlationsSvc, _ := correlations.ProvideService(sqlStore, routing.NewRouteRegister(), ds, acimpl.ProvideAccessControl(setting.NewCfg()), sqlStore.Bus(), quotatest.New(false, nil), sqlStore.Cfg) + return correlationsSvc +} diff --git a/pkg/services/stats/models.go b/pkg/services/stats/models.go index 66ab9dce1ad..ad8c4519531 100644 --- a/pkg/services/stats/models.go +++ b/pkg/services/stats/models.go @@ -42,6 +42,7 @@ type SystemStats struct { DataKeys int64 ActiveDataKeys int64 PublicDashboards int64 + Correlations int64 } type DataSourceStats struct { diff --git a/pkg/services/stats/statsimpl/stats.go b/pkg/services/stats/statsimpl/stats.go index dfc8875cd6b..122f6093707 100644 --- a/pkg/services/stats/statsimpl/stats.go +++ b/pkg/services/stats/statsimpl/stats.go @@ -73,6 +73,7 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("star") + `) AS stars,`) sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("playlist") + `) AS playlists,`) sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("alert") + `) AS alerts,`) + sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("correlation") + `) AS correlations,`) now := time.Now() activeUserDeadlineDate := now.Add(-activeUserTimeLimit) diff --git a/pkg/services/stats/statsimpl/stats_test.go b/pkg/services/stats/statsimpl/stats_test.go index e458f49105c..f2d1c840efd 100644 --- a/pkg/services/stats/statsimpl/stats_test.go +++ b/pkg/services/stats/statsimpl/stats_test.go @@ -8,6 +8,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/grafana/grafana/pkg/services/correlations" + "github.com/grafana/grafana/pkg/services/correlations/correlationstest" "github.com/grafana/grafana/pkg/services/org" "github.com/grafana/grafana/pkg/services/org/orgimpl" "github.com/grafana/grafana/pkg/services/quota/quotatest" @@ -38,6 +40,7 @@ func TestIntegrationStatsDataAccess(t *testing.T) { assert.Equal(t, int64(0), result.LibraryPanels) assert.Equal(t, int64(0), result.LibraryVariables) assert.Equal(t, int64(0), result.APIKeys) + assert.Equal(t, int64(2), result.Correlations) }) t.Run("Get system user count stats should not results in error", func(t *testing.T) { @@ -77,6 +80,25 @@ func populateDB(t *testing.T, sqlStore *sqlstore.SQLStore) { orgService, _ := orgimpl.ProvideService(sqlStore, sqlStore.Cfg, quotatest.New(false, nil)) userSvc, _ := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, "atest.FakeQuotaService{}, supportbundlestest.NewFakeBundleService()) + correlationsSvc := correlationstest.New(sqlStore) + + c := make([]correlations.Correlation, 2) + for i := range c { + cmd := correlations.CreateCorrelationCommand{ + Label: fmt.Sprintf("correlation %v", i), + SourceUID: "graphite", + OrgId: 1, + Config: correlations.CorrelationConfig{ + Field: "field", + Target: map[string]interface{}{}, + Type: correlations.ConfigTypeQuery, + }, + } + correlation, err := correlationsSvc.CreateCorrelation(context.Background(), cmd) + require.NoError(t, err) + c[i] = correlation + } + users := make([]user.User, 3) for i := range users { cmd := user.CreateUserCommand{