Commit e07de80b by Dan Cech Committed by Torkel Ödegaard

Fix issues with metric reporting (#11518)

* report active users in graphite stats

* use bus to publish system stats

* metrics: avoid using events unless we have to

this commit also changes the default interval
for updating the stats gauges. Seems like the old values
was a product of previous metrics implementation
parent ffe9b426
...@@ -54,6 +54,7 @@ var ( ...@@ -54,6 +54,7 @@ var (
M_Alerting_Active_Alerts prometheus.Gauge M_Alerting_Active_Alerts prometheus.Gauge
M_StatTotal_Dashboards prometheus.Gauge M_StatTotal_Dashboards prometheus.Gauge
M_StatTotal_Users prometheus.Gauge M_StatTotal_Users prometheus.Gauge
M_StatActive_Users prometheus.Gauge
M_StatTotal_Orgs prometheus.Gauge M_StatTotal_Orgs prometheus.Gauge
M_StatTotal_Playlists prometheus.Gauge M_StatTotal_Playlists prometheus.Gauge
M_Grafana_Version *prometheus.GaugeVec M_Grafana_Version *prometheus.GaugeVec
...@@ -253,6 +254,12 @@ func init() { ...@@ -253,6 +254,12 @@ func init() {
Namespace: exporterName, Namespace: exporterName,
}) })
M_StatActive_Users = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_active_users",
Help: "number of active users",
Namespace: exporterName,
})
M_StatTotal_Orgs = prometheus.NewGauge(prometheus.GaugeOpts{ M_StatTotal_Orgs = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_total_orgs", Name: "stat_total_orgs",
Help: "total amount of orgs", Help: "total amount of orgs",
...@@ -270,7 +277,6 @@ func init() { ...@@ -270,7 +277,6 @@ func init() {
Help: "Information about the Grafana", Help: "Information about the Grafana",
Namespace: exporterName, Namespace: exporterName,
}, []string{"version"}) }, []string{"version"})
} }
func initMetricVars(settings *MetricSettings) { func initMetricVars(settings *MetricSettings) {
...@@ -305,6 +311,7 @@ func initMetricVars(settings *MetricSettings) { ...@@ -305,6 +311,7 @@ func initMetricVars(settings *MetricSettings) {
M_Alerting_Active_Alerts, M_Alerting_Active_Alerts,
M_StatTotal_Dashboards, M_StatTotal_Dashboards,
M_StatTotal_Users, M_StatTotal_Users,
M_StatActive_Users,
M_StatTotal_Orgs, M_StatTotal_Orgs,
M_StatTotal_Playlists, M_StatTotal_Playlists,
M_Grafana_Version) M_Grafana_Version)
...@@ -315,35 +322,36 @@ func initMetricVars(settings *MetricSettings) { ...@@ -315,35 +322,36 @@ func initMetricVars(settings *MetricSettings) {
func instrumentationLoop(settings *MetricSettings) chan struct{} { func instrumentationLoop(settings *MetricSettings) chan struct{} {
M_Instance_Start.Inc() M_Instance_Start.Inc()
// set the total stats gauges before we publishing metrics
updateTotalStats()
onceEveryDayTick := time.NewTicker(time.Hour * 24) onceEveryDayTick := time.NewTicker(time.Hour * 24)
secondTicker := time.NewTicker(time.Second * time.Duration(settings.IntervalSeconds)) everyMinuteTicker := time.NewTicker(time.Minute)
defer onceEveryDayTick.Stop()
defer everyMinuteTicker.Stop()
for { for {
select { select {
case <-onceEveryDayTick.C: case <-onceEveryDayTick.C:
sendUsageStats() sendUsageStats()
case <-secondTicker.C: case <-everyMinuteTicker.C:
updateTotalStats() updateTotalStats()
} }
} }
} }
var metricPublishCounter int64 = 0
func updateTotalStats() { func updateTotalStats() {
metricPublishCounter++ statsQuery := models.GetSystemStatsQuery{}
if metricPublishCounter == 1 || metricPublishCounter%10 == 0 { if err := bus.Dispatch(&statsQuery); err != nil {
statsQuery := models.GetSystemStatsQuery{} metricsLogger.Error("Failed to get system stats", "error", err)
if err := bus.Dispatch(&statsQuery); err != nil { return
metricsLogger.Error("Failed to get system stats", "error", err)
return
}
M_StatTotal_Dashboards.Set(float64(statsQuery.Result.Dashboards))
M_StatTotal_Users.Set(float64(statsQuery.Result.Users))
M_StatTotal_Playlists.Set(float64(statsQuery.Result.Playlists))
M_StatTotal_Orgs.Set(float64(statsQuery.Result.Orgs))
} }
M_StatTotal_Dashboards.Set(float64(statsQuery.Result.Dashboards))
M_StatTotal_Users.Set(float64(statsQuery.Result.Users))
M_StatActive_Users.Set(float64(statsQuery.Result.ActiveUsers))
M_StatTotal_Playlists.Set(float64(statsQuery.Result.Playlists))
M_StatTotal_Orgs.Set(float64(statsQuery.Result.Orgs))
} }
func sendUsageStats() { func sendUsageStats() {
......
...@@ -68,6 +68,7 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error { ...@@ -68,6 +68,7 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
} }
query.Result = &stats query.Result = &stats
return err return err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment