File: handler_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 (93 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download | duplicates (4)
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
88
89
90
91
92
93
package metrics

type handlerFactoryConfig struct {
	namespace                        string
	subsystem                        string
	requestDurationBuckets           []float64
	timeToWriteHeaderDurationBuckets []float64
	byteSizeBuckets                  []float64
	labels                           []string
}

// HandlerFactoryOption is used to pass options in NewHandlerFactory.
type HandlerFactoryOption func(*handlerFactoryConfig)

func applyHandlerFactoryOptions(opts []HandlerFactoryOption) handlerFactoryConfig {
	config := handlerFactoryConfig{
		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 */
		},
		timeToWriteHeaderDurationBuckets: []float64{
			0.005, /* 5ms */
			0.025, /* 25ms */
			0.1,   /* 100ms */
			0.5,   /* 500ms */
			1.0,   /* 1s */
			10.0,  /* 10s */
			30.0,  /* 30s */
		},
		byteSizeBuckets: []float64{
			10,
			64,
			256,
			1024,             /* 1KiB */
			64 * 1024,        /* 64KiB */
			256 * 1024,       /* 256KiB */
			1024 * 1024,      /* 1MiB */
			64 * 1024 * 1024, /* 64MiB */
		},
		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) HandlerFactoryOption {
	return func(config *handlerFactoryConfig) {
		config.namespace = namespace
	}
}

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

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

// WithTimeToWriteHeaderDurationBuckets will configure the time to write header
// duration histogram buckets.
func WithTimeToWriteHeaderDurationBuckets(buckets []float64) HandlerFactoryOption {
	return func(config *handlerFactoryConfig) {
		config.timeToWriteHeaderDurationBuckets = buckets
	}
}

// WithByteSizeBuckets will configure the byte size histogram buckets for request
// and response payloads.
func WithByteSizeBuckets(buckets []float64) HandlerFactoryOption {
	return func(config *handlerFactoryConfig) {
		config.byteSizeBuckets = buckets
	}
}