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

     1  package monitoring
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"runtime/debug"
     7  
     8  	"github.com/prometheus/client_golang/prometheus"
     9  )
    10  
    11  type listenerFactory func() (net.Listener, error)
    12  
    13  type optionsConfig struct {
    14  	listenerFactory             listenerFactory
    15  	buildInfoGaugeLabels        prometheus.Labels
    16  	registerer                  prometheus.Registerer
    17  	gatherer                    prometheus.Gatherer
    18  	version                     string
    19  	continuousProfilingDisabled bool
    20  	metricsDisabled             bool
    21  	pprofDisabled               bool
    22  	metricsHandlerPattern       string
    23  	profilerCredentialsFile     string
    24  	serveMux                    *http.ServeMux
    25  	server                      *http.Server
    26  }
    27  
    28  // Option is used to pass options to NewListener.
    29  type Option func(*optionsConfig)
    30  
    31  func defaultOptions() optionsConfig {
    32  	return optionsConfig{
    33  		listenerFactory:       defaultListenerFactory,
    34  		buildInfoGaugeLabels:  prometheus.Labels{},
    35  		registerer:            prometheus.DefaultRegisterer,
    36  		gatherer:              prometheus.DefaultGatherer,
    37  		metricsHandlerPattern: "/metrics",
    38  		serveMux:              http.NewServeMux(),
    39  		server:                &http.Server{},
    40  	}
    41  }
    42  
    43  func applyOptions(opts []Option) optionsConfig {
    44  	config := defaultOptions()
    45  
    46  	for _, v := range opts {
    47  		v(&config)
    48  	}
    49  
    50  	return config
    51  }
    52  
    53  // WithServer will configure the health check endpoint to use the provided server.
    54  func WithServer(s *http.Server) Option {
    55  	return func(config *optionsConfig) {
    56  		config.server = s
    57  	}
    58  }
    59  
    60  // WithListener will configure the health check endpoint to use the provided listener.
    61  func WithListener(listener net.Listener) Option {
    62  	return func(config *optionsConfig) {
    63  		config.listenerFactory = func() (net.Listener, error) {
    64  			return listener, nil
    65  		}
    66  	}
    67  }
    68  
    69  // WithListenerAddress will configure the health check endpoint to use the provided listener.
    70  func WithListenerAddress(addr string) Option {
    71  	return func(config *optionsConfig) {
    72  		config.listenerFactory = func() (net.Listener, error) {
    73  			return net.Listen("tcp", addr)
    74  		}
    75  	}
    76  }
    77  
    78  // WithBuildInformation will configure the `gitlab_build_info` metric with appropriate labels.
    79  func WithBuildInformation(version string, buildTime string) Option {
    80  	return func(config *optionsConfig) {
    81  		config.buildInfoGaugeLabels[buildInfoVersionLabel] = version
    82  		config.buildInfoGaugeLabels[buildInfoBuildTimeLabel] = buildTime
    83  		config.version = version
    84  	}
    85  }
    86  
    87  // WithGoBuildInformation will configure the `gitlab_build_info` metric with
    88  // appropriate labels derived from the [runtime/debug.BuildInfo].
    89  func WithGoBuildInformation(info *debug.BuildInfo) Option {
    90  	return func(config *optionsConfig) {
    91  		config.buildInfoGaugeLabels[buildInfoGoVersionLabel] = info.GoVersion
    92  		config.buildInfoGaugeLabels[buildInfoModulePathLabel] = info.Path
    93  		config.buildInfoGaugeLabels[buildInfoModuleVersionLabel] = info.Main.Version
    94  
    95  		for _, setting := range info.Settings {
    96  			switch setting.Key {
    97  			case "vcs.revision":
    98  				config.buildInfoGaugeLabels[buildInfoVersionLabel] = setting.Value
    99  				config.version = setting.Value
   100  			case "vcs.modified":
   101  				config.buildInfoGaugeLabels[buildInfoModifiedLabel] = setting.Value
   102  			case "vcs.time":
   103  				config.buildInfoGaugeLabels[buildInfoCommittedLabel] = setting.Value
   104  			}
   105  		}
   106  	}
   107  }
   108  
   109  // WithBuildExtraLabels will configure extra labels on the `gitlab_build_info` metric.
   110  func WithBuildExtraLabels(labels map[string]string) Option {
   111  	return func(config *optionsConfig) {
   112  		for key, v := range labels {
   113  			config.buildInfoGaugeLabels[key] = v
   114  		}
   115  	}
   116  }
   117  
   118  // WithMetricsHandlerPattern configures the pattern that the metrics handler is registered for (defaults to `/metrics`).
   119  func WithMetricsHandlerPattern(pattern string) Option {
   120  	return func(config *optionsConfig) {
   121  		config.metricsHandlerPattern = pattern
   122  	}
   123  }
   124  
   125  // WithoutContinuousProfiling disables the continuous profiler.
   126  func WithoutContinuousProfiling() Option {
   127  	return func(config *optionsConfig) {
   128  		config.continuousProfilingDisabled = true
   129  	}
   130  }
   131  
   132  // WithoutMetrics disables the metrics endpoint.
   133  func WithoutMetrics() Option {
   134  	return func(config *optionsConfig) {
   135  		config.metricsDisabled = true
   136  	}
   137  }
   138  
   139  // WithoutPprof disables the pprof endpoint.
   140  func WithoutPprof() Option {
   141  	return func(config *optionsConfig) {
   142  		config.pprofDisabled = true
   143  	}
   144  }
   145  
   146  // WithProfilerCredentialsFile configures the credentials file to be used for the profiler service.
   147  func WithProfilerCredentialsFile(path string) Option {
   148  	return func(config *optionsConfig) {
   149  		config.profilerCredentialsFile = path
   150  	}
   151  }
   152  
   153  // WithPrometheusGatherer sets the prometheus.Gatherer to expose in the metrics endpoint.
   154  func WithPrometheusGatherer(gatherer prometheus.Gatherer) Option {
   155  	return func(config *optionsConfig) {
   156  		config.gatherer = gatherer
   157  	}
   158  }
   159  
   160  // WithPrometheusRegisterer sets the prometheus.Registerer to use to register metrics from this package.
   161  func WithPrometheusRegisterer(registerer prometheus.Registerer) Option {
   162  	return func(config *optionsConfig) {
   163  		config.registerer = registerer
   164  	}
   165  }
   166  
   167  // WithServeMux will configure the health check endpoint to use the provided http.ServeMux.
   168  func WithServeMux(mux *http.ServeMux) Option {
   169  	return func(config *optionsConfig) {
   170  		config.serveMux = mux
   171  	}
   172  }
   173  
   174  func defaultListenerFactory() (net.Listener, error) {
   175  	return nil, nil
   176  }