gitlab.com/gitlab-org/labkit@v1.21.0/metrics/handler_factory_options.go (about)

     1  package metrics
     2  
     3  type handlerFactoryConfig struct {
     4  	namespace                        string
     5  	subsystem                        string
     6  	requestDurationBuckets           []float64
     7  	timeToWriteHeaderDurationBuckets []float64
     8  	byteSizeBuckets                  []float64
     9  	labels                           []string
    10  }
    11  
    12  // HandlerFactoryOption is used to pass options in NewHandlerFactory.
    13  type HandlerFactoryOption func(*handlerFactoryConfig)
    14  
    15  func applyHandlerFactoryOptions(opts []HandlerFactoryOption) handlerFactoryConfig {
    16  	config := handlerFactoryConfig{
    17  		subsystem: "http",
    18  		requestDurationBuckets: []float64{
    19  			0.005, /* 5ms */
    20  			0.025, /* 25ms */
    21  			0.1,   /* 100ms */
    22  			0.5,   /* 500ms */
    23  			1.0,   /* 1s */
    24  			10.0,  /* 10s */
    25  			30.0,  /* 30s */
    26  			60.0,  /* 1m */
    27  			300.0, /* 5m */
    28  		},
    29  		timeToWriteHeaderDurationBuckets: []float64{
    30  			0.005, /* 5ms */
    31  			0.025, /* 25ms */
    32  			0.1,   /* 100ms */
    33  			0.5,   /* 500ms */
    34  			1.0,   /* 1s */
    35  			10.0,  /* 10s */
    36  			30.0,  /* 30s */
    37  		},
    38  		byteSizeBuckets: []float64{
    39  			10,
    40  			64,
    41  			256,
    42  			1024,             /* 1KiB */
    43  			64 * 1024,        /* 64KiB */
    44  			256 * 1024,       /* 256KiB */
    45  			1024 * 1024,      /* 1MiB */
    46  			64 * 1024 * 1024, /* 64MiB */
    47  		},
    48  		labels: []string{"code", "method"},
    49  	}
    50  	for _, v := range opts {
    51  		v(&config)
    52  	}
    53  
    54  	return config
    55  }
    56  
    57  // WithNamespace will configure the namespace to apply to the metrics.
    58  func WithNamespace(namespace string) HandlerFactoryOption {
    59  	return func(config *handlerFactoryConfig) {
    60  		config.namespace = namespace
    61  	}
    62  }
    63  
    64  // WithLabels will configure additional labels to apply to the metrics.
    65  func WithLabels(labels ...string) HandlerFactoryOption {
    66  	return func(config *handlerFactoryConfig) {
    67  		config.labels = append(config.labels, labels...)
    68  	}
    69  }
    70  
    71  // WithRequestDurationBuckets will configure the duration buckets used for
    72  // incoming request histogram buckets.
    73  func WithRequestDurationBuckets(buckets []float64) HandlerFactoryOption {
    74  	return func(config *handlerFactoryConfig) {
    75  		config.requestDurationBuckets = buckets
    76  	}
    77  }
    78  
    79  // WithTimeToWriteHeaderDurationBuckets will configure the time to write header
    80  // duration histogram buckets.
    81  func WithTimeToWriteHeaderDurationBuckets(buckets []float64) HandlerFactoryOption {
    82  	return func(config *handlerFactoryConfig) {
    83  		config.timeToWriteHeaderDurationBuckets = buckets
    84  	}
    85  }
    86  
    87  // WithByteSizeBuckets will configure the byte size histogram buckets for request
    88  // and response payloads.
    89  func WithByteSizeBuckets(buckets []float64) HandlerFactoryOption {
    90  	return func(config *handlerFactoryConfig) {
    91  		config.byteSizeBuckets = buckets
    92  	}
    93  }