Browse Source

feat(internal metrics): added some total stats to metrics reporting, closes #5865

Torkel Ödegaard 9 years ago
parent
commit
6574dfacfb
5 changed files with 46 additions and 8 deletions
  1. 4 4
      pkg/metrics/gauge.go
  2. 2 0
      pkg/metrics/graphite.go
  3. 12 0
      pkg/metrics/metrics.go
  4. 24 0
      pkg/metrics/publish.go
  5. 4 4
      pkg/models/stats.go

+ 4 - 4
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.

+ 2 - 0
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)

+ 12 - 0
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")
 }

+ 24 - 0
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

+ 4 - 4
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 {