From 8a56a9478123d00de80e4407e249dcbcb4b8d41e Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Wed, 29 Nov 2023 14:37:36 -0600 Subject: [PATCH] Usagestats: Add stat group for alert rule groups (#78825) * Add rule group support to usagestats service * Quote column name --- pkg/infra/metrics/metrics.go | 10 ++++++++++ pkg/infra/usagestats/statscollector/service.go | 2 ++ pkg/services/stats/models.go | 1 + pkg/services/stats/statsimpl/stats.go | 3 +++ 4 files changed, 16 insertions(+) diff --git a/pkg/infra/metrics/metrics.go b/pkg/infra/metrics/metrics.go index 4ae2d837cff..6c0d1ad706d 100644 --- a/pkg/infra/metrics/metrics.go +++ b/pkg/infra/metrics/metrics.go @@ -185,6 +185,9 @@ var ( // StatsTotalAlertRules is a metric of total number of alert rules stored in Grafana. StatsTotalAlertRules prometheus.Gauge + // StatsTotalRuleGroups is a metric of total number of alert rule groups stored in Grafana. + StatsTotalRuleGroups prometheus.Gauge + // StatsTotalDashboardVersions is a metric of total number of dashboard versions stored in Grafana. StatsTotalDashboardVersions prometheus.Gauge @@ -554,6 +557,12 @@ func init() { Namespace: ExporterName, }) + StatsTotalRuleGroups = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "stat_totals_rule_groups", + Help: "total amount of alert rule groups in the database", + Namespace: ExporterName, + }) + MAccessPermissionsSummary = prometheus.NewHistogram(prometheus.HistogramOpts{ Name: "access_permissions_duration", Help: "Histogram for the runtime of permissions check function.", @@ -708,6 +717,7 @@ func initMetricVars() { StatsTotalDashboardVersions, StatsTotalAnnotations, StatsTotalAlertRules, + StatsTotalRuleGroups, MAccessEvaluationCount, StatsTotalLibraryPanels, StatsTotalLibraryVariables, diff --git a/pkg/infra/usagestats/statscollector/service.go b/pkg/infra/usagestats/statscollector/service.go index bbef8db90f9..158cc5f6367 100644 --- a/pkg/infra/usagestats/statscollector/service.go +++ b/pkg/infra/usagestats/statscollector/service.go @@ -164,6 +164,7 @@ func (s *Service) collectSystemStats(ctx context.Context) (map[string]any, error m["stats.dashboard_versions.count"] = statsResult.DashboardVersions m["stats.annotations.count"] = statsResult.Annotations m["stats.alert_rules.count"] = statsResult.AlertRules + m["stats.rule_groups.count"] = statsResult.RuleGroups m["stats.library_panels.count"] = statsResult.LibraryPanels m["stats.library_variables.count"] = statsResult.LibraryVariables m["stats.dashboards_viewers_can_edit.count"] = statsResult.DashboardsViewersCanEdit @@ -328,6 +329,7 @@ func (s *Service) updateTotalStats(ctx context.Context) bool { metrics.StatsTotalDashboardVersions.Set(float64(statsResult.DashboardVersions)) metrics.StatsTotalAnnotations.Set(float64(statsResult.Annotations)) metrics.StatsTotalAlertRules.Set(float64(statsResult.AlertRules)) + metrics.StatsTotalRuleGroups.Set(float64(statsResult.RuleGroups)) metrics.StatsTotalLibraryPanels.Set(float64(statsResult.LibraryPanels)) metrics.StatsTotalLibraryVariables.Set(float64(statsResult.LibraryVariables)) diff --git a/pkg/services/stats/models.go b/pkg/services/stats/models.go index 447a07f95b5..bf1531d95c2 100644 --- a/pkg/services/stats/models.go +++ b/pkg/services/stats/models.go @@ -26,6 +26,7 @@ type SystemStats struct { DashboardVersions int64 Annotations int64 AlertRules int64 + RuleGroups int64 LibraryPanels int64 LibraryVariables int64 DashboardsViewersCanEdit int64 diff --git a/pkg/services/stats/statsimpl/stats.go b/pkg/services/stats/statsimpl/stats.go index 9cb52295dff..0a350461b65 100644 --- a/pkg/services/stats/statsimpl/stats.go +++ b/pkg/services/stats/statsimpl/stats.go @@ -128,6 +128,9 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `WHERE active = true) AS active_data_keys,`) sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("dashboard_public") + `) AS public_dashboards,`) sb.Write(`(SELECT MIN(timestamp) FROM ` + dialect.Quote("migration_log") + `) AS database_created_time,`) + if ss.IsUnifiedAlertingEnabled() { + sb.Write(`(SELECT COUNT(DISTINCT (` + dialect.Quote("rule_group") + `)) FROM ` + dialect.Quote("alert_rule") + `) AS rule_groups,`) + } sb.Write(ss.roleCounterSQL(ctx))