Add metrics and profile options (#517)

This commit is contained in:
andig 2020-12-10 11:59:55 +01:00 • committed by GitHub
parent 1bc1c45698
commit 79e48ffeff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -16,6 +16,8 @@ import (
type config struct {
URI string
Log string
Metrics bool
Profile bool
Levels map[string]string
Interval time.Duration
Mqtt provider.MqttConfig

View file

@ -2,6 +2,8 @@ package cmd
import (
"fmt"
"net/http"
_ "net/http/pprof" // pprof handler
"os"
"os/signal"
"syscall"
@ -11,8 +13,9 @@ import (
"github.com/andig/evcc/server/updater"
"github.com/andig/evcc/util"
"github.com/andig/evcc/util/pipe"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -74,6 +77,20 @@ func init() {
"Update interval",
)
bind(rootCmd, "interval")
rootCmd.PersistentFlags().Bool(
"metrics",
false,
"Expose metrics",
)
bind(rootCmd, "metrics")
rootCmd.PersistentFlags().Bool(
"profile",
false,
"Expose pprof profiles",
)
bind(rootCmd, "profile")
}
// initConfig reads in config file and ENV variables if set
@ -164,7 +181,15 @@ func run(cmd *cobra.Command, args []string) {
httpd := server.NewHTTPd(uri, site, socketHub, cache)
// metrics
httpd.Handle("/metrics", promhttp.Handler())
if viper.GetBool("metrics") {
httpd.Handle("/metrics", promhttp.Handler())
}
// pprof
if viper.GetBool("profile") {
router := httpd.Handler.(*mux.Router)
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
}
// start HEMS server
if conf.HEMS.Type != "" {