Skip to content

Commit

Permalink
Refactor analytics package to decouple it from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnikovae committed Jul 22, 2021
1 parent fa6048b commit c935964
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
19 changes: 12 additions & 7 deletions pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"time"

"github.com/google/uuid"
"github.com/sirupsen/logrus"

"github.com/pyroscope-io/pyroscope/pkg/build"
"github.com/pyroscope-io/pyroscope/pkg/config"
"github.com/pyroscope-io/pyroscope/pkg/server"
"github.com/pyroscope-io/pyroscope/pkg/storage"
"github.com/sirupsen/logrus"
)

var (
Expand All @@ -37,11 +37,16 @@ var (
uploadFrequency = 24 * time.Hour
)

func NewService(cfg *config.Server, s *storage.Storage, c *server.Controller) *Service {
type StatsProvider interface {
Stats() map[string]int
AppsCount() int
}

func NewService(cfg *config.Server, s *storage.Storage, p StatsProvider) *Service {
return &Service{
cfg: cfg,
s: s,
c: c,
p: p,
httpClient: &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 1,
Expand All @@ -56,7 +61,7 @@ func NewService(cfg *config.Server, s *storage.Storage, c *server.Controller) *S
type Service struct {
cfg *config.Server
s *storage.Storage
c *server.Controller
p StatsProvider
httpClient *http.Client
uploads int

Expand Down Expand Up @@ -125,7 +130,7 @@ func (s *Service) sendReport() {
runtime.ReadMemStats(&ms)
du := s.s.DiskUsage()

controllerStats := s.c.Stats()
controllerStats := s.p.Stats()

m := metrics{
InstallID: s.s.InstallID(),
Expand Down Expand Up @@ -153,7 +158,7 @@ func (s *Service) sendReport() {
SpyEbpfspy: controllerStats["ingest:ebpfspy"],
SpyPhpspy: controllerStats["ingest:phpspy"],
SpyDotnetspy: controllerStats["ingest:dotnetspy"],
AppsCount: s.c.AppsCount(),
AppsCount: s.p.AppsCount(),
}

buf, err := json.Marshal(m)
Expand Down
12 changes: 8 additions & 4 deletions pkg/analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/pyroscope-io/pyroscope/pkg/config"
"github.com/pyroscope-io/pyroscope/pkg/server"
"github.com/pyroscope-io/pyroscope/pkg/storage"
"github.com/pyroscope-io/pyroscope/pkg/testing"
"github.com/sirupsen/logrus"
)

const durThreshold = 30 * time.Millisecond

type mockStatsProvider struct{}

func (mockStatsProvider) Stats() map[string]int { return map[string]int{} }

func (mockStatsProvider) AppsCount() int { return 0 }

var _ = Describe("analytics", func() {
gracePeriod = 100 * time.Millisecond
uploadFrequency = 200 * time.Millisecond
Expand Down Expand Up @@ -55,8 +60,7 @@ var _ = Describe("analytics", func() {
s, err := storage.New(&(*cfg).Server)
Expect(err).ToNot(HaveOccurred())

c, _ := server.New(&(*cfg).Server, s, logrus.New())
analytics := NewService(&(*cfg).Server, s, c)
analytics := NewService(&(*cfg).Server, s, mockStatsProvider{})

startTime := time.Now()
go analytics.Start()
Expand Down

0 comments on commit c935964

Please sign in to comment.