File: factory_options.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download | duplicates (3)
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
package http_round_tripper

type config struct {
	labelValues map[string]string
}

type factoryConfig struct {
	namespace              string
	subsystem              string
	requestDurationBuckets []float64
	labels                 []string
}

// FactoryOption is used to pass options in NewFactory.
type FactoryOption func(*factoryConfig)

func applyFactoryOptions(opts []FactoryOption) factoryConfig {
	config := factoryConfig{
		subsystem: "http",
		requestDurationBuckets: []float64{
			0.005, /* 5ms */
			0.025, /* 25ms */
			0.1,   /* 100ms */
			0.5,   /* 500ms */
			1.0,   /* 1s */
			10.0,  /* 10s */
			30.0,  /* 30s */
			60.0,  /* 1m */
			300.0, /* 5m */
		},
		labels: []string{"code", "method"},
	}
	for _, v := range opts {
		v(&config)
	}

	return config
}

// WithNamespace will configure the namespace to apply to the metrics.
func WithNamespace(namespace string) FactoryOption {
	return func(config *factoryConfig) {
		config.namespace = namespace
	}
}

// WithLabels will configure additional labels to apply to the metrics.
func WithLabels(labels ...string) FactoryOption {
	return func(config *factoryConfig) {
		config.labels = append(config.labels, labels...)
	}
}

// WithRequestDurationBuckets will configure the duration buckets used for
// incoming request histogram buckets.
func WithRequestDurationBuckets(buckets []float64) FactoryOption {
	return func(config *factoryConfig) {
		config.requestDurationBuckets = buckets
	}
}