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
This commit is contained in:
parent
3a76f09ea9
commit
456f9de190
4 changed files with 24 additions and 46 deletions
|
@ -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"`
|
||||
|
|
|
@ -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" {
|
||||
|
|
8
dist/example_config.yaml
vendored
8
dist/example_config.yaml
vendored
|
@ -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.
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in a new issue