-
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Danny Kopping <[email protected]>
Signed-off-by: Danny Kopping <[email protected]>
beb1aca to
d021c47
Compare
|
|
||
| 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) |
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 logger?
|
|
||
| resp, err := next(req) | ||
| if err != nil { | ||
| return resp, err |
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.
Not sure about it but maybe it be useful to have "err" file with error?
| var bodyBytes []byte | ||
| if resp.Body != nil { | ||
| var err error | ||
| bodyBytes, err = io.ReadAll(resp.Body) |
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.
This will make streaming non responsive. I assume this is fine since logging should generally not be enabled other then when debugging.
| func (i *interceptionBase) newCompletionsService(baseURL string, key string) openai.ChatCompletionService { | ||
| opts := []option.RequestOption{option.WithAPIKey(key), option.WithBaseURL(baseURL)} | ||
| func (i *interceptionBase) newCompletionsService() openai.ChatCompletionService { | ||
| opts := []option.RequestOption{option.WithAPIKey(i.cfg.Key), option.WithBaseURL(i.cfg.BaseURL)} |
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.
nit: indent
opts := []option.RequestOption{
option.WithAPIKey(i.cfg.Key),
option.WithBaseURL(i.cfg.BaseURL),
}
| interceptor = responses.NewStreamingInterceptor(id, &req, payload, p.cfg.BaseURL, p.cfg.Key, string(req.Model)) | ||
| } else { | ||
| interceptor = responses.NewBlockingInterceptor(id, &req, payload, p.baseURL, p.key, string(req.Model)) | ||
| interceptor = responses.NewBlockingInterceptor(id, &req, payload, p.cfg.BaseURL, p.cfg.Key, string(req.Model)) |
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.
apidump could be added to responses interceptor as well
| name: config.ProviderOpenAI, | ||
| fixture: fixtures.OaiChatSimple, | ||
| providerName: config.ProviderOpenAI, | ||
| configureFunc: func(addr, dumpDir string, client aibridge.Recorder) (*aibridge.RequestBridge, error) { |
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.
I think inlined configureFunc with provider as a test param would be easier to understand
| reqDumpData, err := os.ReadFile(reqDumpFile) | ||
| require.NoError(t, err, "request dump file should exist") | ||
| require.Contains(t, string(reqDumpData), "POST ") | ||
| require.Contains(t, string(reqDumpData), "Host:") |
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
Logs the raw HTTP requests to and responses from AI providers' APIs.
$ tree /tmp/anthropic/ /tmp/anthropic/ ├── claude-haiku-4-5-20251001 │ ├── bb8e4048-8ea2-4aee-a58d-25027b962bf8.req.txt │ ├── bb8e4048-8ea2-4aee-a58d-25027b962bf8.resp.txt │ ├── e1f338a1-7ae9-4ccf-b898-fc3fbc25124e.req.txt │ └── e1f338a1-7ae9-4ccf-b898-fc3fbc25124e.resp.txt └── claude-opus-4-5-20251101 ├── 024f344e-c75a-46ea-abfb-e45e117e121a.req.txt ├── 024f344e-c75a-46ea-abfb-e45e117e121a.resp.txt ├── 0f02d1f5-882d-474f-b83d-85f09fd63ded.req.txt ├── 0f02d1f5-882d-474f-b83d-85f09fd63ded.resp.txt ├── 7f80f624-bf0b-4c12-a521-8c7654febe5d.req.txt ├── 7f80f624-bf0b-4c12-a521-8c7654febe5d.resp.txt ├── b9a37e54-1cd2-4a4f-92aa-c9adff088f4e.req.txt └── b9a37e54-1cd2-4a4f-92aa-c9adff088f4e.resp.txt