Skip to content

Commit

Permalink
Export GetUsageStats and use registry to collect them
Browse files Browse the repository at this point in the history
  • Loading branch information
kalleep committed Apr 3, 2024
1 parent 7be6270 commit 0b7ae57
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
3 changes: 3 additions & 0 deletions pkg/registry/usagestatssvcs/usage_stats_providers_registry.go
Expand Up @@ -3,16 +3,19 @@ package usagestatssvcs
import (
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/authn/authnimpl"
"github.com/grafana/grafana/pkg/services/user"
)

func ProvideUsageStatsProvidersRegistry(
accesscontrol accesscontrol.Service,
user user.Service,
authnSvc *authnimpl.Service,
) *UsageStatsProvidersRegistry {
return NewUsageStatsProvidersRegistry(
accesscontrol,
user,
authnSvc,
)
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/services/authn/authn.go
Expand Up @@ -86,10 +86,8 @@ type Service interface {
RedirectURL(ctx context.Context, client string, r *Request) (*Redirect, error)
// Logout revokes session token and does additional clean up if client used to authenticate supports it
Logout(ctx context.Context, user identity.Requester, sessionToken *usertoken.UserToken) (*Redirect, error)

// ResolveIdentity resolves an identity from org and namespace id.
ResolveIdentity(ctx context.Context, orgID int64, namespaceID string) (*Identity, error)

// RegisterClient will register a new authn.Client that can be used for authentication
RegisterClient(c Client)
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/services/authn/authnimpl/service.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/network"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/infra/usagestats"
"github.com/grafana/grafana/pkg/services/auth"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/authn"
Expand Down Expand Up @@ -47,8 +46,8 @@ func ProvideIdentitySynchronizer(s *Service) authn.IdentitySynchronizer {

func ProvideService(
cfg *setting.Cfg, tracer tracing.Tracer,
sessionService auth.UserTokenService, usageStats usagestats.Service,
authInfoService login.AuthInfoService, registerer prometheus.Registerer,
sessionService auth.UserTokenService, authInfoService login.AuthInfoService,
registerer prometheus.Registerer,
) *Service {
s := &Service{
log: log.New("authn.service"),
Expand All @@ -63,7 +62,6 @@ func ProvideService(
postLoginHooks: newQueue[authn.PostLoginHookFn](),
}

usageStats.RegisterMetricsFunc(s.getUsageStats)
return s
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/services/authn/authnimpl/usage_stats.go
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/grafana/grafana/pkg/services/authn"
)

func (s *Service) getUsageStats(ctx context.Context) (map[string]any, error) {
func (s *Service) GetUsageStats(ctx context.Context) map[string]any {
m := map[string]any{}

// Add stats about auth configuration
Expand Down Expand Up @@ -53,5 +53,5 @@ func (s *Service) getUsageStats(ctx context.Context) (map[string]any, error) {
}
}

return m, nil
return m
}
5 changes: 2 additions & 3 deletions pkg/services/authn/authnimpl/usage_stats_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/grafana/pkg/services/authn/authntest"
)

func TestService_getUsageStats(t *testing.T) {
func TestService_GetUsageStats(t *testing.T) {
svc := setupTests(t, func(svc *Service) {
svc.RegisterClient(
&authntest.FakeClient{ExpectedErr: nil, ExpectedName: "test", ExpectedPriority: 1, ExpectedStats: map[string]any{"stats.test.enabled.count": 1}})
Expand All @@ -26,8 +26,7 @@ func TestService_getUsageStats(t *testing.T) {
svc.cfg.EditorsCanAdmin = true
svc.cfg.ViewersCanEdit = true

got, err := svc.getUsageStats(context.Background())
require.NoError(t, err)
got := svc.GetUsageStats(context.Background())
want := map[string]any{"stats.auth_enabled.anonymous.count": 0,
"stats.auth_enabled.auth_proxy.count": 1,
"stats.auth_enabled.basic_auth.count": 1,
Expand Down

0 comments on commit 0b7ae57

Please sign in to comment.