2020-03-24 13:15:01 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.opencensus.io/plugin/ochttp"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-10-21 08:18:34 +00:00
|
|
|
func TraceHandler(handler http.Handler, ignoredEndpoints ...string) http.Handler {
|
2020-03-24 13:15:01 +00:00
|
|
|
return &ochttp.Handler{
|
|
|
|
Handler: handler,
|
|
|
|
FormatSpanName: func(r *http.Request) string {
|
|
|
|
host := r.URL.Host
|
|
|
|
if host == "" {
|
|
|
|
host = r.Host
|
|
|
|
}
|
|
|
|
return host + r.URL.Path
|
|
|
|
},
|
|
|
|
|
|
|
|
StartOptions: trace.StartOptions{Sampler: Sampler()},
|
|
|
|
IsHealthEndpoint: func(r *http.Request) bool {
|
2020-10-21 08:18:34 +00:00
|
|
|
for _, endpoint := range ignoredEndpoints {
|
|
|
|
if strings.HasPrefix(r.URL.RequestURI(), endpoint) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2020-03-24 13:15:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|