-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Periodically send anonymous usage stats report (#2662)
* Periodically send anonymous usage stats report Signed-off-by: Marco Pracucci <marco@pracucci.com> * Addressed review comments Signed-off-by: Marco Pracucci <marco@pracucci.com> * Addressed review comments Signed-off-by: Marco Pracucci <marco@pracucci.com>
- Loading branch information
Showing
8 changed files
with
655 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package usagestats | ||
|
||
import ( | ||
"expvar" | ||
"runtime" | ||
"time" | ||
|
||
prom "github.com/prometheus/prometheus/web/api/v1" | ||
|
||
"github.com/grafana/mimir/pkg/util/version" | ||
) | ||
|
||
// Report is the JSON object sent to the stats server | ||
type Report struct { | ||
// ClusterID is the unique Mimir cluster ID. | ||
ClusterID string `json:"clusterID"` | ||
|
||
// CreatedAt is when the cluster was created. | ||
CreatedAt time.Time `json:"createdAt"` | ||
|
||
// Interval is when the report was created (value is aligned across all replicas of the same Mimir cluster). | ||
Interval time.Time `json:"interval"` | ||
|
||
// IntervalPeriod is how frequently the report is sent, in seconds. | ||
IntervalPeriod float64 `json:"intervalPeriod"` | ||
|
||
// Target used to run Mimir. | ||
Target string `json:"target"` | ||
|
||
// Version holds information about the Mimir version. | ||
Version prom.PrometheusVersion `json:"version"` | ||
|
||
// Os is the operating system where Mimir is running. | ||
Os string `json:"os"` | ||
|
||
// Arch is the architecture where Mimir is running. | ||
Arch string `json:"arch"` | ||
|
||
// Edition is the Mimir edition ("oss" or "enterprise"). | ||
Edition string `json:"edition"` | ||
|
||
// Metrics holds custom metrics tracked by Mimir. Can contain nested objects. | ||
Metrics map[string]interface{} `json:"metrics"` | ||
} | ||
|
||
// buildReport builds the report to be sent to the stats server. | ||
func buildReport(seed ClusterSeed, reportAt time.Time, reportInterval time.Duration) Report { | ||
var ( | ||
targetName string | ||
editionName string | ||
) | ||
if target := expvar.Get(statsPrefix + targetKey); target != nil { | ||
if target, ok := target.(*expvar.String); ok { | ||
targetName = target.Value() | ||
} | ||
} | ||
if edition := expvar.Get(statsPrefix + editionKey); edition != nil { | ||
if edition, ok := edition.(*expvar.String); ok { | ||
editionName = edition.Value() | ||
} | ||
} | ||
|
||
return Report{ | ||
ClusterID: seed.UID, | ||
CreatedAt: seed.CreatedAt, | ||
Version: buildVersion(), | ||
Interval: reportAt, | ||
IntervalPeriod: reportInterval.Seconds(), | ||
Os: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
Target: targetName, | ||
Edition: editionName, | ||
Metrics: buildMetrics(), | ||
} | ||
} | ||
|
||
// buildMetrics builds the metrics part of the report to be sent to the stats server. | ||
func buildMetrics() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"memstats": buildMemstats(), | ||
"num_cpu": runtime.NumCPU(), | ||
"num_goroutine": runtime.NumGoroutine(), | ||
} | ||
} | ||
|
||
func buildMemstats() interface{} { | ||
stats := new(runtime.MemStats) | ||
runtime.ReadMemStats(stats) | ||
|
||
return map[string]interface{}{ | ||
"alloc": stats.Alloc, | ||
"total_alloc": stats.TotalAlloc, | ||
"sys": stats.Sys, | ||
"heap_alloc": stats.HeapAlloc, | ||
"heap_inuse": stats.HeapInuse, | ||
"stack_inuse": stats.StackInuse, | ||
"pause_total_ns": stats.PauseTotalNs, | ||
"num_gc": stats.NumGC, | ||
"gc_cpu_fraction": stats.GCCPUFraction, | ||
} | ||
} | ||
|
||
func buildVersion() prom.PrometheusVersion { | ||
return prom.PrometheusVersion{ | ||
Version: version.Version, | ||
Revision: version.Revision, | ||
Branch: version.Branch, | ||
GoVersion: version.GoVersion, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package usagestats | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/grafana/mimir/pkg/util/version" | ||
) | ||
|
||
func TestBuildReport(t *testing.T) { | ||
var ( | ||
clusterCreatedAt = time.Now().Add(-time.Hour) | ||
seed = ClusterSeed{UID: "test", CreatedAt: clusterCreatedAt} | ||
reportAt = time.Now() | ||
reportInterval = time.Hour | ||
) | ||
|
||
SetTarget("all") | ||
version.Version = "dev-version" | ||
version.Branch = "dev-branch" | ||
version.Revision = "dev-revision" | ||
|
||
report := buildReport(seed, reportAt, reportInterval) | ||
assert.Equal(t, "test", report.ClusterID) | ||
assert.Equal(t, clusterCreatedAt, report.CreatedAt) | ||
assert.Equal(t, reportAt, report.Interval) | ||
assert.Equal(t, reportInterval.Seconds(), report.IntervalPeriod) | ||
assert.Equal(t, "all", report.Target) | ||
assert.Equal(t, runtime.GOOS, report.Os) | ||
assert.Equal(t, runtime.GOARCH, report.Arch) | ||
assert.Equal(t, "oss", report.Edition) | ||
assert.NotNil(t, report.Metrics["memstats"]) | ||
assert.Equal(t, "dev-version", report.Version.Version) | ||
assert.Equal(t, "dev-branch", report.Version.Branch) | ||
assert.Equal(t, "dev-revision", report.Version.Revision) | ||
assert.Equal(t, runtime.Version(), report.Version.GoVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.