From adeefdbeaa13042a5d68b96ffe1c47b626fd6f8d Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Fri, 26 Oct 2018 11:20:15 +0200 Subject: [PATCH] fix(all): - config fixes for singleton values (most) - add monitor into internal/metrics - clean up cmd folders to remove monitor - implement versions on all apps --- auth/flags.go | 21 +++++--- auth/start.go | 8 +++ cmd/auth/flags.go | 12 +---- cmd/auth/main.go | 3 +- cmd/crm/flags.go | 12 +---- cmd/crm/main.go | 3 +- cmd/crm/monitor.go | 59 ----------------------- cmd/sam/flags.go | 12 +---- cmd/sam/main.go | 6 +-- cmd/sam/monitor.go | 59 ----------------------- crm/flags.go | 9 +++- crm/start.go | 8 +++ internal/config/database.go | 2 +- internal/config/http.go | 2 +- internal/config/jwt.go | 2 +- internal/config/monitor.go | 27 +++++++++++ internal/config/oidc.go | 2 +- {cmd/auth => internal/metrics}/monitor.go | 2 +- sam/flags.go | 5 ++ sam/start.go | 10 +++- 20 files changed, 88 insertions(+), 176 deletions(-) delete mode 100644 cmd/crm/monitor.go delete mode 100644 cmd/sam/monitor.go create mode 100644 internal/config/monitor.go rename {cmd/auth => internal/metrics}/monitor.go (98%) diff --git a/auth/flags.go b/auth/flags.go index 9635dd5b7..8df776540 100644 --- a/auth/flags.go +++ b/auth/flags.go @@ -8,10 +8,11 @@ import ( type ( appFlags struct { - http *config.HTTP - db *config.Database - //jwt *config.JWT - oidc *config.OIDC + http *config.HTTP + monitor *config.Monitor + db *config.Database + oidc *config.OIDC + //jwt *config.JWT } ) @@ -24,15 +25,18 @@ func (c *appFlags) Validate() error { if err := c.http.Validate(); err != nil { return err } + if err := c.monitor.Validate(); err != nil { + return err + } if err := c.db.Validate(); err != nil { return err } + if err := c.oidc.Validate(); err != nil { + return err + } //if err := c.jwt.Validate(); err != nil { // return err //} - if err := c.oidc.Validate(); err != nil { - return err - } return nil } @@ -45,8 +49,9 @@ func Flags(prefix ...string) { } flags = &appFlags{ new(config.HTTP).Init(prefix...), + new(config.Monitor).Init(prefix...), new(config.Database).Init(prefix...), - //new(config.JWT).Init(prefix...), new(config.OIDC).Init(prefix...), + //new(config.JWT).Init(prefix...), } } diff --git a/auth/start.go b/auth/start.go index d7480bb9c..f2326704f 100644 --- a/auth/start.go +++ b/auth/start.go @@ -16,7 +16,10 @@ import ( migrate "github.com/crusttech/crust/auth/db" "github.com/crusttech/crust/auth/rest" "github.com/crusttech/crust/auth/service" + "github.com/crusttech/crust/internal/auth" + "github.com/crusttech/crust/internal/metrics" + "github.com/crusttech/crust/internal/version" ) func Init() error { @@ -62,6 +65,7 @@ func Init() error { func Start() error { var deadline = sigctx.New() + log.Printf("Starting auth, version: %v, built on: %v", version.Version, version.BuildTime) log.Println("Starting http server on address " + flags.http.Addr) listener, err := net.Listen("tcp", flags.http.Addr) if err != nil { @@ -86,6 +90,10 @@ func Start() error { printRoutes(r, flags.http) mountSystemRoutes(r, flags.http) + if flags.monitor.Interval > 0 { + go metrics.NewMonitor(flags.monitor.Interval) + } + go http.Serve(listener, r) <-deadline.Done() diff --git a/cmd/auth/flags.go b/cmd/auth/flags.go index b705f48c2..d074ab67c 100644 --- a/cmd/auth/flags.go +++ b/cmd/auth/flags.go @@ -5,19 +5,9 @@ import ( "github.com/namsral/flag" ) -type configuration struct { - monitorInterval int -} - -func flags(prefix string, mountFlags ...func(...string)) configuration { - var config configuration - - flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)") - +func flags(prefix string, mountFlags ...func(...string)) { for _, mount := range mountFlags { mount(prefix) } - flag.Parse() - return config } diff --git a/cmd/auth/main.go b/cmd/auth/main.go index fd59a0807..9a3c0b2fe 100644 --- a/cmd/auth/main.go +++ b/cmd/auth/main.go @@ -11,12 +11,11 @@ import ( ) func main() { - config := flags("auth", service.Flags, auth.Flags, rbac.Flags) + flags("auth", service.Flags, auth.Flags, rbac.Flags) // log to stdout not stderr log.SetOutput(os.Stdout) log.SetFlags(log.LstdFlags | log.Lshortfile) - go NewMonitor(config.monitorInterval) if err := service.Init(); err != nil { log.Fatalf("Error initializing auth: %+v", err) diff --git a/cmd/crm/flags.go b/cmd/crm/flags.go index b705f48c2..d074ab67c 100644 --- a/cmd/crm/flags.go +++ b/cmd/crm/flags.go @@ -5,19 +5,9 @@ import ( "github.com/namsral/flag" ) -type configuration struct { - monitorInterval int -} - -func flags(prefix string, mountFlags ...func(...string)) configuration { - var config configuration - - flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)") - +func flags(prefix string, mountFlags ...func(...string)) { for _, mount := range mountFlags { mount(prefix) } - flag.Parse() - return config } diff --git a/cmd/crm/main.go b/cmd/crm/main.go index 8397c3ad4..f4aa15e94 100644 --- a/cmd/crm/main.go +++ b/cmd/crm/main.go @@ -11,12 +11,11 @@ import ( ) func main() { - config := flags("crm", service.Flags, auth.Flags, rbac.Flags) + flags("crm", service.Flags, auth.Flags, rbac.Flags) // log to stdout not stderr log.SetOutput(os.Stdout) log.SetFlags(log.LstdFlags | log.Lshortfile) - go NewMonitor(config.monitorInterval) if err := service.Init(); err != nil { log.Fatalf("Error initializing crm: %+v", err) diff --git a/cmd/crm/monitor.go b/cmd/crm/monitor.go deleted file mode 100644 index e0e382862..000000000 --- a/cmd/crm/monitor.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "encoding/json" - "expvar" - "fmt" - "runtime" - "time" -) - -type Monitor struct { - Alloc, - TotalAlloc, - Sys, - Mallocs, - Frees, - LiveObjects, - PauseTotalNs uint64 - - NumGC uint32 - NumGoroutine int -} - -func NewMonitor(duration int) { - var ( - m = Monitor{} - rtm runtime.MemStats - goroutines = expvar.NewInt("num_goroutine") - ) - var interval = time.Duration(duration) * time.Second - for { - <-time.After(interval) - - // Read full mem stats - runtime.ReadMemStats(&rtm) - - // Number of goroutines - m.NumGoroutine = runtime.NumGoroutine() - goroutines.Set(int64(m.NumGoroutine)) - - // Misc memory stats - m.Alloc = rtm.Alloc - m.TotalAlloc = rtm.TotalAlloc - m.Sys = rtm.Sys - m.Mallocs = rtm.Mallocs - m.Frees = rtm.Frees - - // Live objects = Mallocs - Frees - m.LiveObjects = m.Mallocs - m.Frees - - // GC Stats - m.PauseTotalNs = rtm.PauseTotalNs - m.NumGC = rtm.NumGC - - // Just encode to json and print - b, _ := json.Marshal(m) - fmt.Println(string(b)) - } -} diff --git a/cmd/sam/flags.go b/cmd/sam/flags.go index b705f48c2..d074ab67c 100644 --- a/cmd/sam/flags.go +++ b/cmd/sam/flags.go @@ -5,19 +5,9 @@ import ( "github.com/namsral/flag" ) -type configuration struct { - monitorInterval int -} - -func flags(prefix string, mountFlags ...func(...string)) configuration { - var config configuration - - flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)") - +func flags(prefix string, mountFlags ...func(...string)) { for _, mount := range mountFlags { mount(prefix) } - flag.Parse() - return config } diff --git a/cmd/sam/main.go b/cmd/sam/main.go index 4c3c73439..f61bb1beb 100644 --- a/cmd/sam/main.go +++ b/cmd/sam/main.go @@ -8,18 +8,14 @@ import ( "github.com/crusttech/crust/internal/auth" "github.com/crusttech/crust/internal/rbac" - "github.com/crusttech/crust/internal/version" ) func main() { - config := flags("sam", service.Flags, auth.Flags, rbac.Flags) - - log.Printf("Starting sam, version: %v, built on: %v", version.Version, version.BuildTime) + flags("sam", service.Flags, auth.Flags, rbac.Flags) // log to stdout not stderr log.SetOutput(os.Stdout) log.SetFlags(log.LstdFlags | log.Lshortfile) - go NewMonitor(config.monitorInterval) if err := service.Init(); err != nil { log.Fatalf("Error initializing sam: %+v", err) diff --git a/cmd/sam/monitor.go b/cmd/sam/monitor.go deleted file mode 100644 index e0e382862..000000000 --- a/cmd/sam/monitor.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "encoding/json" - "expvar" - "fmt" - "runtime" - "time" -) - -type Monitor struct { - Alloc, - TotalAlloc, - Sys, - Mallocs, - Frees, - LiveObjects, - PauseTotalNs uint64 - - NumGC uint32 - NumGoroutine int -} - -func NewMonitor(duration int) { - var ( - m = Monitor{} - rtm runtime.MemStats - goroutines = expvar.NewInt("num_goroutine") - ) - var interval = time.Duration(duration) * time.Second - for { - <-time.After(interval) - - // Read full mem stats - runtime.ReadMemStats(&rtm) - - // Number of goroutines - m.NumGoroutine = runtime.NumGoroutine() - goroutines.Set(int64(m.NumGoroutine)) - - // Misc memory stats - m.Alloc = rtm.Alloc - m.TotalAlloc = rtm.TotalAlloc - m.Sys = rtm.Sys - m.Mallocs = rtm.Mallocs - m.Frees = rtm.Frees - - // Live objects = Mallocs - Frees - m.LiveObjects = m.Mallocs - m.Frees - - // GC Stats - m.PauseTotalNs = rtm.PauseTotalNs - m.NumGC = rtm.NumGC - - // Just encode to json and print - b, _ := json.Marshal(m) - fmt.Println(string(b)) - } -} diff --git a/crm/flags.go b/crm/flags.go index 302474645..f5777ebf8 100644 --- a/crm/flags.go +++ b/crm/flags.go @@ -8,8 +8,9 @@ import ( type ( appFlags struct { - http *config.HTTP - db *config.Database + http *config.HTTP + monitor *config.Monitor + db *config.Database } ) @@ -22,6 +23,9 @@ func (c *appFlags) Validate() error { if err := c.http.Validate(); err != nil { return err } + if err := c.monitor.Validate(); err != nil { + return err + } if err := c.db.Validate(); err != nil { return err } @@ -38,6 +42,7 @@ func Flags(prefix ...string) { flags = &appFlags{ new(config.HTTP).Init(prefix...), + new(config.Monitor).Init(prefix...), new(config.Database).Init(prefix...), } } diff --git a/crm/start.go b/crm/start.go index b3ecd19af..d175e804f 100644 --- a/crm/start.go +++ b/crm/start.go @@ -15,7 +15,10 @@ import ( migrate "github.com/crusttech/crust/crm/db" "github.com/crusttech/crust/crm/rest" + "github.com/crusttech/crust/internal/auth" + "github.com/crusttech/crust/internal/metrics" + "github.com/crusttech/crust/internal/version" ) func Init() error { @@ -59,6 +62,7 @@ func Init() error { func Start() error { var deadline = sigctx.New() + log.Printf("Starting crm, version: %v, built on: %v", version.Version, version.BuildTime) log.Println("Starting http server on address " + flags.http.Addr) listener, err := net.Listen("tcp", flags.http.Addr) if err != nil { @@ -83,6 +87,10 @@ func Start() error { printRoutes(r, flags.http) mountSystemRoutes(r, flags.http) + if flags.monitor.Interval > 0 { + go metrics.NewMonitor(flags.monitor.Interval) + } + go http.Serve(listener, r) <-deadline.Done() diff --git a/internal/config/database.go b/internal/config/database.go index 746ed599c..1afc32d1e 100644 --- a/internal/config/database.go +++ b/internal/config/database.go @@ -33,7 +33,7 @@ func (*Database) Init(prefix ...string) *Database { return prefix[0] + "-" + s } - db := new(Database) + db = new(Database) flag.StringVar(&db.DSN, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") flag.StringVar(&db.Profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)") return db diff --git a/internal/config/http.go b/internal/config/http.go index ed25e5893..c64d9c379 100644 --- a/internal/config/http.go +++ b/internal/config/http.go @@ -36,7 +36,7 @@ func (*HTTP) Init(prefix ...string) *HTTP { return prefix[0] + "-" + s } - http := new(HTTP) + http = new(HTTP) flag.StringVar(&http.Addr, p("http-addr"), ":3000", "Listen address for HTTP server") flag.BoolVar(&http.Logging, p("http-log"), true, "Enable/disable HTTP request log") flag.BoolVar(&http.Pretty, p("http-pretty-json"), false, "Prettify returned JSON output") diff --git a/internal/config/jwt.go b/internal/config/jwt.go index 447c0d6db..cb7892498 100644 --- a/internal/config/jwt.go +++ b/internal/config/jwt.go @@ -31,7 +31,7 @@ func (*JWT) Init(prefix ...string) *JWT { return jwt } - jwt := new(JWT) + jwt = new(JWT) flag.StringVar(&jwt.Secret, "auth-jwt-secret", "", "JWT Secret") flag.Int64Var(&jwt.Expiry, "auth-jwt-expiry", 3600, "JWT Expiration in minutes") flag.StringVar(&jwt.CookieDomain, "auth-jwt-cookie-domain", "", "JWT Cookie domain") diff --git a/internal/config/monitor.go b/internal/config/monitor.go new file mode 100644 index 000000000..598720363 --- /dev/null +++ b/internal/config/monitor.go @@ -0,0 +1,27 @@ +package config + +import ( + "github.com/namsral/flag" +) + +type ( + Monitor struct { + Interval int + } +) + +var monitor *Monitor + +func (c *Monitor) Validate() error { + return nil +} + +func (*Monitor) Init(prefix ...string) *Monitor { + if monitor != nil { + return monitor + } + + monitor = new(Monitor) + flag.IntVar(&monitor.Interval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)") + return monitor +} diff --git a/internal/config/oidc.go b/internal/config/oidc.go index 0e801d9ea..f16ba333f 100644 --- a/internal/config/oidc.go +++ b/internal/config/oidc.go @@ -40,7 +40,7 @@ func (*OIDC) Init(prefix ...string) *OIDC { return oidc } - oidc := new(OIDC) + oidc = new(OIDC) flag.StringVar(&oidc.Issuer, "auth-oidc-issuer", "", "OIDC Issuer") flag.StringVar(&oidc.ClientID, "auth-oidc-client-id", "", "OIDC Client ID") flag.StringVar(&oidc.ClientSecret, "auth-oidc-client-secret", "", "OIDC Client Secret") diff --git a/cmd/auth/monitor.go b/internal/metrics/monitor.go similarity index 98% rename from cmd/auth/monitor.go rename to internal/metrics/monitor.go index e0e382862..0030996dd 100644 --- a/cmd/auth/monitor.go +++ b/internal/metrics/monitor.go @@ -1,4 +1,4 @@ -package main +package metrics import ( "encoding/json" diff --git a/sam/flags.go b/sam/flags.go index d336929c5..7f131ce52 100644 --- a/sam/flags.go +++ b/sam/flags.go @@ -10,6 +10,7 @@ import ( type ( appFlags struct { http *config.HTTP + monitor *config.Monitor db *config.Database repository *repository.Flags } @@ -24,6 +25,9 @@ func (c *appFlags) Validate() error { if err := c.http.Validate(); err != nil { return err } + if err := c.monitor.Validate(); err != nil { + return err + } if err := c.db.Validate(); err != nil { return err } @@ -43,6 +47,7 @@ func Flags(prefix ...string) { flags = &appFlags{ new(config.HTTP).Init(prefix...), + new(config.Monitor).Init(prefix...), new(config.Database).Init(prefix...), new(repository.Flags).Init(prefix...), } diff --git a/sam/start.go b/sam/start.go index 6edead143..260f07ca7 100644 --- a/sam/start.go +++ b/sam/start.go @@ -15,11 +15,14 @@ import ( "github.com/titpetric/factory/resputil" authService "github.com/crusttech/crust/auth/service" - "github.com/crusttech/crust/internal/auth" migrate "github.com/crusttech/crust/sam/db" "github.com/crusttech/crust/sam/rest" samService "github.com/crusttech/crust/sam/service" "github.com/crusttech/crust/sam/websocket" + + "github.com/crusttech/crust/internal/auth" + "github.com/crusttech/crust/internal/metrics" + "github.com/crusttech/crust/internal/version" ) func Init() error { @@ -66,6 +69,7 @@ func Init() error { func Start() error { deadline := sigctx.New() + log.Printf("Starting sam, version: %v, built on: %v", version.Version, version.BuildTime) log.Println("Starting http server on address " + flags.http.Addr) listener, err := net.Listen("tcp", flags.http.Addr) if err != nil { @@ -90,6 +94,10 @@ func Start() error { printRoutes(r, flags.http) mountSystemRoutes(r, flags.http) + if flags.monitor.Interval > 0 { + go metrics.NewMonitor(flags.monitor.Interval) + } + go http.Serve(listener, r) <-deadline.Done()