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
Static string
PProf bool
Metrics bool
CORS []string
}
@ -42,6 +43,11 @@ func (Server) Init(cmd *cobra.Command) error {
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")
if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil {
return err
@ -56,6 +62,7 @@ func (s *Server) Set() {
s.Bind = viper.GetString("server.bind")
s.Static = viper.GetString("server.static")
s.PProf = viper.GetBool("server.pprof")
s.Metrics = viper.GetBool("server.metrics")
s.CORS = viper.GetStringSlice("server.cors")
in, _ := utils.ArrayIn("*", s.CORS)

View File

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