ServiceAccounts: Add Prometheus metrics service (#51831)

* ServiceAccounts: Add Prometheus metrics service

* add missing init in tests
This commit is contained in:
Jguer
2022-07-07 10:03:16 -04:00
committed by GitHub
parent 10cb84e401
commit db9f80a7a3
6 changed files with 74 additions and 0 deletions
@@ -2,10 +2,69 @@ package database
import (
"context"
"sync"
"time"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/prometheus/client_golang/prometheus"
)
const (
ExporterName = "grafana"
metricsCollectionInterval = time.Minute * 30
)
var (
// MStatTotalServiceAccounts is a metric gauge for total number of service accounts
MStatTotalServiceAccounts prometheus.Gauge
// MStatTotalServiceAccountTokens is a metric gauge for total number of service account tokens
MStatTotalServiceAccountTokens prometheus.Gauge
once sync.Once
Initialised bool = false
)
func InitMetrics() {
once.Do(func() {
MStatTotalServiceAccounts = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_total_service_accounts",
Help: "total amount of service accounts",
Namespace: ExporterName,
})
MStatTotalServiceAccountTokens = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_total_service_account_tokens",
Help: "total amount of service account tokens",
Namespace: ExporterName,
})
prometheus.MustRegister(
MStatTotalServiceAccounts,
MStatTotalServiceAccountTokens,
)
})
}
func (s *ServiceAccountsStoreImpl) RunMetricsCollection(ctx context.Context) error {
if _, err := s.GetUsageMetrics(ctx); err != nil {
s.log.Warn("Failed to get usage metrics", "error", err.Error())
}
updateStatsTicker := time.NewTicker(metricsCollectionInterval)
defer updateStatsTicker.Stop()
for {
select {
case <-updateStatsTicker.C:
if _, err := s.GetUsageMetrics(ctx); err != nil {
s.log.Warn("Failed to get usage metrics", "error", err.Error())
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (map[string]interface{}, error) {
stats := map[string]interface{}{}
@@ -39,5 +98,8 @@ func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (map[str
stats["stats.serviceaccounts.tokens.count"] = sqlStats.Tokens
stats["stats.serviceaccounts.in_teams.count"] = sqlStats.InTeams
MStatTotalServiceAccountTokens.Set(float64(sqlStats.Tokens))
MStatTotalServiceAccounts.Set(float64(sqlStats.ServiceAccounts))
return stats, nil
}
@@ -16,6 +16,7 @@ func TestStore_UsageStats(t *testing.T) {
saToCreate := tests.TestUser{Login: "servicetestwithTeam@admin", IsServiceAccount: true}
db, store := setupTestDatabase(t)
sa := tests.SetupUserServiceAccount(t, db, saToCreate)
InitMetrics()
keyName := t.Name()
key, err := apikeygen.New(sa.OrgID, keyName)