Commit fac34f65 by Vardan Torosyan Committed by GitHub

Usage Stats: Allow to add additional metrics to the stats (#29774)

The change allows to add external, additional metrics which will be reported with the rest of hardcoded metrics, allowing to add metrics without a need to change the service itself.
parent f4fcd875
...@@ -21,6 +21,8 @@ func init() { ...@@ -21,6 +21,8 @@ func init() {
registry.RegisterService(&UsageStatsService{}) registry.RegisterService(&UsageStatsService{})
} }
type MetricFunc func() (interface{}, error)
type UsageStatsService struct { type UsageStatsService struct {
Cfg *setting.Cfg `inject:""` Cfg *setting.Cfg `inject:""`
Bus bus.Bus `inject:""` Bus bus.Bus `inject:""`
...@@ -31,11 +33,13 @@ type UsageStatsService struct { ...@@ -31,11 +33,13 @@ type UsageStatsService struct {
log log.Logger log log.Logger
oauthProviders map[string]bool oauthProviders map[string]bool
externalMetrics map[string]MetricFunc
} }
func (uss *UsageStatsService) Init() error { func (uss *UsageStatsService) Init() error {
uss.log = log.New("infra.usagestats") uss.log = log.New("infra.usagestats")
uss.oauthProviders = social.GetOAuthProviders(uss.Cfg) uss.oauthProviders = social.GetOAuthProviders(uss.Cfg)
uss.externalMetrics = make(map[string]MetricFunc)
return nil return nil
} }
......
...@@ -72,6 +72,8 @@ func (uss *UsageStatsService) GetUsageReport() (UsageReport, error) { ...@@ -72,6 +72,8 @@ func (uss *UsageStatsService) GetUsageReport() (UsageReport, error) {
metrics["stats.edition.oss.count"] = getOssEditionCount() metrics["stats.edition.oss.count"] = getOssEditionCount()
metrics["stats.edition.enterprise.count"] = getEnterpriseEditionCount() metrics["stats.edition.enterprise.count"] = getEnterpriseEditionCount()
uss.registerExternalMetrics(metrics)
userCount := statsQuery.Result.Users userCount := statsQuery.Result.Users
avgAuthTokensPerUser := statsQuery.Result.AuthTokens avgAuthTokensPerUser := statsQuery.Result.AuthTokens
if userCount != 0 { if userCount != 0 {
...@@ -186,6 +188,21 @@ func (uss *UsageStatsService) GetUsageReport() (UsageReport, error) { ...@@ -186,6 +188,21 @@ func (uss *UsageStatsService) GetUsageReport() (UsageReport, error) {
return report, nil return report, nil
} }
func (uss *UsageStatsService) registerExternalMetrics(metrics map[string]interface{}) {
for name, fn := range uss.externalMetrics {
result, err := fn()
if err != nil {
metricsLogger.Error("Failed to fetch external metric", "name", name, "error", err)
continue
}
metrics[name] = result
}
}
func (uss *UsageStatsService) RegisterMetric(name string, fn MetricFunc) {
uss.externalMetrics[name] = fn
}
func (uss *UsageStatsService) sendUsageStats() error { func (uss *UsageStatsService) sendUsageStats() error {
if !setting.ReportingEnabled { if !setting.ReportingEnabled {
return nil return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment