From 64ca7ba9752e9fc9dc736bbdb1566f06e26d7190 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Fri, 15 Jul 2022 12:40:15 -0500 Subject: [PATCH] codeintel-qa: Clean up debugging output (#38895) --- dev/codeintel-qa/cmd/upload/state.go | 12 +++++++++-- dev/codeintel-qa/cmd/upload/upload.go | 2 +- dev/codeintel-qa/internal/graphql.go | 30 +++++++++++++++++++++++---- internal/gqltestutil/client.go | 24 ++++++++++----------- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/dev/codeintel-qa/cmd/upload/state.go b/dev/codeintel-qa/cmd/upload/state.go index 402d12a2f75..ec0bffe7bb6 100644 --- a/dev/codeintel-qa/cmd/upload/state.go +++ b/dev/codeintel-qa/cmd/upload/state.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "context" + "encoding/json" "fmt" "os" "os/exec" @@ -35,6 +37,7 @@ func monitor(ctx context.Context, repoNames []string, uploads []uploadMeta) erro if err != nil { return err } + request, response := internal.LastRequestResponsePair() if verbose { parts := make([]string, 0, len(repoNames)) @@ -73,7 +76,7 @@ func monitor(ctx context.Context, repoNames []string, uploads []uploadMeta) erro } if oldState != "COMPLETED" { - fmt.Printf("[%5s] %s Finished processing index for %s@%s - ID %s\n", internal.TimeSince(start), internal.EmojiSuccess, repoName, uploadState.upload.commit[:7], uploadState.upload.id) + fmt.Printf("[%5s] %s Finished processing index %s for %s@%s\n", internal.TimeSince(start), internal.EmojiSuccess, uploadState.upload.id, repoName, uploadState.upload.commit[:7]) } } else if uploadState.state != "QUEUED" && uploadState.state != "PROCESSING" { var payload struct { @@ -91,6 +94,11 @@ func monitor(ctx context.Context, repoNames []string, uploads []uploadMeta) erro return errors.Newf("unexpected state '%s' for %s@%s - ID %s\nAudit Logs:\n%s", uploadState.state, uploadState.upload.repoName, uploadState.upload.commit[:7], &uploadState.upload.id, errors.Wrap(err, "error getting audit logs")) } + var dst bytes.Buffer + json.Indent(&dst, []byte(response), "", "\t") + fmt.Printf("GRAPHQL REQUEST:\n%s\n\n", strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(request, "\\t", "\t"), "\\n", "\n"), "\n\n", "\n")) + fmt.Printf("GRAPHQL RESPONSE:\n%s\n\n", dst.String()) + fmt.Printf("RAW STATE DUMP:\n%+v\n", state) fmt.Printf("RAW PAYLOAD DUMP:\n%+v\n", payload) fmt.Println("SEARCHING FOR ID", uploadState.upload.id) @@ -112,7 +120,7 @@ func monitor(ctx context.Context, repoNames []string, uploads []uploadMeta) erro fmt.Printf("DUMP:\n\n%s\n\n\n", out) } - return errors.Newf("unexpected state '%s' for %s@%s - ID %s\nAudit Logs:\n%s", uploadState.state, uploadState.upload.repoName, uploadState.upload.commit[:7], uploadState.upload.id, logs) + return errors.Newf("unexpected state '%s' for %s (%s@%s)\nAudit Logs:\n%s", uploadState.state, uploadState.upload.id, uploadState.upload.repoName, uploadState.upload.commit[:7], logs) } } diff --git a/dev/codeintel-qa/cmd/upload/upload.go b/dev/codeintel-qa/cmd/upload/upload.go index c2d0c9f739b..3df31e07925 100644 --- a/dev/codeintel-qa/cmd/upload/upload.go +++ b/dev/codeintel-qa/cmd/upload/upload.go @@ -53,7 +53,7 @@ func uploadAll(ctx context.Context, commitsByRepo map[string][]string, limiter * return } - fmt.Printf("[%5s] %s Finished uploading index for %s@%s - ID %s\n", internal.TimeSince(start), internal.EmojiSuccess, repoName, commit[:7], graphqlID) + fmt.Printf("[%5s] %s Finished uploading index %s for %s@%s\n", internal.TimeSince(start), internal.EmojiSuccess, graphqlID, repoName, commit[:7]) uploadCh <- uploadMeta{ id: graphqlID, diff --git a/dev/codeintel-qa/internal/graphql.go b/dev/codeintel-qa/internal/graphql.go index bc723349440..d5cab17acd6 100644 --- a/dev/codeintel-qa/internal/graphql.go +++ b/dev/codeintel-qa/internal/graphql.go @@ -1,18 +1,40 @@ package internal import ( - "os" - "github.com/sourcegraph/sourcegraph/internal/gqltestutil" ) -var client *gqltestutil.Client +var ( + client *gqltestutil.Client + requestWriter = &requestResponseWriter{} + responseWriter = &requestResponseWriter{} +) func InitializeGraphQLClient() (err error) { - client, err = gqltestutil.NewClient(SourcegraphEndpoint, os.Stderr, os.Stderr) + client, err = gqltestutil.NewClient(SourcegraphEndpoint, requestWriter.Write, responseWriter.Write) return err } func GraphQLClient() *gqltestutil.Client { return client } + +func LastRequestResponsePair() (string, string) { + return requestWriter.Last(), responseWriter.Last() +} + +type requestResponseWriter struct { + payloads []string +} + +func (w *requestResponseWriter) Write(payload []byte) { + w.payloads = append(w.payloads, string(payload)) +} + +func (w *requestResponseWriter) Last() string { + if len(w.payloads) == 0 { + return "" + } + + return w.payloads[len(w.payloads)-1] +} diff --git a/internal/gqltestutil/client.go b/internal/gqltestutil/client.go index cda185ff807..40b2f2a15fe 100644 --- a/internal/gqltestutil/client.go +++ b/internal/gqltestutil/client.go @@ -98,20 +98,24 @@ type Client struct { sessionCookie *http.Cookie userID string - requestLogger io.StringWriter - responseLogger io.StringWriter + requestLogger LogFunc + responseLogger LogFunc } +type LogFunc func(payload []byte) + +func noopLog(payload []byte) {} + // NewClient instantiates a new client by performing a GET request then obtains the // CSRF token and cookie from its response, if there is one (old versions of Sourcegraph only). // If request- or responseLogger are provided, the request and response bodies, respectively, // will be written to them for any GraphQL requests only. -func NewClient(baseURL string, requestLogger, responseLogger io.StringWriter) (*Client, error) { +func NewClient(baseURL string, requestLogger, responseLogger LogFunc) (*Client, error) { if requestLogger == nil { - requestLogger = io.Discard.(io.StringWriter) + requestLogger = noopLog } if responseLogger == nil { - responseLogger = io.Discard.(io.StringWriter) + responseLogger = noopLog } resp, err := http.Get(baseURL) @@ -125,8 +129,6 @@ func NewClient(baseURL string, requestLogger, responseLogger io.StringWriter) (* return nil, errors.Wrap(err, "read GET body") } - responseLogger.WriteString(string(p)) - csrfToken := extractCSRFToken(string(p)) var csrfCookie *http.Cookie for _, cookie := range resp.Cookies() { @@ -154,8 +156,6 @@ func (c *Client) authenticate(path string, body any) error { return errors.Wrap(err, "marshal body") } - c.requestLogger.WriteString(string(p)) - req, err := http.NewRequest("POST", c.baseURL+path, bytes.NewReader(p)) if err != nil { return errors.Wrap(err, "new request") @@ -274,8 +274,6 @@ func (c *Client) GraphQL(token, query string, variables map[string]any, target a name = matches[2] } - c.requestLogger.WriteString(string(body)) - req, err := http.NewRequest("POST", fmt.Sprintf("%s/.api/graphql?%s", c.baseURL, name), bytes.NewReader(body)) if err != nil { return err @@ -294,6 +292,8 @@ func (c *Client) GraphQL(token, query string, variables map[string]any, target a } } + c.requestLogger(body) + resp, err := http.DefaultClient.Do(req) if err != nil { return err @@ -305,7 +305,7 @@ func (c *Client) GraphQL(token, query string, variables map[string]any, target a return errors.Wrap(err, "read response body") } - c.responseLogger.WriteString(string(body)) + c.responseLogger(body) // Check if the response format should be JSON if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {