Files
grafana/pkg/services/serviceaccounts/manager/stats.go
Jo 5c1f614d3c Service Accounts: Separate metrics logic from store (#54085)
* separate stats logic from store

* remove in_teams unused stat

* use init instead
2022-08-23 08:24:55 -04:00

58 lines
1.4 KiB
Go

package manager
import (
"context"
"github.com/prometheus/client_golang/prometheus"
)
const (
ExporterName = "grafana"
)
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
Initialised bool = false
)
func init() {
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 (sa *ServiceAccountsService) getUsageMetrics(ctx context.Context) (map[string]interface{}, error) {
stats := map[string]interface{}{}
sqlStats, err := sa.store.GetUsageMetrics(ctx)
if err != nil {
return nil, err
}
stats["stats.serviceaccounts.count"] = sqlStats.ServiceAccounts
stats["stats.serviceaccounts.tokens.count"] = sqlStats.Tokens
MStatTotalServiceAccountTokens.Set(float64(sqlStats.Tokens))
MStatTotalServiceAccounts.Set(float64(sqlStats.ServiceAccounts))
return stats, nil
}