From 456f9de190625994da12ccdd4de002828b1056c4 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Sat, 27 Feb 2021 12:49:24 -0500 Subject: [PATCH] pkg/metrics: move profiles into the metrics server This change: - renames pkg/prometheus into pkg/metrics - renames the prometheus_addr config to metrics_addr - adds pprof endpoints to the metrics server - removes profile/trace cli flags - adds endpoints for profiling to the metrics server --- cmd/chihaya/config.go | 2 +- cmd/chihaya/main.go | 42 +++------------------------ dist/example_config.yaml | 8 +++-- pkg/{prometheus => metrics}/server.go | 18 +++++++++--- 4 files changed, 24 insertions(+), 46 deletions(-) rename pkg/{prometheus => metrics}/server.go (63%) diff --git a/cmd/chihaya/config.go b/cmd/chihaya/config.go index 453bd6b..38ee59c 100644 --- a/cmd/chihaya/config.go +++ b/cmd/chihaya/config.go @@ -30,7 +30,7 @@ type storageConfig struct { // Config represents the configuration used for executing Chihaya. type Config struct { middleware.ResponseConfig `yaml:",inline"` - PrometheusAddr string `yaml:"prometheus_addr"` + MetricsAddr string `yaml:"metrics_addr"` HTTPConfig http.Config `yaml:"http"` UDPConfig udp.Config `yaml:"udp"` Storage storageConfig `yaml:"storage"` diff --git a/cmd/chihaya/main.go b/cmd/chihaya/main.go index 83ecf20..f797bc4 100644 --- a/cmd/chihaya/main.go +++ b/cmd/chihaya/main.go @@ -5,8 +5,6 @@ import ( "os" "os/signal" "runtime" - "runtime/pprof" - "runtime/trace" "strings" "syscall" "time" @@ -18,7 +16,7 @@ import ( "github.com/chihaya/chihaya/frontend/udp" "github.com/chihaya/chihaya/middleware" "github.com/chihaya/chihaya/pkg/log" - "github.com/chihaya/chihaya/pkg/prometheus" + "github.com/chihaya/chihaya/pkg/metrics" "github.com/chihaya/chihaya/pkg/stop" "github.com/chihaya/chihaya/storage" ) @@ -52,8 +50,8 @@ func (r *Run) Start(ps storage.PeerStore) error { r.sg = stop.NewGroup() - log.Info("starting Prometheus server", log.Fields{"addr": cfg.PrometheusAddr}) - r.sg.Add(prometheus.NewServer(cfg.PrometheusAddr)) + log.Info("starting metrics server", log.Fields{"addr": cfg.MetricsAddr}) + r.sg.Add(metrics.NewServer(cfg.MetricsAddr)) if ps == nil { log.Info("starting storage", log.Fields{"name": cfg.Storage.Name}) @@ -112,7 +110,7 @@ func combineErrors(prefix string, errs []error) error { // Stop shuts down an instance of Chihaya. func (r *Run) Stop(keepPeerStore bool) (storage.PeerStore, error) { - log.Debug("stopping frontends and prometheus endpoint") + log.Debug("stopping frontends and metrics server") if errs := r.sg.Stop().Wait(); len(errs) != 0 { return nil, combineErrors("failed while shutting down frontends", errs) } @@ -202,42 +200,12 @@ func RootPreRunCmdFunc(cmd *cobra.Command, args []string) error { log.Info("enabled debug logging") } - tracePath, err := cmd.Flags().GetString("trace") - if err != nil { - return err - } - if tracePath != "" { - f, err := os.Create(tracePath) - if err != nil { - return err - } - trace.Start(f) - log.Info("enabled tracing", log.Fields{"path": tracePath}) - } - - cpuProfilePath, err := cmd.Flags().GetString("cpuprofile") - if err != nil { - return err - } - if cpuProfilePath != "" { - f, err := os.Create(cpuProfilePath) - if err != nil { - return err - } - pprof.StartCPUProfile(f) - log.Info("enabled CPU profiling", log.Fields{"path": cpuProfilePath}) - } - return nil } // RootPostRunCmdFunc handles clean up of any state initialized by command line // flags. func RootPostRunCmdFunc(cmd *cobra.Command, args []string) error { - // These can be called regardless because it noops when not profiling. - pprof.StopCPUProfile() - trace.Stop() - return nil } @@ -251,8 +219,6 @@ func main() { PersistentPostRunE: RootPostRunCmdFunc, } - rootCmd.PersistentFlags().String("cpuprofile", "", "location to save a CPU profile") - rootCmd.PersistentFlags().String("trace", "", "location to save a trace") rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging") rootCmd.PersistentFlags().Bool("json", false, "enable json logging") if runtime.GOOS == "windows" { diff --git a/dist/example_config.yaml b/dist/example_config.yaml index 9d41e4e..c1afcc7 100644 --- a/dist/example_config.yaml +++ b/dist/example_config.yaml @@ -8,9 +8,11 @@ chihaya: min_announce_interval: 15m # The network interface that will bind to an HTTP endpoint that can be - # scraped by an instance of the Prometheus time series database. - # For more info see: https://prometheus.io - prometheus_addr: "0.0.0.0:6880" + # scraped by programs collecting metrics. + # + # /metrics serves metrics in the Prometheus format + # /debug/pprof/{cmdline,profile,symbol,trace} serves profiles in the pprof format + metrics_addr: "0.0.0.0:6880" # This block defines configuration for the tracker's HTTP interface. # If you do not wish to run this, delete this section. diff --git a/pkg/prometheus/server.go b/pkg/metrics/server.go similarity index 63% rename from pkg/prometheus/server.go rename to pkg/metrics/server.go index b729d5c..61aa455 100644 --- a/pkg/prometheus/server.go +++ b/pkg/metrics/server.go @@ -1,10 +1,11 @@ -// Package prometheus implements a standalone HTTP server for serving a -// Prometheus metrics endpoint. -package prometheus +// Package metrics implements a standalone HTTP server for serving pprof +// profiles and Prometheus metrics. +package metrics import ( "context" "net/http" + "net/http/pprof" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -31,10 +32,19 @@ func (s *Server) Stop() stop.Result { // NewServer creates a new instance of a Prometheus server that asynchronously // serves requests. func NewServer(addr string) *Server { + mux := http.NewServeMux() + + mux.Handle("/metrics", promhttp.Handler()) + mux.HandleFunc("/debug/pprof/", pprof.Index) + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) + s := &Server{ srv: &http.Server{ Addr: addr, - Handler: promhttp.Handler(), + Handler: mux, }, }