feat(check): record what HTTP checks saw

With Spec.Capture, HTTP, JSON and Prometheus checks record every request
and response of the redirect chain, the time spent per phase, the remote
address, the server certificate and the first 32 KiB of the body.
Configured headers, credentials and cookie values are redacted. The
certificate is recorded before verification, which is now done in
VerifyConnection the way crypto/tls does it, because crypto/tls drops the
certificate exactly when verification fails.

Refs #79

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 12:09:42 +01:00
1 parent 94b519b421
commit 48312fc59c
4 files changed
+500 -4

No files matched your search

+46 -4
View File
@@ -10,6 +10,7 @@ import (
"io"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strconv"
"strings"
@@ -133,16 +134,37 @@ type request struct {
// the response body.
func fetch(ctx context.Context, s Spec, r request) (*http.Response, error) {
d := netguard.Dialer(s.AllowPrivate, s.Timeout)
rec := recorderFrom(ctx)
tlsConfig := &tls.Config{InsecureSkipVerify: !r.VerifyTLS} //nolint:gosec // opt-out chosen by the customer per monitor
if rec != nil {
tlsConfig = recordingTLS(rec, r.VerifyTLS)
}
tr := &http.Transport{
DialContext: d.DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !r.VerifyTLS}, //nolint:gosec // opt-out chosen by the customer per monitor
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
TLSHandshakeTimeout: s.Timeout,
ForceAttemptHTTP2: true,
}
client := &http.Client{Transport: tr}
if !r.FollowRedirects {
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
client.CheckRedirect = func(next *http.Request, via []*http.Request) error {
if !r.FollowRedirects {
// The redirect is the final response and recorded as such.
return http.ErrUseLastResponse
}
if rec != nil {
rec.step(via[len(via)-1], next.Response)
}
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
return nil
}
if rec != nil {
ctx = httptrace.WithClientTrace(ctx, rec.trace())
for k := range r.Headers {
rec.secretHeaders[http.CanonicalHeaderKey(k)] = true
}
}
var body io.Reader
if r.Body != "" {
@@ -159,7 +181,27 @@ func fetch(ctx context.Context, s Spec, r request) (*http.Response, error) {
for k, v := range r.Headers {
req.Header.Set(k, v)
}
return client.Do(req)
resp, err := client.Do(req)
if rec == nil {
return resp, err
}
if err != nil {
// The failed request may be a redirect target; url.Error names it.
failed := req
var uerr *url.Error
if errors.As(err, &uerr) {
if u, perr := url.Parse(uerr.URL); perr == nil {
failed = req.Clone(ctx)
failed.URL = u
}
}
rec.step(failed, nil)
return resp, err
}
rec.step(resp.Request, resp)
rec.ev.ContentType = resp.Header.Get("Content-Type")
resp.Body = captureBody{ReadCloser: resp.Body, r: rec}
return resp, nil
}
// validURL accepts absolute http(s) URLs without credentials.