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

@ -8,12 +8,13 @@ import (
) )
type Server struct { type Server struct {
Cert string Cert string
Key string Key string
Bind string Bind string
Static string Static string
PProf bool PProf bool
CORS []string Metrics bool
CORS []string
} }
func (Server) Init(cmd *cobra.Command) error { func (Server) Init(cmd *cobra.Command) error {
@ -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
}) })
router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) error { if config.Metrics {
promhttp.Handler().ServeHTTP(w, r) router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) error {
return nil promhttp.Handler().ServeHTTP(w, r)
}) return nil
})
}
if config.Static != "" { if config.Static != "" {
fs := http.FileServer(http.Dir(config.Static)) fs := http.FileServer(http.Dir(config.Static))