add metrics to config.

This commit is contained in:
Miroslav Šedivý 2022-07-04 18:26:29 +02:00
parent bf47e5a8d0
commit 2bf83f6217
2 changed files with 19 additions and 10 deletions

View File

@ -13,6 +13,7 @@ type Server struct {
Bind string Bind string
Static string Static string
PProf bool PProf bool
Metrics bool
CORS []string CORS []string
} }
@ -42,6 +43,11 @@ func (Server) Init(cmd *cobra.Command) error {
return err return err
} }
cmd.PersistentFlags().Bool("server.metrics", true, "enable prometheus metrics available at /metrics")
if err := viper.BindPFlag("server.metrics", cmd.PersistentFlags().Lookup("server.metrics")); err != nil {
return err
}
cmd.PersistentFlags().StringSlice("server.cors", []string{"*"}, "list of allowed origins for CORS") cmd.PersistentFlags().StringSlice("server.cors", []string{"*"}, "list of allowed origins for CORS")
if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil { if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil {
return err return err
@ -56,6 +62,7 @@ func (s *Server) Set() {
s.Bind = viper.GetString("server.bind") s.Bind = viper.GetString("server.bind")
s.Static = viper.GetString("server.static") s.Static = viper.GetString("server.static")
s.PProf = viper.GetBool("server.pprof") s.PProf = viper.GetBool("server.pprof")
s.Metrics = viper.GetBool("server.metrics")
s.CORS = viper.GetStringSlice("server.cors") s.CORS = viper.GetStringSlice("server.cors")
in, _ := utils.ArrayIn("*", s.CORS) in, _ := utils.ArrayIn("*", s.CORS)

View File

@ -47,10 +47,12 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c
return err return err
}) })
if config.Metrics {
router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) error { router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) error {
promhttp.Handler().ServeHTTP(w, r) promhttp.Handler().ServeHTTP(w, r)
return nil return nil
}) })
}
if config.Static != "" { if config.Static != "" {
fs := http.FileServer(http.Dir(config.Static)) fs := http.FileServer(http.Dir(config.Static))