Commit 53153c82 by Dan Cech Committed by GitHub

add method to get usage report (#27555)

parent cade6dd0
...@@ -50,7 +50,7 @@ func (uss *UsageStatsService) Run(ctx context.Context) error { ...@@ -50,7 +50,7 @@ func (uss *UsageStatsService) Run(ctx context.Context) error {
for { for {
select { select {
case <-onceEveryDayTick.C: case <-onceEveryDayTick.C:
uss.sendUsageStats(uss.oauthProviders) uss.sendUsageStats()
case <-everyMinuteTicker.C: case <-everyMinuteTicker.C:
uss.updateTotalStats() uss.updateTotalStats()
case <-ctx.Done(): case <-ctx.Done():
......
...@@ -17,30 +17,35 @@ import ( ...@@ -17,30 +17,35 @@ import (
var usageStatsURL = "https://stats.grafana.org/grafana-usage-report" var usageStatsURL = "https://stats.grafana.org/grafana-usage-report"
func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { type UsageReport struct {
if !setting.ReportingEnabled { Version string `json:"version"`
return Metrics map[string]interface{} `json:"metrics"`
} Os string `json:"os"`
Arch string `json:"arch"`
metricsLogger.Debug(fmt.Sprintf("Sending anonymous usage stats to %s", usageStatsURL)) Edition string `json:"edition"`
HasValidLicense bool `json:"hasValidLicense"`
Packaging string `json:"packaging"`
}
func (uss *UsageStatsService) GetUsageReport() (UsageReport, error) {
version := strings.Replace(setting.BuildVersion, ".", "_", -1) version := strings.Replace(setting.BuildVersion, ".", "_", -1)
metrics := map[string]interface{}{} metrics := map[string]interface{}{}
report := map[string]interface{}{
"version": version, report := UsageReport{
"metrics": metrics, Version: version,
"os": runtime.GOOS, Metrics: metrics,
"arch": runtime.GOARCH, Os: runtime.GOOS,
"edition": getEdition(), Arch: runtime.GOARCH,
"hasValidLicense": uss.License.HasValidLicense(), Edition: getEdition(),
"packaging": setting.Packaging, HasValidLicense: uss.License.HasValidLicense(),
Packaging: setting.Packaging,
} }
statsQuery := models.GetSystemStatsQuery{} statsQuery := models.GetSystemStatsQuery{}
if err := uss.Bus.Dispatch(&statsQuery); err != nil { if err := uss.Bus.Dispatch(&statsQuery); err != nil {
metricsLogger.Error("Failed to get system stats", "error", err) metricsLogger.Error("Failed to get system stats", "error", err)
return return report, err
} }
metrics["stats.dashboards.count"] = statsQuery.Result.Dashboards metrics["stats.dashboards.count"] = statsQuery.Result.Dashboards
...@@ -78,7 +83,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { ...@@ -78,7 +83,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
dsStats := models.GetDataSourceStatsQuery{} dsStats := models.GetDataSourceStatsQuery{}
if err := uss.Bus.Dispatch(&dsStats); err != nil { if err := uss.Bus.Dispatch(&dsStats); err != nil {
metricsLogger.Error("Failed to get datasource stats", "error", err) metricsLogger.Error("Failed to get datasource stats", "error", err)
return return report, err
} }
// send counters for each data source // send counters for each data source
...@@ -100,7 +105,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { ...@@ -100,7 +105,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
alertingUsageStats, err := uss.AlertingUsageStats.QueryUsageStats() alertingUsageStats, err := uss.AlertingUsageStats.QueryUsageStats()
if err != nil { if err != nil {
uss.log.Error("Failed to get alerting usage stats", "error", err) uss.log.Error("Failed to get alerting usage stats", "error", err)
return return report, err
} }
var addAlertingUsageStats = func(dsType string, usageCount int) { var addAlertingUsageStats = func(dsType string, usageCount int) {
...@@ -122,7 +127,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { ...@@ -122,7 +127,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
dsAccessStats := models.GetDataSourceAccessStatsQuery{} dsAccessStats := models.GetDataSourceAccessStatsQuery{}
if err := uss.Bus.Dispatch(&dsAccessStats); err != nil { if err := uss.Bus.Dispatch(&dsAccessStats); err != nil {
metricsLogger.Error("Failed to get datasource access stats", "error", err) metricsLogger.Error("Failed to get datasource access stats", "error", err)
return return report, err
} }
// send access counters for each data source // send access counters for each data source
...@@ -152,7 +157,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { ...@@ -152,7 +157,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
anStats := models.GetAlertNotifierUsageStatsQuery{} anStats := models.GetAlertNotifierUsageStatsQuery{}
if err := uss.Bus.Dispatch(&anStats); err != nil { if err := uss.Bus.Dispatch(&anStats); err != nil {
metricsLogger.Error("Failed to get alert notification stats", "error", err) metricsLogger.Error("Failed to get alert notification stats", "error", err)
return return report, err
} }
for _, stats := range anStats.Result { for _, stats := range anStats.Result {
...@@ -166,7 +171,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { ...@@ -166,7 +171,7 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
authTypes["ldap"] = setting.LDAPEnabled authTypes["ldap"] = setting.LDAPEnabled
authTypes["auth_proxy"] = setting.AuthProxyEnabled authTypes["auth_proxy"] = setting.AuthProxyEnabled
for provider, enabled := range oauthProviders { for provider, enabled := range uss.oauthProviders {
authTypes["oauth_"+provider] = enabled authTypes["oauth_"+provider] = enabled
} }
...@@ -178,6 +183,21 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) { ...@@ -178,6 +183,21 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
metrics["stats.auth_enabled."+authType+".count"] = enabledValue metrics["stats.auth_enabled."+authType+".count"] = enabledValue
} }
return report, nil
}
func (uss *UsageStatsService) sendUsageStats() {
if !setting.ReportingEnabled {
return
}
metricsLogger.Debug(fmt.Sprintf("Sending anonymous usage stats to %s", usageStatsURL))
report, err := uss.GetUsageReport()
if err != nil {
return
}
out, _ := json.MarshalIndent(report, "", " ") out, _ := json.MarshalIndent(report, "", " ")
data := bytes.NewBuffer(out) data := bytes.NewBuffer(out)
......
...@@ -162,7 +162,7 @@ func TestMetrics(t *testing.T) { ...@@ -162,7 +162,7 @@ func TestMetrics(t *testing.T) {
})) }))
usageStatsURL = ts.URL usageStatsURL = ts.URL
oauthProviders := map[string]bool{ uss.oauthProviders = map[string]bool{
"github": true, "github": true,
"gitlab": true, "gitlab": true,
"azuread": true, "azuread": true,
...@@ -171,11 +171,11 @@ func TestMetrics(t *testing.T) { ...@@ -171,11 +171,11 @@ func TestMetrics(t *testing.T) {
"grafana_com": true, "grafana_com": true,
} }
uss.sendUsageStats(oauthProviders) uss.sendUsageStats()
Convey("Given reporting not enabled and sending usage stats", func() { Convey("Given reporting not enabled and sending usage stats", func() {
setting.ReportingEnabled = false setting.ReportingEnabled = false
uss.sendUsageStats(oauthProviders) uss.sendUsageStats()
Convey("Should not gather stats or call http endpoint", func() { Convey("Should not gather stats or call http endpoint", func() {
So(getSystemStatsQuery, ShouldBeNil) So(getSystemStatsQuery, ShouldBeNil)
...@@ -195,7 +195,7 @@ func TestMetrics(t *testing.T) { ...@@ -195,7 +195,7 @@ func TestMetrics(t *testing.T) {
setting.Packaging = "deb" setting.Packaging = "deb"
wg.Add(1) wg.Add(1)
uss.sendUsageStats(oauthProviders) uss.sendUsageStats()
Convey("Should gather stats and call http endpoint", func() { Convey("Should gather stats and call http endpoint", func() {
if waitTimeout(&wg, 2*time.Second) { if waitTimeout(&wg, 2*time.Second) {
......
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