Go中监听HTTP请求需实现http.Handler接口,通过中间件包装原始handler;监听请求/响应体需用io.TeeReader和自定义ResponseWriter;客户端请求监控应在http.Transport.RoundTrip层;生产环境应采样而非全量捕获,并脱敏敏感字段。
http.ServeMux 或 http.Handler 包裹原始 handler 实现请求监听Go 的 net/http 没有内置“拦截器”概念,但所有请求都经过 http.Handler 接口。最直接的方式是写一个中间层 handler,在调用下游 handler 前后插入日志、计时、统计等逻辑。
常见错误是直接修改 http.DefaultServeMux 后再调用 http.ListenAndServe,却忘了自己没做任何包装——那根本不算监听,只是注册路由。
http.Handler 接口(即定义 ServeHTTP(http.ResponseWriter, *http.Request) 方法)req.URL.Path、req.Method、req.Header 等字段,再调用 next.ServeHTTP(w, req)
defer 记录响应耗时:用 time.Now() 开始,time.Since() 结束func loggingHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
log.Printf("→ %s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
log.Printf("← %s %s %v", r.Method, r.URL.Path, time.Since(start))
})
}
http.ListenAndServe(":8080", loggingHandler(http.DefaultServeMux))
io.TeeReader 和自定义 ResponseWriter
默认的 http.Request.Body 是单次读取流,直接 io.ReadAll(r.Body) 会导致后续 handler 读不到数据;同理,http.ResponseWriter 不允许二次写入或读取已发内容。要真正“看到”请求/响应载荷,必须做流代理。
关键点在于:不能破坏原有流语义,也不能阻塞或提前关闭连接。
io.TeeReader(r.Body, writer) 把字节同时写入缓冲区和原 body,再替换 r.Body 为新 io.NopCloser 包装的缓冲读取器ResponseWriter,重写 Write()、WriteHeader()、Header() 方法,把输出先暂存到 bytes.Buffer
WriteHeader() 被调用后才可读取,不能只依赖 Write()
type captureResponseWriter struct {
http.ResponseWriter
buf *bytes.Buffer
}
func (w *captureResponseWriter) Write(b []byte) (int, error) {
w.buf.Write(b)
return w.ResponseWriter.Write(b)
}
func (w *captureResponseWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
}
func captureHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
cw := &captureResponseWriter{
ResponseWriter: w,
buf: &buf,
}
next.ServeHTTP(cw, r)
log.Printf("response body: %s", buf.String())
})
}
http.Transport 层监控仅适用于 HTTP 客户端出向请求如果你要监控的是本程序作为 HTTP 客户端发出的请求(比如调用第三方 API),那得换到 http.Transport 层——它控制连接复用、超时、TLS 握手等,也能通过 RoundTrip 钩子捕获请求/响应。
别误以为设了 http.DefaultClient.Transport 就能监听服务端进来的请求:那是两套完全不相干的路径。
&http.Transport{},并覆盖其 RoundTrip 方法RoundTrip 中记录 req.URL、req.Header,再调用原 transport 的 RoundTrip
io.TeeReader 或 httputil.DumpResponse(仅调试,勿用于生产)originalRT := http.DefaultTransport.RoundTrip
http.DefaultTransport.RoundTrip = func(req *http.Request) (*http.Response, error) {
log.Printf("client → %s %s", req.Method, req.URL.String())
resp, err := originalRT(req)
if err == nil {
log.Printf("client ← %s %d", req.URL.String(), resp.StatusCode)
}
return resp, err
}
完整记录每个请求/响应体极易拖垮性能、撑爆内存或泄露敏感字段(如 token、密码)。真实系统中,应按需采样,且只提取关键字段做指标聚合。
最容易被忽略的是 header 大小限制和 body 截断策略——不设限时,一个 100MB 的上传请求会卡住整个 handler。
http.MaxBytesReader 包裹 r.Body,防止恶意大 body 占用资源Authorization、Cookie)必须脱敏,至少打码前几位otelhttp.NewHandler 替代手写日志,自动注入 trace context 并导出 metrics真要 debug 某个异常请求,靠日志 ID 查找比全局监听更高效也更安全。