Skip to content

Commit

Permalink
Use the specific config structure for each command #192
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianKurylak committed May 22, 2021
1 parent 99d76c2 commit 9ad208d
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 102 deletions.
14 changes: 7 additions & 7 deletions pkg/agent/cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
)

type Agent struct {
cfg *config.Config
cfg *config.Agent
cs *csock.CSock
activeProfiles map[int]*agent.ProfileSession
id id.ID
u upstream.Upstream
}

func New(cfg *config.Config) *Agent {
func New(cfg *config.Agent) *Agent {
// TODO: handle this error properly
r, _ := remote.New(remote.RemoteConfig{
UpstreamThreads: cfg.Agent.UpstreamThreads,
UpstreamAddress: cfg.Agent.ServerAddress,
UpstreamRequestTimeout: cfg.Agent.UpstreamRequestTimeout,
UpstreamThreads: cfg.UpstreamThreads,
UpstreamAddress: cfg.ServerAddress,
UpstreamRequestTimeout: cfg.UpstreamRequestTimeout,
})
r.Logger = logrus.StandardLogger()
return &Agent{
Expand All @@ -38,15 +38,15 @@ func New(cfg *config.Config) *Agent {
}

func (a *Agent) Start() error {
sockPath := a.cfg.Agent.UNIXSocketPath
sockPath := a.cfg.UNIXSocketPath
cs, err := csock.NewUnixCSock(sockPath, a.controlSocketHandler)
if err != nil {
return err
}
a.cs = cs
defer os.Remove(sockPath)

go agent.SelfProfile(a.cfg, a.u, "pyroscope.agent.cpu{}", logrus.StandardLogger())
go agent.SelfProfile(a.u, "pyroscope.agent.cpu{}", logrus.StandardLogger())
cs.Start()
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/selfprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (

"github.com/pyroscope-io/pyroscope/pkg/agent/spy"
"github.com/pyroscope-io/pyroscope/pkg/agent/upstream"
"github.com/pyroscope-io/pyroscope/pkg/config"
"github.com/pyroscope-io/pyroscope/pkg/util/atexit"
)

func SelfProfile(_ *config.Config, u upstream.Upstream, appName string, logger Logger) error {
func SelfProfile(u upstream.Upstream, appName string, logger Logger) error {
// TODO: sample rate and upload rate should come from config
c := SessionConfig{
Upstream: u,
Expand Down
5 changes: 1 addition & 4 deletions pkg/agent/upstream/direct/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@ import (
"github.com/sirupsen/logrus"

"github.com/pyroscope-io/pyroscope/pkg/agent/upstream"
"github.com/pyroscope-io/pyroscope/pkg/config"
"github.com/pyroscope-io/pyroscope/pkg/storage"
"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
)

const upstreamThreads = 1

type Direct struct {
cfg *config.Config
s *storage.Storage
todo chan *upstream.UploadJob
done chan struct{}
}

func New(cfg *config.Config, s *storage.Storage) *Direct {
func New(s *storage.Storage) *Direct {
d := &Direct{
cfg: cfg,
s: s,
todo: make(chan *upstream.UploadJob, 100),
done: make(chan struct{}, upstreamThreads),
Expand Down
9 changes: 6 additions & 3 deletions pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*
Package analytics deals with collecting pyroscope server usage data.
By default pyroscope server sends anonymized usage data to Pyroscope team. This helps us understand how people use Pyroscope and prioritize features accordingly. We take privacy of our users very seriously and only collect high-level stats such as number of apps added, types of spies used, etc.
By default pyroscope server sends anonymized usage data to Pyroscope team.
This helps us understand how people use Pyroscope and prioritize features accordingly.
We take privacy of our users very seriously and only collect high-level stats such as
number of apps added, types of spies used, etc.
You can disable this with a flag or an environment variable
Expand Down Expand Up @@ -34,7 +37,7 @@ var (
uploadFrequency = 24 * time.Hour
)

func NewService(cfg *config.Config, s *storage.Storage, c *server.Controller) *Service {
func NewService(cfg *config.Server, s *storage.Storage, c *server.Controller) *Service {
return &Service{
cfg: cfg,
s: s,
Expand All @@ -50,7 +53,7 @@ func NewService(cfg *config.Config, s *storage.Storage, c *server.Controller) *S
}

type Service struct {
cfg *config.Config
cfg *config.Server
s *storage.Storage
c *server.Controller
httpClient *http.Client
Expand Down
6 changes: 3 additions & 3 deletions pkg/analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ var _ = Describe("analytics", func() {
}
go mockServer.ListenAndServe()

s, err := storage.New(*cfg)
s, err := storage.New(&(*cfg).Server)
Expect(err).ToNot(HaveOccurred())
c := server.New(*cfg, s)
analytics := NewService(*cfg, s, c)
c := server.New(&(*cfg).Server, s)
analytics := NewService(&(*cfg).Server, s, c)

startTime := time.Now()
go analytics.Start()
Expand Down
38 changes: 20 additions & 18 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,18 @@ func generateRootCmd(cfg *config.Config) *ffcli.Command {
if l, err := logrus.ParseLevel(cfg.Server.LogLevel); err == nil {
logrus.SetLevel(l)
}
startServer(cfg)

s, err := storage.New(&cfg.Server)
atexit.Register(func() { s.Close() })
if err != nil {
panic(err)
}

startServer(&cfg.Server, s)
return nil
}
convertCmd.Exec = func(_ context.Context, args []string) error {
return convert.Cli(cfg, args)
return convert.Cli(&cfg.Convert, args)
}
execCmd.Exec = func(_ context.Context, args []string) error {
if cfg.Exec.NoLogging {
Expand All @@ -319,7 +326,7 @@ func generateRootCmd(cfg *config.Config) *ffcli.Command {
return nil
}

return exec.Cli(cfg, args)
return exec.Cli(&cfg.Exec, args)
}

connectCmd.Exec = func(_ context.Context, args []string) error {
Expand All @@ -334,14 +341,14 @@ func generateRootCmd(cfg *config.Config) *ffcli.Command {
return nil
}

return exec.Cli(cfg, args)
return exec.Cli(&cfg.Exec, args)
}

dbmanagerCmd.Exec = func(_ context.Context, args []string) error {
if l, err := logrus.ParseLevel(cfg.DbManager.LogLevel); err == nil {
logrus.SetLevel(l)
}
return dbmanager.Cli(cfg, args)
return dbmanager.Cli(&cfg.DbManager, &cfg.Server, args)
}
rootCmd.Exec = func(_ context.Context, args []string) error {
if cfg.Version || len(args) > 0 && args[0] == "version" {
Expand All @@ -362,20 +369,15 @@ func Start(cfg *config.Config) error {
return generateRootCmd(cfg).ParseAndRun(context.Background(), os.Args[1:])
}

func startServer(cfg *config.Config) {
s, err := storage.New(cfg)
atexit.Register(func() { s.Close() })
if err != nil {
panic(err)
}
u := direct.New(cfg, s)
go agent.SelfProfile(cfg, u, "pyroscope.server", logrus.StandardLogger())
func startServer(cfg *config.Server, storage *storage.Storage) {
u := direct.New(storage)
go agent.SelfProfile(u, "pyroscope.server", logrus.StandardLogger())
go printRAMUsage()
go printDiskUsage(cfg)
c := server.New(cfg, s)
c := server.New(cfg, storage)
atexit.Register(func() { c.Stop() })
if !cfg.Server.AnalyticsOptOut {
analyticsService := analytics.NewService(cfg, s, c)
if !cfg.AnalyticsOptOut {
analyticsService := analytics.NewService(cfg, storage, c)
go analyticsService.Start()
atexit.Register(func() { analyticsService.Stop() })
}
Expand All @@ -395,12 +397,12 @@ func printRAMUsage() {
}
}

func printDiskUsage(cfg *config.Config) {
func printDiskUsage(cfg *config.Server) {
t := time.NewTicker(30 * time.Second)
for {
<-t.C
if logrus.IsLevelEnabled(logrus.DebugLevel) {
debug.PrintDiskUsage(cfg.Server.StoragePath)
debug.PrintDiskUsage(cfg.StoragePath)
}
}
}
6 changes: 3 additions & 3 deletions pkg/convert/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/sirupsen/logrus"
)

func Cli(cfg *config.Config, args []string) error {
func Cli(cfg *config.Convert, args []string) error {
logrus.SetOutput(os.Stderr)
var input io.Reader
if len(args) == 0 {
Expand All @@ -21,7 +21,7 @@ func Cli(cfg *config.Config, args []string) error {
}

parser := ParseGroups
switch cfg.Convert.Format {
switch cfg.Format {
case "tree":
t := tree.New()
parser(input, func(name []byte, val int) {
Expand All @@ -37,7 +37,7 @@ func Cli(cfg *config.Config, args []string) error {

t.Serialize(os.Stdout)
default:
log.Fatal("unknown format: ", cfg.Convert.Format)
log.Fatal("unknown format: ", cfg.Format)
}
return nil
}
28 changes: 14 additions & 14 deletions pkg/dbmanager/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ import (
"github.com/cheggaaa/pb/v3"
)

func Cli(cfg *config.Config, args []string) error {
func Cli(db_cfg *config.DbManager, srv_cfg *config.Server, args []string) error {
if len(args) == 0 {
return fmt.Errorf("please provide a command")
}

switch args[0] {
case "copy":
copyData(cfg)
// TODO: this is meh, I think config.Config should be separate from storage config
srv_cfg.StoragePath = db_cfg.StoragePath
srv_cfg.LogLevel = "error"
copyData(db_cfg, srv_cfg)
default:
return fmt.Errorf("unknown command %q", args[0])
}
Expand All @@ -33,14 +36,11 @@ func Cli(cfg *config.Config, args []string) error {
const resolution = 10 * time.Second

// src start time, src end time, dst start time
func copyData(cfg *config.Config) error {
// TODO: this is meh, I think config.Config should be separate from storage config
cfg.Server.StoragePath = cfg.DbManager.StoragePath
cfg.Server.LogLevel = "error"
appName := cfg.DbManager.ApplicationName
srcSt := cfg.DbManager.SrcStartTime.Truncate(resolution)
dstSt := cfg.DbManager.DstStartTime.Truncate(resolution)
dstEt := cfg.DbManager.DstEndTime.Truncate(resolution)
func copyData(db_cfg *config.DbManager, srv_cfg *config.Server) error {
appName := db_cfg.ApplicationName
srcSt := db_cfg.SrcStartTime.Truncate(resolution)
dstSt := db_cfg.DstStartTime.Truncate(resolution)
dstEt := db_cfg.DstEndTime.Truncate(resolution)
srcEt := srcSt.Add(dstEt.Sub(dstSt))

fmt.Printf("copying %s from %s-%s to %s-%s\n",
Expand All @@ -60,14 +60,14 @@ func copyData(cfg *config.Config) error {
return fmt.Errorf("src start time (%q) has to be before src end time (%q)", srcSt, srcEt)
}

s, err := storage.New(cfg)
s, err := storage.New(srv_cfg)
if err != nil {
return err
}

if cfg.DbManager.EnableProfiling {
u := direct.New(cfg, s)
go agent.SelfProfile(cfg, u, "pyroscope.dbmanager.cpu{}", logrus.StandardLogger())
if db_cfg.EnableProfiling {
u := direct.New(s)
go agent.SelfProfile(u, "pyroscope.dbmanager.cpu{}", logrus.StandardLogger())
}

st := srcSt
Expand Down
Loading

0 comments on commit 9ad208d

Please sign in to comment.