gitlab.com/gitlab-org/labkit@v1.21.0/tracing/inbound_http_options.go (about)

     1  package tracing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  // OperationNamer will return an operation name given an HTTP request.
     9  type OperationNamer func(*http.Request) string
    10  
    11  // The configuration for InjectCorrelationID.
    12  type handlerConfig struct {
    13  	getOperationName OperationNamer
    14  }
    15  
    16  // HandlerOption will configure a correlation handler.
    17  type HandlerOption func(*handlerConfig)
    18  
    19  func applyHandlerOptions(opts []HandlerOption) handlerConfig {
    20  	config := handlerConfig{
    21  		getOperationName: func(req *http.Request) string {
    22  			// By default use `GET /x/y/z` for operation names
    23  			return fmt.Sprintf("%s %s", req.Method, req.URL.Path)
    24  		},
    25  	}
    26  	for _, v := range opts {
    27  		v(&config)
    28  	}
    29  
    30  	return config
    31  }
    32  
    33  // WithRouteIdentifier allows a RouteIdentifier attribute to be set in the handler.
    34  // This value will appear in the traces.
    35  func WithRouteIdentifier(routeIdentifier string) HandlerOption {
    36  	return func(config *handlerConfig) {
    37  		config.getOperationName = func(req *http.Request) string {
    38  			// Use `GET routeIdentifier` for operation names
    39  			return fmt.Sprintf("%s %s", req.Method, routeIdentifier)
    40  		}
    41  	}
    42  }