From 6574dfacfb2fb93faf6d884da5709f83b7f49f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 22 Sep 2016 11:16:19 +0200 Subject: [PATCH] feat(internal metrics): added some total stats to metrics reporting, closes #5865 --- pkg/metrics/gauge.go | 8 ++++---- pkg/metrics/graphite.go | 2 ++ pkg/metrics/metrics.go | 12 ++++++++++++ pkg/metrics/publish.go | 24 ++++++++++++++++++++++++ pkg/models/stats.go | 8 ++++---- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/pkg/metrics/gauge.go b/pkg/metrics/gauge.go index 01cd584cb39..59758aa4ecb 100644 --- a/pkg/metrics/gauge.go +++ b/pkg/metrics/gauge.go @@ -24,10 +24,10 @@ func NewGauge(meta *MetricMeta) Gauge { } } -func RegGauge(meta *MetricMeta) Gauge { - g := NewGauge(meta) - MetricStats.Register(g) - return g +func RegGauge(name string, tagStrings ...string) Gauge { + tr := NewGauge(NewMetricMeta(name, tagStrings)) + MetricStats.Register(tr) + return tr } // GaugeSnapshot is a read-only copy of another Gauge. diff --git a/pkg/metrics/graphite.go b/pkg/metrics/graphite.go index e88df2ebb1b..59c992776de 100644 --- a/pkg/metrics/graphite.go +++ b/pkg/metrics/graphite.go @@ -63,6 +63,8 @@ func (this *GraphitePublisher) Publish(metrics []Metric) { switch metric := m.(type) { case Counter: this.addCount(buf, metricName+".count", metric.Count(), now) + case Gauge: + this.addCount(buf, metricName, metric.Value(), now) case Timer: percentiles := metric.Percentiles([]float64{0.25, 0.75, 0.90, 0.99}) this.addCount(buf, metricName+".count", metric.Count(), now) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index bbe580de218..002f2369c9b 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -49,6 +49,12 @@ var ( // Timers M_DataSource_ProxyReq_Timer Timer M_Alerting_Exeuction_Time Timer + + // StatTotals + M_StatTotal_Dashboards Gauge + M_StatTotal_Users Gauge + M_StatTotal_Orgs Gauge + M_StatTotal_Playlists Gauge ) func initMetricVars(settings *MetricSettings) { @@ -105,4 +111,10 @@ func initMetricVars(settings *MetricSettings) { // Timers M_DataSource_ProxyReq_Timer = RegTimer("api.dataproxy.request.all") M_Alerting_Exeuction_Time = RegTimer("alerting.execution_time") + + // StatTotals + M_StatTotal_Dashboards = RegGauge("stat_totals", "stat", "dashboards") + M_StatTotal_Users = RegGauge("stat_totals", "stat", "users") + M_StatTotal_Orgs = RegGauge("stat_totals", "stat", "orgs") + M_StatTotal_Playlists = RegGauge("stat_totals", "stat", "playlists") } diff --git a/pkg/metrics/publish.go b/pkg/metrics/publish.go index 9c1de6e05d2..c5bb7f61f0a 100644 --- a/pkg/metrics/publish.go +++ b/pkg/metrics/publish.go @@ -15,6 +15,7 @@ import ( ) var metricsLogger log.Logger = log.New("metrics") +var metricPublishCounter int64 = 0 func Init() { settings := readSettings() @@ -45,12 +46,35 @@ func sendMetrics(settings *MetricSettings) { return } + updateTotalStats() + metrics := MetricStats.GetSnapshots() for _, publisher := range settings.Publishers { publisher.Publish(metrics) } } +func updateTotalStats() { + + // every interval also publish totals + metricPublishCounter++ + if metricPublishCounter%2 == 0 { + metricsLogger.Info("Stats!") + + // get stats + statsQuery := m.GetSystemStatsQuery{} + if err := bus.Dispatch(&statsQuery); err != nil { + metricsLogger.Error("Failed to get system stats", "error", err) + return + } + + M_StatTotal_Dashboards.Update(statsQuery.Result.DashboardCount) + M_StatTotal_Users.Update(statsQuery.Result.UserCount) + M_StatTotal_Playlists.Update(statsQuery.Result.PlaylistCount) + M_StatTotal_Orgs.Update(statsQuery.Result.OrgCount) + } +} + func sendUsageStats() { if !setting.ReportingEnabled { return diff --git a/pkg/models/stats.go b/pkg/models/stats.go index fa9cfdab6e8..067dec763e5 100644 --- a/pkg/models/stats.go +++ b/pkg/models/stats.go @@ -1,10 +1,10 @@ package models type SystemStats struct { - DashboardCount int - UserCount int - OrgCount int - PlaylistCount int + DashboardCount int64 + UserCount int64 + OrgCount int64 + PlaylistCount int64 } type DataSourceStats struct {