Skip to content

Commit 7530185

Browse files
authored
Merge pull request #2 from Vinelab/exclude_paths
Allow to disable tracing for specified paths
2 parents fac9736 + 76135de commit 7530185

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ import (
248248
func main() {
249249
router := chi.NewRouter()
250250

251-
router.Use(middleware.NewTraceRequests(Trace, []string{"application/json"}).Handler)
251+
// tracer, slice of content-types for request and response bodies you want to log and slcie of excluded url paths
252+
router.Use(middleware.NewTraceRequests(Trace, []string{"application/json"}, []string{}).Handler)
252253

253254
// ...
254255
}

middleware/trace_requests.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import (
1919
type TraceRequests struct {
2020
tracer tracing.Tracer
2121
contentTypes []string
22+
excludedPaths []string
2223
}
2324

2425
// NewTraceRequests creates a new TraceRequests middleware with the provided options
25-
func NewTraceRequests(tracer tracing.Tracer, contentTypes []string) *TraceRequests {
26+
func NewTraceRequests(tracer tracing.Tracer, contentTypes []string, excludedPaths []string) *TraceRequests {
2627
return &TraceRequests{
2728
tracer: tracer,
2829
contentTypes: contentTypes,
30+
excludedPaths: excludedPaths,
2931
}
3032
}
3133

@@ -34,6 +36,11 @@ func NewTraceRequests(tracer tracing.Tracer, contentTypes []string) *TraceReques
3436
// client ip, input, response code and content etc.
3537
func (mdlw *TraceRequests) Handler(next http.Handler) http.Handler {
3638
fn := func(w http.ResponseWriter, r *http.Request) {
39+
if slice.Contains(mdlw.excludedPaths, r.URL.Path) {
40+
next.ServeHTTP(w, r)
41+
return
42+
}
43+
3744
// Create a proxy that hooks into response and allows us to access its contents
3845
response := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
3946

0 commit comments

Comments
 (0)