From c93596467c81cbbbb59385311e6613d98356fd0a Mon Sep 17 00:00:00 2001 From: Anton Kolesnikov Date: Thu, 22 Jul 2021 17:57:05 +0200 Subject: [PATCH] Refactor analytics package to decouple it from controller --- pkg/analytics/analytics.go | 19 ++++++++++++------- pkg/analytics/analytics_test.go | 12 ++++++++---- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pkg/analytics/analytics.go b/pkg/analytics/analytics.go index 7679452af9..39632cc3a8 100644 --- a/pkg/analytics/analytics.go +++ b/pkg/analytics/analytics.go @@ -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 ( @@ -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, @@ -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 @@ -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(), @@ -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) diff --git a/pkg/analytics/analytics_test.go b/pkg/analytics/analytics_test.go index aa3ace2ca9..44bcfd6f67 100644 --- a/pkg/analytics/analytics_test.go +++ b/pkg/analytics/analytics_test.go @@ -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 @@ -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()