Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add /status/usage-stats endpoint to show usage stats data #1782

Merged
merged 6 commits into from Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,4 +1,5 @@
## main / unreleased
* [FEATURE] Add `/status/usage-stats` endpoint to show usage stats data [#1782](https://github.com/grafana/tempo/pull/1782) (@electron0zero)
* [FEATURE] Add generic forwarder and implement otlpgrpc forwarder [#1775](https://github.com/grafana/tempo/pull/1775) (@Blinkuu)
New config options and example configuration:
```
Expand Down
27 changes: 27 additions & 0 deletions cmd/tempo/app/modules.go
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"io"
"net/http"
"path"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/grafana/dskit/modules"
"github.com/grafana/dskit/ring"
"github.com/grafana/dskit/services"
jsoniter "github.com/json-iterator/go"
"github.com/prometheus/client_golang/prometheus"
"github.com/thanos-io/thanos/pkg/discovery/dns"
"github.com/weaveworks/common/middleware"
Expand Down Expand Up @@ -257,6 +259,9 @@ func (t *App) initQueryFrontend() (services.Service, error) {
// http query echo endpoint
t.Server.HTTP.Handle(addHTTPAPIPrefix(&t.cfg, api.PathEcho), echoHandler())

// http endpoint to see usage stats data
t.Server.HTTP.Handle(addHTTPAPIPrefix(&t.cfg, api.PathUsageStats), usageStatsHandler(t.cfg.UsageReport))

// todo: queryFrontend should implement service.Service and take the cortex frontend a submodule
return t.frontend, nil
}
Expand Down Expand Up @@ -447,3 +452,25 @@ func echoHandler() http.HandlerFunc {
http.Error(w, "echo", http.StatusOK)
}
}

func usageStatsHandler(urCfg usagestats.Config) http.HandlerFunc {
if !urCfg.Enabled {
return func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "usage-stats is not enabled", http.StatusOK)
}
}

// usage stats is Enabled, build and return usage stats json
reportStr, err := jsoniter.MarshalToString(usagestats.BuildStats())
if err != nil {
return func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "error building usage report", http.StatusInternalServerError)
}
}

return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, reportStr)
}
}
6 changes: 6 additions & 0 deletions docs/tempo/website/api_docs/_index.md
Expand Up @@ -379,3 +379,9 @@ Displays the override configuration.

Query parameter:
- `mode = (diff)`: Show the difference between defaults and overrides.

```
GET /status/usage-stats
```

Displays anonymous usage stats data that is reported back to Grafana Labs.
1 change: 1 addition & 0 deletions pkg/api/http.go
Expand Up @@ -58,6 +58,7 @@ const (
PathSearchTags = "/api/search/tags"
PathSearchTagValues = "/api/search/tag/{tagName}/values"
PathEcho = "/api/echo"
PathUsageStats = "/status/usage-stats"

QueryModeKey = "mode"
QueryModeIngesters = "ingesters"
Expand Down
4 changes: 2 additions & 2 deletions pkg/usagestats/reporter.go
Expand Up @@ -34,7 +34,7 @@ var (
reportInterval = 4 * time.Hour

stabilityCheckInterval = 5 * time.Second
stabilityMinimunRequired = 6
stabilityMinimumRequired = 6
)

type Reporter struct {
Expand Down Expand Up @@ -149,7 +149,7 @@ func ensureStableKey(ctx context.Context, kvClient kv.Client, logger log.Logger)
continue
}
stableCount++
if stableCount > stabilityMinimunRequired {
if stableCount > stabilityMinimumRequired {
return seed
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/usagestats/reporter_test.go
Expand Up @@ -156,7 +156,7 @@ func Test_ReportLoop(t *testing.T) {
r.initLeader(ctx)

go func() {
<-time.After(6*time.Second + (stabilityCheckInterval * time.Duration(stabilityMinimunRequired+1)))
<-time.After(6*time.Second + (stabilityCheckInterval * time.Duration(stabilityMinimumRequired+1)))
cancel()
}()
require.Equal(t, nil, r.running(ctx))
Expand Down
18 changes: 13 additions & 5 deletions pkg/usagestats/stats.go
Expand Up @@ -71,8 +71,19 @@ func sendReport(ctx context.Context, seed *ClusterSeed, interval time.Time) erro
return nil
}

// buildReport builds the report to be sent to the stats server
// buildReport builds the report to be sent to the stats server,
// this report includes the cluster seed data.
func buildReport(seed *ClusterSeed, interval time.Time) Report {
report := BuildStats()
report.ClusterID = seed.UID
report.CreatedAt = seed.CreatedAt
report.Interval = interval

return report
}

// BuildStats builds the report without cluster seed data
func BuildStats() Report {
var (
targetName string
editionName string
Expand All @@ -89,10 +100,7 @@ func buildReport(seed *ClusterSeed, interval time.Time) Report {
}

return Report{
ClusterID: seed.UID,
PrometheusVersion: build.GetVersion(),
CreatedAt: seed.CreatedAt,
Interval: interval,
IntervalPeriod: reportInterval.Seconds(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Expand Down Expand Up @@ -167,7 +175,7 @@ func NewFloat(name string) *expvar.Float {
}

// NewInt returns a new Int stats object.
// If an Int stats object object with the same name already exists it is returned.
// If an Int stats object with the same name already exists it is returned.
func NewInt(name string) *expvar.Int {
existing := expvar.Get(statsPrefix + name)
if existing != nil {
Expand Down
51 changes: 51 additions & 0 deletions pkg/usagestats/stats_test.go
Expand Up @@ -65,6 +65,57 @@ func Test_BuildReport(t *testing.T) {
t.Log(string(out))
}

func Test_BuildStats(t *testing.T) {
Edition("non-OSS")
Edition("OSS")
Target("distributor")
Target("compactor")
NewString("compression").Set("snappy")
NewString("compression").Set("lz4")
NewInt("compression_ratio").Set(50)
NewInt("compression_ratio").Set(100)
NewFloat("size_mb").Set(100.1)
NewFloat("size_mb").Set(200.1)
NewCounter("lines_written").Inc(200)
s := NewStatistics("test_build_stats")
s.Record(25)
s = NewStatistics("test_build_stats")
s.Record(300)
s.Record(5)
w := NewWordCounter("active_tenants_build_stats")
w.Add("buz")
w = NewWordCounter("active_tenants_build_stats")
w.Add("foo")
w.Add("bar")
w.Add("foo")

r := BuildStats()
require.Equal(t, r.Arch, runtime.GOARCH)
require.Equal(t, r.Os, runtime.GOOS)
require.Equal(t, r.PrometheusVersion, build.GetVersion())
require.Equal(t, r.Edition, "OSS")
require.Equal(t, r.Target, "compactor")
require.Equal(t, r.Metrics["num_cpu"], runtime.NumCPU())
// Don't check num_goroutine because it could have changed since the report was created.
require.Equal(t, r.Metrics["compression"], "lz4")
require.Equal(t, r.Metrics["compression_ratio"], int64(100))
require.Equal(t, r.Metrics["size_mb"], 200.1)
require.Equal(t, r.Metrics["lines_written"].(map[string]interface{})["total"], int64(200))
require.Equal(t, r.Metrics["test_build_stats"].(map[string]interface{})["min"], float64(5))
require.Equal(t, r.Metrics["test_build_stats"].(map[string]interface{})["max"], float64(300))
require.Equal(t, r.Metrics["test_build_stats"].(map[string]interface{})["count"], int64(3))
require.Equal(t, r.Metrics["test_build_stats"].(map[string]interface{})["avg"], float64(25+300+5)/3)
require.Equal(t, r.Metrics["active_tenants_build_stats"], int64(3))

// check if ClusterID and Seed related attrs are not set.
require.Equal(t, r.ClusterID, "")
require.Equal(t, r.CreatedAt, time.Time{})
require.Equal(t, r.Interval, time.Time{})

out, _ := jsoniter.MarshalIndent(r, "", " ")
t.Log(string(out))
}

func TestCounter(t *testing.T) {
c := NewCounter("test_counter")
c.Inc(100)
Expand Down