package httptraces
import (
)
const scopeName = "go.pact.im/x/traces/httptraces"
const (
attrHTTPRequestMethod = "http.request.method"
attrHTTPResponseStatusCode = "http.response.status_code"
attrHTTPRoute = "http.route"
attrURLPath = "url.path"
attrURLFull = "url.full"
attrURLScheme = "url.scheme"
attrServerAddress = "server.address"
attrServerPort = "server.port"
attrClientAddress = "client.address"
attrUserAgentOriginal = "user_agent.original"
attrNetworkProtocolVersion = "network.protocol.version"
attrErrorType = "error.type"
schemeHTTP = "http"
schemeHTTPS = "https"
errorTypePanic = "panic"
)
type Handler struct {
tracer *traces.Tracer
next http.Handler
filter func(*http.Request) bool
extract func(get func(key string) []string) traces.SpanRef
route func(*http.Request) string
spanName func(r *http.Request, route string) string
attrs func(*http.Request) []traces.Attr
}
var _ http.Handler = (*Handler)(nil)
func ( *traces.Tracer, http.Handler) *Handler {
return &Handler{
tracer: .WithScope(scopeName),
next: ,
route: defaultRoute,
spanName: defaultServerSpanName,
}
}
func ( *Handler) ( func(*http.Request) bool) *Handler {
:= *
.filter =
return &
}
func ( *Handler) (
func( func( string) []string) traces.SpanRef,
) *Handler {
:= *
.extract =
return &
}
func ( *Handler) ( func(*http.Request) string) *Handler {
if == nil {
= defaultRoute
}
:= *
.route =
return &
}
func ( *Handler) ( func( *http.Request, string) string) *Handler {
if == nil {
= defaultServerSpanName
}
:= *
.spanName =
return &
}
func ( *Handler) ( func(*http.Request) []traces.Attr) *Handler {
:= *
.attrs =
return &
}
func ( *Handler) ( http.ResponseWriter, *http.Request) {
if .filter != nil && !.filter() {
.next.ServeHTTP(, )
return
}
:= .Context()
var traces.SpanRef
if .extract != nil {
= .extract(.Header.Values)
}
:= []traces.Attr{
traces.String(attrHTTPRequestMethod, .Method),
traces.String(attrURLPath, .URL.Path),
traces.String(attrURLScheme, requestScheme()),
}
if , := splitHostPort(.Host); != "" {
= append(, traces.String(attrServerAddress, ))
if > 0 {
= append(, traces.Int(attrServerPort, ))
}
}
if := protocolVersion(.ProtoMajor, .ProtoMinor); != "" {
= append(, traces.String(attrNetworkProtocolVersion, ))
}
if := .UserAgent(); != "" {
= append(, traces.String(attrUserAgentOriginal, ))
}
if , := splitHostPort(.RemoteAddr); != "" {
= append(, traces.String(attrClientAddress, ))
}
if .attrs != nil {
= append(, .attrs()...)
}
:= .tracer.Start(, , traces.SpanKindServer, .Method, ...)
= traces.NewContext(, .Ref())
= .WithContext()
:= http.StatusOK
:= false
:= httpsnoop.Wrap(, httpsnoop.Hooks{
WriteHeader: func( httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
return func( int) {
if ! {
=
= true
}
()
}
},
Write: func( httpsnoop.WriteFunc) httpsnoop.WriteFunc {
return func( []byte) (int, error) {
if ! {
= true
}
return ()
}
},
})
defer func() {
if := recover(); != nil {
.SetAttrs(traces.String(attrErrorType, errorTypePanic))
.SetError(errPanic)
.End()
panic()
}
}()
.next.ServeHTTP(, )
:= .route()
if != "" {
.SetAttrs(traces.String(attrHTTPRoute, ))
}
if := .spanName(, ); != "" && != .Method {
.SetName()
}
.SetAttrs(traces.Int(attrHTTPResponseStatusCode, ))
if >= http.StatusInternalServerError {
.SetAttrs(traces.String(attrErrorType, strconv.Itoa()))
.SetError(errors.New(http.StatusText()))
}
.End()
}
var errPanic = errors.New(errorTypePanic)
func ( *http.Request) string {
if .TLS != nil {
return schemeHTTPS
}
return schemeHTTP
}
func ( string) (string, int) {
if == "" {
return "", 0
}
, , := net.SplitHostPort()
if != nil {
return , 0
}
, := strconv.Atoi()
if != nil || <= 0 {
return , 0
}
return ,
}
func (, int) string {
switch {
case == 1 && == 0:
return "1.0"
case == 1 && == 1:
return "1.1"
case == 2 && == 0:
return "2"
case == 3 && == 0:
return "3"
case > 0:
return fmt.Sprintf("%d.%d", , )
default:
return ""
}
}
func ( *http.Request) string {
return routeFromPattern(.Pattern)
}
func ( *http.Request, string) string {
if == "" {
return .Method
}
return .Method + " " +
}
func ( string) string {
if , , := strings.Cut(, " "); {
=
}
if := strings.IndexByte(, '/'); >= 0 {
return [:]
}
return ""
}