|
|
|
@ -29,16 +29,16 @@ var usageStatsURL = "https://stats.grafana.org/grafana-usage-report" |
|
|
|
|
|
|
|
|
|
func (uss *UsageStats) GetUsageReport(ctx context.Context) (usagestats.Report, error) { |
|
|
|
|
version := strings.ReplaceAll(uss.Cfg.BuildVersion, ".", "_") |
|
|
|
|
metrics := map[string]any{} |
|
|
|
|
metrics := sync.Map{} |
|
|
|
|
start := time.Now() |
|
|
|
|
|
|
|
|
|
edition := "oss" |
|
|
|
|
if uss.Cfg.IsEnterprise { |
|
|
|
|
edition = "enterprise" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
report := usagestats.Report{ |
|
|
|
|
Version: version, |
|
|
|
|
Metrics: metrics, |
|
|
|
|
Os: runtime.GOOS, |
|
|
|
|
Arch: runtime.GOARCH, |
|
|
|
|
Edition: edition, |
|
|
|
@ -46,20 +46,28 @@ func (uss *UsageStats) GetUsageReport(ctx context.Context) (usagestats.Report, e |
|
|
|
|
UsageStatsId: uss.GetUsageStatsId(ctx), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uss.gatherMetrics(ctx, metrics) |
|
|
|
|
uss.gatherMetrics(ctx, &metrics) |
|
|
|
|
|
|
|
|
|
// must run after registration of external metrics
|
|
|
|
|
if v, ok := metrics["stats.valid_license.count"]; ok { |
|
|
|
|
if v, ok := metrics.Load("stats.valid_license.count"); ok { |
|
|
|
|
report.HasValidLicense = v == 1 |
|
|
|
|
} else { |
|
|
|
|
metrics["stats.valid_license.count"] = 0 |
|
|
|
|
metrics.Store("stats.valid_license.count", 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uss.log.FromContext(ctx).Debug("Collected usage stats", "metricCount", len(metrics), "version", report.Version, "os", report.Os, "arch", report.Arch, "edition", report.Edition, "duration", time.Since(start)) |
|
|
|
|
report.Metrics = make(map[string]any) |
|
|
|
|
metricCount := 0 |
|
|
|
|
metrics.Range(func(key, value any) bool { |
|
|
|
|
report.Metrics[key.(string)] = value |
|
|
|
|
metricCount++ |
|
|
|
|
return true |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
uss.log.FromContext(ctx).Debug("Collected usage stats", "metricCount", metricCount, "version", report.Version, "os", report.Os, "arch", report.Arch, "edition", report.Edition, "duration", time.Since(start)) |
|
|
|
|
return report, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (uss *UsageStats) gatherMetrics(ctx context.Context, metrics map[string]any) { |
|
|
|
|
func (uss *UsageStats) gatherMetrics(ctx context.Context, metrics *sync.Map) { |
|
|
|
|
ctxTracer, span := uss.tracer.Start(ctx, "UsageStats.GatherLoop") |
|
|
|
|
defer span.End() |
|
|
|
|
totC, errC := 0, 0 |
|
|
|
@ -86,14 +94,14 @@ func (uss *UsageStats) gatherMetrics(ctx context.Context, metrics map[string]any |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for name, value := range fnMetrics { |
|
|
|
|
metrics[name] = value |
|
|
|
|
metrics.Store(name, value) |
|
|
|
|
} |
|
|
|
|
}(fn) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
metrics["stats.usagestats.debug.collect.total.count"] = totC |
|
|
|
|
metrics["stats.usagestats.debug.collect.error.count"] = errC |
|
|
|
|
metrics.Store("stats.usagestats.debug.collect.total.count", totC) |
|
|
|
|
metrics.Store("stats.usagestats.debug.collect.error.count", errC) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (uss *UsageStats) runMetricsFunc(ctx context.Context, fn usagestats.MetricsFunc) (map[string]any, error) { |
|
|
|
|