Skip to content

Commit c9423b8

Browse files
committed
improves analytics
1 parent a8f0f7c commit c9423b8

File tree

9 files changed

+76
-6
lines changed

9 files changed

+76
-6
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59
7+
github.com/clarkduvall/hyperloglog v0.0.0-20171127014514-a0107a5d8004 // indirect
78
github.com/cosmtrek/air v1.21.2
89
github.com/creack/pty v1.1.11 // indirect
910
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
1717
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
1818
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
1919
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
20+
github.com/clarkduvall/hyperloglog v0.0.0-20171127014514-a0107a5d8004 h1:mK6JroY6bLiPS3s6QCYOSjRyErFc2iHNkhhmRfF0nHo=
21+
github.com/clarkduvall/hyperloglog v0.0.0-20171127014514-a0107a5d8004/go.mod h1:drodPoQNro6QBO6TJ/MpMZbz8Bn2eSDtRN6jpG4VGw8=
2022
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
2123
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
2224
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=

pkg/analytics/analytics.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/google/uuid"
1212
"github.com/pyroscope-io/pyroscope/pkg/build"
1313
"github.com/pyroscope-io/pyroscope/pkg/config"
14+
"github.com/pyroscope-io/pyroscope/pkg/server"
1415
"github.com/pyroscope-io/pyroscope/pkg/storage"
1516
"github.com/sirupsen/logrus"
1617
)
@@ -19,10 +20,11 @@ const url = "https://analytics.pyroscope.io/api/events"
1920
const gracePeriod = 5 * time.Minute
2021
const uploadFrequency = 24 * time.Hour
2122

22-
func NewService(cfg *config.Config, s *storage.Storage) *Service {
23+
func NewService(cfg *config.Config, s *storage.Storage, c *server.Controller) *Service {
2324
return &Service{
2425
cfg: cfg,
2526
s: s,
27+
c: c,
2628
httpClient: &http.Client{
2729
Transport: &http.Transport{
2830
MaxConnsPerHost: 1,
@@ -36,6 +38,7 @@ func NewService(cfg *config.Config, s *storage.Storage) *Service {
3638
type Service struct {
3739
cfg *config.Config
3840
s *storage.Storage
41+
c *server.Controller
3942
httpClient *http.Client
4043
uploads int
4144
stopCh chan struct{}
@@ -58,6 +61,13 @@ type metrics struct {
5861
BadgerDicts int `json:"badger_dicts"`
5962
BadgerDimensions int `json:"badger_dimensions"`
6063
BadgerSegments int `json:"badger_segments"`
64+
ControllerIndex int `json:"controller_index"`
65+
ControllerIngest int `json:"controller_ingest"`
66+
ControllerRender int `json:"controller_render"`
67+
SpyRbspy int `json:"spy_rbspy"`
68+
SpyPyspy int `json:"spy_pyspy"`
69+
SpyGospy int `json:"spy_gospy"`
70+
AppsCount int `json:"apps_count"`
6171
}
6272

6373
func (s *Service) Start() {
@@ -89,6 +99,8 @@ func (s *Service) sendReport() {
8999
runtime.ReadMemStats(&ms)
90100
du := s.s.DiskUsage()
91101

102+
controllerStats := s.c.Stats()
103+
92104
m := metrics{
93105
InstallID: s.s.InstallID(),
94106
RunID: uuid.New().String(),
@@ -106,6 +118,13 @@ func (s *Service) sendReport() {
106118
BadgerDicts: int(du["dicts"]),
107119
BadgerDimensions: int(du["dimensions"]),
108120
BadgerSegments: int(du["segments"]),
121+
ControllerIndex: controllerStats["index"],
122+
ControllerIngest: controllerStats["ingest"],
123+
ControllerRender: controllerStats["render"],
124+
SpyRbspy: controllerStats["ingest:rbspy"],
125+
SpyPyspy: controllerStats["ingest:pyspy"],
126+
SpyGospy: controllerStats["ingest:gospy"],
127+
AppsCount: s.c.AppsCount(),
109128
}
110129

111130
buf, err := json.Marshal(m)

pkg/cli/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ func startServer(cfg *config.Config) {
267267
go agent.SelfProfile(cfg, u, "pyroscope.server.cpu{}")
268268
go printRAMUsage()
269269
go printDiskUsage(cfg)
270+
c := server.New(cfg, s)
270271
if !cfg.Server.AnalyticsOptOut {
271-
analyticsService := analytics.NewService(cfg, s)
272+
analyticsService := analytics.NewService(cfg, s, c)
272273
go analyticsService.Start()
273274
atexit.Register(func() { analyticsService.Stop() })
274275
}
275-
c := server.New(cfg, s)
276276
// if you ever change this line, make sure to update this homebrew test:
277277
// https://github.com/pyroscope-io/homebrew-brew/blob/main/Formula/pyroscope.rb#L94
278278
log.Info("starting HTTP server")

pkg/server/controller.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package server
33
import (
44
golog "log"
55
"net/http"
6+
"sync"
67
"time"
78

89
_ "net/http/pprof"
910

11+
"github.com/clarkduvall/hyperloglog"
1012
"github.com/markbates/pkger"
1113
"github.com/pyroscope-io/pyroscope/pkg/build"
1214
"github.com/pyroscope-io/pyroscope/pkg/config"
@@ -22,12 +24,20 @@ func init() {
2224
type Controller struct {
2325
cfg *config.Config
2426
s *storage.Storage
27+
28+
statsMutex sync.Mutex
29+
stats map[string]int
30+
31+
appStats *hyperloglog.HyperLogLogPlus
2532
}
2633

2734
func New(cfg *config.Config, s *storage.Storage) *Controller {
35+
appStats, _ := hyperloglog.NewPlus(uint8(18))
2836
return &Controller{
29-
cfg: cfg,
30-
s: s,
37+
cfg: cfg,
38+
s: s,
39+
stats: make(map[string]int),
40+
appStats: appStats,
3141
}
3242
}
3343

@@ -44,7 +54,10 @@ func (ctrl *Controller) Start() {
4454
} else {
4555
fs = http.FileServer(http.Dir("./webapp/public"))
4656
}
47-
mux.HandleFunc("/", fs.ServeHTTP)
57+
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
58+
ctrl.statsInc("index")
59+
fs.ServeHTTP(rw, r)
60+
})
4861

4962
logger := log.New()
5063
w := logger.Writer()

pkg/server/ingest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,9 @@ func (ctrl *Controller) ingestHandler(w http.ResponseWriter, r *http.Request) {
9696
if err != nil {
9797
log.Fatal(err)
9898
}
99+
ctrl.statsInc("ingest")
100+
ctrl.statsInc("ingest:" + ip.spyName)
101+
k := *ip.storageKey
102+
ctrl.appStats.Add(hashString(k.AppName()))
99103
w.WriteHeader(200)
100104
}

pkg/server/render.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (ctrl *Controller) renderHandler(w http.ResponseWriter, r *http.Request) {
2727
}
2828

2929
resultTree, tl, spyName, sampleRate, err := ctrl.s.Get(startTime, endTime, storageKey)
30+
ctrl.statsInc("render")
3031
if err != nil {
3132
panic(err) // TODO: handle
3233
}

pkg/server/stats.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package server
2+
3+
import "github.com/spaolacci/murmur3"
4+
5+
const seed = 6231912
6+
7+
type hashString string
8+
9+
func (hs hashString) Sum64() uint64 {
10+
return murmur3.Sum64WithSeed([]byte(hs), seed)
11+
}
12+
13+
func (ctrl *Controller) statsInc(name string) {
14+
ctrl.statsMutex.Lock()
15+
defer ctrl.statsMutex.Unlock()
16+
17+
ctrl.stats[name]++
18+
}
19+
20+
func (ctrl *Controller) Stats() map[string]int {
21+
return ctrl.stats
22+
}
23+
24+
func (ctrl *Controller) AppsCount() int {
25+
return int(ctrl.appStats.Count())
26+
}

pkg/storage/key.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,7 @@ func (k *Key) Hashed() []byte {
130130
binary.LittleEndian.PutUint64(b[8:16], u2)
131131
return b
132132
}
133+
134+
func (k *Key) AppName() string {
135+
return k.labels["__name__"]
136+
}

0 commit comments

Comments
 (0)