File: inbound_http_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 (42 lines) | stat: -rw-r--r-- 1,105 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
package tracing

import (
	"fmt"
	"net/http"
)

// OperationNamer will return an operation name given an HTTP request.
type OperationNamer func(*http.Request) string

// The configuration for InjectCorrelationID.
type handlerConfig struct {
	getOperationName OperationNamer
}

// HandlerOption will configure a correlation handler.
type HandlerOption func(*handlerConfig)

func applyHandlerOptions(opts []HandlerOption) handlerConfig {
	config := handlerConfig{
		getOperationName: func(req *http.Request) string {
			// By default use `GET /x/y/z` for operation names
			return fmt.Sprintf("%s %s", req.Method, req.URL.Path)
		},
	}
	for _, v := range opts {
		v(&config)
	}

	return config
}

// WithRouteIdentifier allows a RouteIdentifier attribute to be set in the handler.
// This value will appear in the traces.
func WithRouteIdentifier(routeIdentifier string) HandlerOption {
	return func(config *handlerConfig) {
		config.getOperationName = func(req *http.Request) string {
			// Use `GET routeIdentifier` for operation names
			return fmt.Sprintf("%s %s", req.Method, routeIdentifier)
		}
	}
}