1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
package monitoring
import (
"log"
"net/http/pprof"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Start will start a new monitoring service listening on the address
// configured through the option arguments. Additionally, it'll start
// a Continuous Profiler configured through environment variables
// (see more at https://gitlab.com/gitlab-org/labkit/-/blob/master/monitoring/doc.go).
//
// If `WithListenerAddress` option is provided, Start will block or return a non-nil error,
// similar to `http.ListenAndServe` (for instance).
func Start(options ...Option) error {
config := applyOptions(options)
listener, err := config.listenerFactory()
if err != nil {
return err
}
// Initialize the Continuous Profiler.
if !config.continuousProfilingDisabled {
profOpts := profilerOpts{
ServiceVersion: config.version,
CredentialsFile: config.profilerCredentialsFile,
}
initProfiler(profOpts)
}
if listener == nil {
// No listener has been configured, skip mux setup.
return nil
}
metricsHandler(config)
pprofHandlers(config)
config.server.Handler = config.serveMux
return config.server.Serve(listener)
}
func metricsHandler(cfg optionsConfig) {
if cfg.metricsDisabled {
return
}
// Register the `gitlab_build_info` metric if configured
if len(cfg.buildInfoGaugeLabels) > 0 {
registerBuildInfoGauge(cfg.registerer, cfg.buildInfoGaugeLabels)
}
cfg.serveMux.Handle(
cfg.metricsHandlerPattern,
promhttp.InstrumentMetricHandler(cfg.registerer, promhttp.HandlerFor(cfg.gatherer, promhttp.HandlerOpts{})),
)
}
func pprofHandlers(cfg optionsConfig) {
if cfg.pprofDisabled {
return
}
cfg.serveMux.HandleFunc("/debug/pprof/", pprof.Index)
cfg.serveMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
cfg.serveMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
cfg.serveMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
cfg.serveMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
// Serve will start a new monitoring service listening on the address
// configured through the option arguments. Additionally, it'll start
// a Continuous Profiler configured through environment variables
// (see more at https://gitlab.com/gitlab-org/labkit/-/blob/master/monitoring/doc.go).
//
// If `WithListenerAddress` option is provided, Serve will block or return a non-nil error,
// similar to `http.ListenAndServe` (for instance).
//
// Deprecated: Use Start instead.
func Serve(options ...Option) error {
log.Print("warning: deprecated function, use Start instead")
return Start(options...)
}
|