|
|
|
@ -6,21 +6,20 @@ import ( |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus" |
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
|
"github.com/grafana/grafana/pkg/login/social" |
|
|
|
|
"github.com/grafana/grafana/pkg/models" |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins" |
|
|
|
|
"github.com/grafana/grafana/pkg/registry" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
|
"github.com/grafana/grafana/pkg/registry" |
|
|
|
|
"github.com/grafana/grafana/pkg/setting" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var metricsLogger log.Logger = log.New("metrics") |
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
|
registry.RegisterService(&UsageStatsService{ |
|
|
|
|
registry.RegisterService(&Service{ |
|
|
|
|
log: log.New("infra.usagestats"), |
|
|
|
|
externalMetrics: make(map[string]MetricFunc), |
|
|
|
|
}) |
|
|
|
@ -33,7 +32,7 @@ type UsageStats interface { |
|
|
|
|
|
|
|
|
|
type MetricFunc func() (interface{}, error) |
|
|
|
|
|
|
|
|
|
type UsageStatsService struct { |
|
|
|
|
type Service struct { |
|
|
|
|
Cfg *setting.Cfg `inject:""` |
|
|
|
|
Bus bus.Bus `inject:""` |
|
|
|
|
SQLStore *sqlstore.SQLStore `inject:""` |
|
|
|
@ -48,12 +47,12 @@ type UsageStatsService struct { |
|
|
|
|
concurrentUserStatsCache memoConcurrentUserStats |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (uss *UsageStatsService) Init() error { |
|
|
|
|
func (uss *Service) Init() error { |
|
|
|
|
uss.oauthProviders = social.GetOAuthProviders(uss.Cfg) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (uss *UsageStatsService) Run(ctx context.Context) error { |
|
|
|
|
func (uss *Service) Run(ctx context.Context) error { |
|
|
|
|
uss.updateTotalStats() |
|
|
|
|
|
|
|
|
|
sendReportTicker := time.NewTicker(time.Hour * 24) |
|
|
|
@ -83,7 +82,7 @@ type memoConcurrentUserStats struct { |
|
|
|
|
|
|
|
|
|
const concurrentUserStatsCacheLifetime = time.Hour |
|
|
|
|
|
|
|
|
|
func (uss *UsageStatsService) GetConcurrentUsersStats(ctx context.Context) (*concurrentUsersStats, error) { |
|
|
|
|
func (uss *Service) GetConcurrentUsersStats(ctx context.Context) (*concurrentUsersStats, error) { |
|
|
|
|
memoizationPeriod := time.Now().Add(-concurrentUserStatsCacheLifetime) |
|
|
|
|
if !uss.concurrentUserStatsCache.memoized.Before(memoizationPeriod) { |
|
|
|
|
return uss.concurrentUserStatsCache.stats, nil |
|
|
|
|