gitlab.com/gitlab-org/labkit@v1.21.0/monitoring/start.go (about)

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