blob: 0a0aff607012c43a031cbe2ce9e47cf032cc2d49 (
plain)
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)
}
}
}
|