-
Notifications
You must be signed in to change notification settings - Fork 3
feat: req/resp logging middleware #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dannykopping
wants to merge
5
commits into
main
Choose a base branch
from
dk/request-logging-redux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d96635
feat: req/resp logging middleware
dannykopping 2a3e465
chore: rename requestlog->apidump
dannykopping 745f876
chore: fix tests; need to make path deterministic
dannykopping 4f777db
feat: redact headers & stream response bodies
dannykopping db0f11e
feat: allow dumping via env var
dannykopping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package apidump | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/coder/quartz" | ||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| const ( | ||
| // SuffixRequest is the file suffix for request dump files. | ||
| SuffixRequest = ".req.txt" | ||
| // SuffixResponse is the file suffix for response dump files. | ||
| SuffixResponse = ".resp.txt" | ||
| ) | ||
|
|
||
| // MiddlewareNext is the function to call the next middleware or the actual request. | ||
| type MiddlewareNext = func(*http.Request) (*http.Response, error) | ||
|
|
||
| // Middleware is an HTTP middleware function compatible with SDK WithMiddleware options. | ||
| type Middleware = func(*http.Request, MiddlewareNext) (*http.Response, error) | ||
|
|
||
| // NewMiddleware returns a middleware function that dumps requests and responses to files. | ||
| // Files are written to the path returned by DumpPath. | ||
| // If baseDir is empty, returns nil (no middleware). | ||
| func NewMiddleware(baseDir, provider, model string, interceptionID uuid.UUID, clk quartz.Clock) Middleware { | ||
| if baseDir == "" { | ||
| return nil | ||
| } | ||
|
|
||
| d := &dumper{ | ||
| baseDir: baseDir, | ||
| provider: provider, | ||
| model: model, | ||
| interceptionID: interceptionID, | ||
| clk: clk, | ||
| } | ||
|
|
||
| return func(req *http.Request, next MiddlewareNext) (*http.Response, error) { | ||
| if err := d.dumpRequest(req); err != nil { | ||
| fmt.Fprintf(os.Stderr, "apidump: failed to dump request: %v\n", err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe logger? |
||
| } | ||
|
|
||
| resp, err := next(req) | ||
| if err != nil { | ||
| return resp, err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about it but maybe it be useful to have "err" file with error? |
||
| } | ||
|
|
||
| if err := d.dumpResponse(resp); err != nil { | ||
| fmt.Fprintf(os.Stderr, "apidump: failed to dump response: %v\n", err) | ||
| } | ||
|
|
||
| return resp, nil | ||
| } | ||
| } | ||
|
|
||
| type dumper struct { | ||
| baseDir string | ||
| provider string | ||
| model string | ||
| interceptionID uuid.UUID | ||
| clk quartz.Clock | ||
| } | ||
|
|
||
| func (d *dumper) dumpRequest(req *http.Request) error { | ||
| dumpPath := d.path(SuffixRequest) | ||
| if err := os.MkdirAll(filepath.Dir(dumpPath), 0o755); err != nil { | ||
| return fmt.Errorf("create dump dir: %w", err) | ||
| } | ||
|
|
||
| // Read and restore body | ||
| var bodyBytes []byte | ||
| if req.Body != nil { | ||
| var err error | ||
| bodyBytes, err = io.ReadAll(req.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("read request body: %w", err) | ||
| } | ||
| req.Body = io.NopCloser(bytes.NewReader(bodyBytes)) | ||
| } | ||
|
|
||
| // Build raw HTTP request format | ||
| var buf bytes.Buffer | ||
| fmt.Fprintf(&buf, "%s %s %s\r\n", req.Method, req.URL.RequestURI(), req.Proto) | ||
| fmt.Fprintf(&buf, "Host: %s\r\n", req.Host) | ||
| for key, values := range req.Header { | ||
| _, sensitive := sensitiveRequestHeaders[key] | ||
| for _, value := range values { | ||
| if sensitive { | ||
| value = redactHeaderValue(value) | ||
| } | ||
| fmt.Fprintf(&buf, "%s: %s\r\n", key, value) | ||
| } | ||
| } | ||
| fmt.Fprintf(&buf, "\r\n") | ||
| buf.Write(prettyPrintJSON(bodyBytes)) | ||
|
|
||
| return os.WriteFile(dumpPath, buf.Bytes(), 0o644) | ||
| } | ||
|
|
||
| func (d *dumper) dumpResponse(resp *http.Response) error { | ||
| dumpPath := d.path(SuffixResponse) | ||
|
|
||
| // Build raw HTTP response headers | ||
| var headerBuf bytes.Buffer | ||
| fmt.Fprintf(&headerBuf, "%s %s\r\n", resp.Proto, resp.Status) | ||
| for key, values := range resp.Header { | ||
| _, sensitive := sensitiveResponseHeaders[key] | ||
| for _, value := range values { | ||
| if sensitive { | ||
| value = redactHeaderValue(value) | ||
| } | ||
| fmt.Fprintf(&headerBuf, "%s: %s\r\n", key, value) | ||
| } | ||
| } | ||
| fmt.Fprintf(&headerBuf, "\r\n") | ||
|
|
||
| // Wrap the response body to capture it as it streams | ||
| if resp.Body != nil { | ||
| resp.Body = &streamingBodyDumper{ | ||
| body: resp.Body, | ||
| dumpPath: dumpPath, | ||
| headerData: headerBuf.Bytes(), | ||
| } | ||
| } else { | ||
| // No body, just write headers | ||
| return os.WriteFile(dumpPath, headerBuf.Bytes(), 0o644) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // path returns the path to a request/response dump file for a given interception. | ||
| // suffix should be SuffixRequest or SuffixResponse. | ||
| func (d *dumper) path(suffix string) string { | ||
| safeModel := strings.ReplaceAll(d.model, "/", "-") | ||
| return filepath.Join(d.baseDir, d.provider, safeModel, fmt.Sprintf("%d-%s%s", d.clk.Now().UTC().UnixMilli(), d.interceptionID, suffix)) | ||
| } | ||
|
|
||
| // prettyPrintJSON returns indented JSON if body is valid JSON, otherwise returns body as-is. | ||
| func prettyPrintJSON(body []byte) []byte { | ||
| if len(body) == 0 { | ||
| return body | ||
| } | ||
| var parsed any | ||
| if err := json.Unmarshal(body, &parsed); err != nil { | ||
| return body | ||
| } | ||
| pretty, err := json.MarshalIndent(parsed, "", " ") | ||
| if err != nil { | ||
| return body | ||
| } | ||
| return pretty | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add check like
require.Contains(t, string(reqDumpData), string(reqBody))and similar for response