Service Accounts: Separate metrics logic from store (#54085)
* separate stats logic from store * remove in_teams unused stat * use init instead
This commit is contained in:
@@ -2,70 +2,22 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"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
|
||||
|
||||
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) GetUsageMetrics(ctx context.Context) (map[string]interface{}, error) {
|
||||
stats := map[string]interface{}{}
|
||||
func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (*serviceaccounts.Stats, error) {
|
||||
dialect := s.sqlStore.Dialect
|
||||
|
||||
sb := &sqlstore.SQLBuilder{}
|
||||
dialect := s.sqlStore.Dialect
|
||||
sb.Write("SELECT ")
|
||||
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("user") +
|
||||
` WHERE is_service_account = ` + dialect.BooleanStr(true) + `) AS serviceaccounts,`)
|
||||
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("api_key") +
|
||||
` WHERE service_account_id IS NOT NULL ) AS serviceaccount_tokens,`)
|
||||
// Add count to how many service accounts are in teams
|
||||
sb.Write(`(SELECT COUNT(*) FROM team_member
|
||||
JOIN ` + dialect.Quote("user") + ` on team_member.user_id=` + dialect.Quote("user") + `.id
|
||||
WHERE ` + dialect.Quote("user") + `.is_service_account=` + dialect.BooleanStr(true) + ` ) as serviceaccounts_in_teams`)
|
||||
` WHERE service_account_id IS NOT NULL ) AS serviceaccount_tokens`)
|
||||
|
||||
type saStats struct {
|
||||
ServiceAccounts int64 `xorm:"serviceaccounts"`
|
||||
Tokens int64 `xorm:"serviceaccount_tokens"`
|
||||
InTeams int64 `xorm:"serviceaccounts_in_teams"`
|
||||
}
|
||||
|
||||
var sqlStats saStats
|
||||
var sqlStats serviceaccounts.Stats
|
||||
if err := s.sqlStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
_, err := sess.SQL(sb.GetSQLString(), sb.GetParams()...).Get(&sqlStats)
|
||||
return err
|
||||
@@ -73,12 +25,5 @@ func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (map[str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats["stats.serviceaccounts.count"] = sqlStats.ServiceAccounts
|
||||
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
|
||||
return &sqlStats, nil
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/apikeygen"
|
||||
"github.com/grafana/grafana/pkg/services/apikey"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -16,7 +15,6 @@ 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)
|
||||
@@ -27,7 +25,6 @@ func TestStore_UsageStats(t *testing.T) {
|
||||
OrgId: sa.OrgID,
|
||||
Key: key.HashedKey,
|
||||
SecondsToLive: 0,
|
||||
Result: &apikey.APIKey{},
|
||||
}
|
||||
|
||||
err = store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
||||
@@ -36,7 +33,6 @@ func TestStore_UsageStats(t *testing.T) {
|
||||
stats, err := store.GetUsageMetrics(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, int64(1), stats["stats.serviceaccounts.count"].(int64))
|
||||
assert.Equal(t, int64(1), stats["stats.serviceaccounts.tokens.count"].(int64))
|
||||
assert.Equal(t, int64(0), stats["stats.serviceaccounts.in_teams.count"].(int64))
|
||||
assert.Equal(t, int64(1), stats.ServiceAccounts)
|
||||
assert.Equal(t, int64(1), stats.Tokens)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user