feat: add responseWrapper to exclude errors (status codes >= 400) from being reported

This commit is contained in:
Oleh Astappiev 2024-12-08 21:46:35 +01:00
parent 02f9dd951c
commit a978d4921d
No known key found for this signature in database
GPG key ID: 37D7AC22589E9632
4 changed files with 78 additions and 6 deletions

44
responsewriter.go Normal file
View file

@ -0,0 +1,44 @@
package traefik_umami_feeder
import (
"bufio"
"fmt"
"net"
"net/http"
)
// Copied and adapted from https://github.com/safing/plausiblefeeder/blob/master/responsewriter.go
// Licensed as MIT license
// ResponseWriter is used to wrap given response writers.
type ResponseWriter struct {
http.ResponseWriter
request *http.Request
feeder *UmamiFeeder
}
// WriteHeader adds custom handling to the wrapped WriterHeader method.
func (rw *ResponseWriter) WriteHeader(code int) {
if rw.feeder.shouldTrackStatus(code) {
rw.feeder.submitToFeed(rw.request, code)
}
// Continue with the original method.
rw.ResponseWriter.WriteHeader(code)
}
func (rw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := rw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("%T is not a http.Hijacker", rw.ResponseWriter)
}
return hijacker.Hijack()
}
func (rw *ResponseWriter) Flush() {
if flusher, ok := rw.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}