Commit 1aadb442 by Carl Bergquist Committed by GitHub

metrics: expose stats about roles as metrics (#17469)

parent c31b939e
...@@ -43,8 +43,10 @@ var ( ...@@ -43,8 +43,10 @@ var (
// Timers // Timers
M_DataSource_ProxyReq_Timer prometheus.Summary M_DataSource_ProxyReq_Timer prometheus.Summary
M_Alerting_Execution_Time prometheus.Summary M_Alerting_Execution_Time prometheus.Summary
)
// StatTotals // StatTotals
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
...@@ -52,6 +54,13 @@ var ( ...@@ -52,6 +54,13 @@ var (
M_StatTotal_Orgs prometheus.Gauge M_StatTotal_Orgs prometheus.Gauge
M_StatTotal_Playlists prometheus.Gauge M_StatTotal_Playlists prometheus.Gauge
StatsTotalViewers prometheus.Gauge
StatsTotalEditors prometheus.Gauge
StatsTotalAdmins prometheus.Gauge
StatsTotalActiveViewers prometheus.Gauge
StatsTotalActiveEditors prometheus.Gauge
StatsTotalActiveAdmins prometheus.Gauge
// M_Grafana_Version is a gauge that contains build info about this binary // M_Grafana_Version is a gauge that contains build info about this binary
// //
// Deprecated: use M_Grafana_Build_Version instead. // Deprecated: use M_Grafana_Build_Version instead.
...@@ -274,6 +283,42 @@ func init() { ...@@ -274,6 +283,42 @@ func init() {
Namespace: exporterName, Namespace: exporterName,
}) })
StatsTotalViewers = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_viewers",
Help: "total amount of viewers",
Namespace: exporterName,
})
StatsTotalEditors = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_editors",
Help: "total amount of editors",
Namespace: exporterName,
})
StatsTotalAdmins = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_admins",
Help: "total amount of admins",
Namespace: exporterName,
})
StatsTotalActiveViewers = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_active_viewers",
Help: "total amount of viewers",
Namespace: exporterName,
})
StatsTotalActiveEditors = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_active_editors",
Help: "total amount of active editors",
Namespace: exporterName,
})
StatsTotalActiveAdmins = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_active_admins",
Help: "total amount of active admins",
Namespace: exporterName,
})
M_Grafana_Version = prometheus.NewGaugeVec(prometheus.GaugeOpts{ M_Grafana_Version = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "info", Name: "info",
Help: "Information about the Grafana. This metric is deprecated. please use `grafana_build_info`", Help: "Information about the Grafana. This metric is deprecated. please use `grafana_build_info`",
...@@ -339,6 +384,12 @@ func initMetricVars() { ...@@ -339,6 +384,12 @@ func initMetricVars() {
M_StatTotal_Orgs, M_StatTotal_Orgs,
M_StatTotal_Playlists, M_StatTotal_Playlists,
M_Grafana_Version, M_Grafana_Version,
StatsTotalViewers,
StatsTotalEditors,
StatsTotalAdmins,
StatsTotalActiveViewers,
StatsTotalActiveEditors,
StatsTotalActiveAdmins,
grafanaBuildVersion) grafanaBuildVersion)
} }
......
...@@ -166,6 +166,12 @@ func (uss *UsageStatsService) updateTotalStats() { ...@@ -166,6 +166,12 @@ func (uss *UsageStatsService) updateTotalStats() {
metrics.M_StatActive_Users.Set(float64(statsQuery.Result.ActiveUsers)) metrics.M_StatActive_Users.Set(float64(statsQuery.Result.ActiveUsers))
metrics.M_StatTotal_Playlists.Set(float64(statsQuery.Result.Playlists)) metrics.M_StatTotal_Playlists.Set(float64(statsQuery.Result.Playlists))
metrics.M_StatTotal_Orgs.Set(float64(statsQuery.Result.Orgs)) metrics.M_StatTotal_Orgs.Set(float64(statsQuery.Result.Orgs))
metrics.StatsTotalViewers.Set(float64(statsQuery.Result.Viewers))
metrics.StatsTotalActiveViewers.Set(float64(statsQuery.Result.ActiveViewers))
metrics.StatsTotalEditors.Set(float64(statsQuery.Result.Editors))
metrics.StatsTotalActiveEditors.Set(float64(statsQuery.Result.ActiveEditors))
metrics.StatsTotalAdmins.Set(float64(statsQuery.Result.Admins))
metrics.StatsTotalActiveAdmins.Set(float64(statsQuery.Result.ActiveAdmins))
} }
func getEdition() string { func getEdition() string {
......
...@@ -16,6 +16,14 @@ type SystemStats struct { ...@@ -16,6 +16,14 @@ type SystemStats struct {
Folders int64 Folders int64
ProvisionedDashboards int64 ProvisionedDashboards int64
AuthTokens int64 AuthTokens int64
Admins int
Editors int
Viewers int
ActiveAdmins int
ActiveEditors int
ActiveViewers int
ActiveSessions int
} }
type DataSourceStats struct { type DataSourceStats struct {
......
...@@ -2,8 +2,7 @@ package alerting ...@@ -2,8 +2,7 @@ package alerting
import "github.com/grafana/grafana/pkg/components/null" import "github.com/grafana/grafana/pkg/components/null"
// Job holds state about when the alert rule should // Job holds state about when the alert rule should be evaluated.
// be evaluated.
type Job struct { type Job struct {
Offset int64 Offset int64
OffsetWait bool OffsetWait bool
......
...@@ -75,7 +75,11 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error { ...@@ -75,7 +75,11 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_provisioning") + `) AS provisioned_dashboards,`) 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_snapshot") + `) AS snapshots,`)
sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("team") + `) AS teams,`) 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`) sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("user_auth_token") + `) AS auth_tokens,`)
sb.Write(roleCounterSQL("Viewer", "viewers")+`,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Editor", "editors")+`,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Admin", "admins")+``, activeUserDeadlineDate)
var stats m.SystemStats var stats m.SystemStats
_, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats) _, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats)
...@@ -88,33 +92,31 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error { ...@@ -88,33 +92,31 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
return err return err
} }
func roleCounterSQL(role, alias string) string {
return `
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` as u
WHERE
(SELECT COUNT(*)
FROM org_user
WHERE org_user.user_id=u.id
AND org_user.role='` + role + `')>0
) as ` + alias + `,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` as u
WHERE
(SELECT COUNT(*)
FROM org_user
WHERE org_user.user_id=u.id
AND org_user.role='` + role + `')>0
AND u.last_seen_at>?
) as active_` + alias
}
func GetAdminStats(query *m.GetAdminStatsQuery) error { func GetAdminStats(query *m.GetAdminStatsQuery) error {
activeEndDate := time.Now().Add(-activeUserTimeLimit) activeEndDate := time.Now().Add(-activeUserTimeLimit)
roleCounter := func(role, alias string) string {
sql :=
`
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` as u
WHERE
(SELECT COUNT(*)
FROM org_user
WHERE org_user.user_id=u.id
AND org_user.role='` + role + `')>0
) as ` + alias + `,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` as u
WHERE
(SELECT COUNT(*)
FROM org_user
WHERE org_user.user_id=u.id
AND org_user.role='` + role + `')>0
AND u.last_seen_at>?
) as active_` + alias
return sql
}
var rawSql = `SELECT var rawSql = `SELECT
( (
...@@ -156,9 +158,9 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error { ...@@ -156,9 +158,9 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error {
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` where last_seen_at > ? FROM ` + dialect.Quote("user") + ` where last_seen_at > ?
) as active_users, ) as active_users,
` + roleCounter("Admin", "admins") + `, ` + roleCounterSQL("Admin", "admins") + `,
` + roleCounter("Editor", "editors") + `, ` + roleCounterSQL("Editor", "editors") + `,
` + roleCounter("Viewer", "viewers") + `, ` + roleCounterSQL("Viewer", "viewers") + `,
( (
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("user_auth_token") + ` where rotated_at > ? FROM ` + dialect.Quote("user_auth_token") + ` where rotated_at > ?
......
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