Skip to content

Commit

Permalink
Expose usage stats under /status/usage-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
electron0zero committed Nov 4, 2022
1 parent a91d441 commit 4861c7d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
27 changes: 27 additions & 0 deletions cmd/tempo/app/modules.go
Expand Up @@ -2,8 +2,10 @@ package app

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

"github.com/go-kit/log/level"
"github.com/grafana/dskit/kv/codec"
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.usageReport, 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(usageReport *usagestats.Reporter, 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
report, err := usageReport.ReportJSON(time.Now())
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, report)
}
}
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
9 changes: 8 additions & 1 deletion pkg/usagestats/stats.go
Expand Up @@ -46,6 +46,7 @@ type Report struct {

// sendReport sends the report to the stats server
func sendReport(ctx context.Context, seed *ClusterSeed, interval time.Time) error {
// make buildReport func on ??
report := buildReport(seed, interval)
out, err := jsoniter.MarshalIndent(report, "", " ")
if err != nil {
Expand Down Expand Up @@ -167,7 +168,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 Expand Up @@ -400,3 +401,9 @@ func (w *WordCounter) String() string {
func (w *WordCounter) Value() int64 {
return w.count.Load()
}

func (rep *Reporter) ReportJSON(interval time.Time) (string, error) {
// FIXME: panic on boot here, please fix
report := buildReport(rep.cluster, interval)
return jsoniter.MarshalToString(report)
}

0 comments on commit 4861c7d

Please sign in to comment.