From 26852ca788b83a4c757d5495dd0b57103f314acb Mon Sep 17 00:00:00 2001 From: Carl Bergquist Date: Fri, 3 Jul 2020 15:47:46 +0200 Subject: [PATCH] Instrument dashboard versions and annotation count (#26044) --- pkg/infra/metrics/metrics.go | 20 ++++++++++++++++++++ pkg/infra/usagestats/usage_stats.go | 4 ++++ pkg/infra/usagestats/usage_stats_test.go | 4 ++++ pkg/models/stats.go | 2 ++ pkg/services/sqlstore/stats.go | 2 ++ 5 files changed, 32 insertions(+) diff --git a/pkg/infra/metrics/metrics.go b/pkg/infra/metrics/metrics.go index f4bf21d66cd..4f51e4f36b6 100644 --- a/pkg/infra/metrics/metrics.go +++ b/pkg/infra/metrics/metrics.go @@ -159,6 +159,12 @@ var ( // StatsTotalDataSources is a metric total number of defined datasources, labeled by pluginId StatsTotalDataSources *prometheus.GaugeVec + // StatsTotalAnnotations is a metric of total number of annotations stored in Grafana. + StatsTotalAnnotations prometheus.Gauge + + // StatsTotalDashboardVersions is a metric of total number of dashboard versions stored in Grafana. + StatsTotalDashboardVersions prometheus.Gauge + // grafanaBuildVersion is a metric with a constant '1' value labeled by version, revision, branch, and goversion from which Grafana was built grafanaBuildVersion *prometheus.GaugeVec @@ -483,6 +489,18 @@ func init() { Help: "A metric with a constant '1' value labeled by pluginId, pluginType and version from which Grafana plugin was built", Namespace: ExporterName, }, []string{"plugin_id", "plugin_type", "version"}) + + StatsTotalDashboardVersions = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "stat_totals_dashboard_versions", + Help: "total amount of dashboard versions in the database", + Namespace: ExporterName, + }) + + StatsTotalAnnotations = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "stat_totals_annotations", + Help: "total amount of annotations in the database", + Namespace: ExporterName, + }) } // SetBuildInformation sets the build information for this binary @@ -550,6 +568,8 @@ func initMetricVars() { StatsTotalDataSources, grafanaBuildVersion, grafanaPluginBuildInfoDesc, + StatsTotalDashboardVersions, + StatsTotalAnnotations, ) } diff --git a/pkg/infra/usagestats/usage_stats.go b/pkg/infra/usagestats/usage_stats.go index 6d22e55d03c..36f5c4ebe8e 100644 --- a/pkg/infra/usagestats/usage_stats.go +++ b/pkg/infra/usagestats/usage_stats.go @@ -61,6 +61,8 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { metrics["stats.snapshots.count"] = statsQuery.Result.Snapshots metrics["stats.teams.count"] = statsQuery.Result.Teams metrics["stats.total_auth_token.count"] = statsQuery.Result.AuthTokens + metrics["stats.dashboard_versions.count"] = statsQuery.Result.DashboardVersions + metrics["stats.annotations.count"] = statsQuery.Result.Annotations metrics["stats.valid_license.count"] = getValidLicenseCount(uss.License.HasValidLicense()) metrics["stats.edition.oss.count"] = getOssEditionCount() metrics["stats.edition.enterprise.count"] = getEnterpriseEditionCount() @@ -212,6 +214,8 @@ func (uss *UsageStatsService) updateTotalStats() { metrics.StatsTotalActiveEditors.Set(float64(statsQuery.Result.ActiveEditors)) metrics.StatsTotalAdmins.Set(float64(statsQuery.Result.Admins)) metrics.StatsTotalActiveAdmins.Set(float64(statsQuery.Result.ActiveAdmins)) + metrics.StatsTotalDashboardVersions.Set(float64(statsQuery.Result.DashboardVersions)) + metrics.StatsTotalAnnotations.Set(float64(statsQuery.Result.Annotations)) dsStats := models.GetDataSourceStatsQuery{} if err := uss.Bus.Dispatch(&dsStats); err != nil { diff --git a/pkg/infra/usagestats/usage_stats_test.go b/pkg/infra/usagestats/usage_stats_test.go index afa49360cc4..917ccd6d3a2 100644 --- a/pkg/infra/usagestats/usage_stats_test.go +++ b/pkg/infra/usagestats/usage_stats_test.go @@ -50,6 +50,8 @@ func TestMetrics(t *testing.T) { Snapshots: 13, Teams: 14, AuthTokens: 15, + DashboardVersions: 16, + Annotations: 17, } getSystemStatsQuery = query return nil @@ -238,6 +240,8 @@ func TestMetrics(t *testing.T) { So(metrics.Get("stats.teams.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Teams) So(metrics.Get("stats.total_auth_token.count").MustInt64(), ShouldEqual, 15) So(metrics.Get("stats.avg_auth_token_per_user.count").MustInt64(), ShouldEqual, 5) + So(metrics.Get("stats.dashboard_versions.count").MustInt64(), ShouldEqual, 16) + So(metrics.Get("stats.annotations.count").MustInt64(), ShouldEqual, 17) So(metrics.Get("stats.ds."+models.DS_ES+".count").MustInt(), ShouldEqual, 9) So(metrics.Get("stats.ds."+models.DS_PROMETHEUS+".count").MustInt(), ShouldEqual, 10) diff --git a/pkg/models/stats.go b/pkg/models/stats.go index 498cdd5c0bf..05df8182566 100644 --- a/pkg/models/stats.go +++ b/pkg/models/stats.go @@ -16,6 +16,8 @@ type SystemStats struct { Folders int64 ProvisionedDashboards int64 AuthTokens int64 + DashboardVersions int64 + Annotations int64 Admins int Editors int diff --git a/pkg/services/sqlstore/stats.go b/pkg/services/sqlstore/stats.go index 6f80a8017e6..685a0783ee4 100644 --- a/pkg/services/sqlstore/stats.go +++ b/pkg/services/sqlstore/stats.go @@ -75,6 +75,8 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error { sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_provisioning") + `) AS provisioned_dashboards,`) sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_snapshot") + `) AS snapshots,`) + sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_version") + `) AS dashboard_versions,`) + sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("annotation") + `) AS annotations,`) sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("team") + `) AS teams,`) sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("user_auth_token") + `) AS auth_tokens,`)