codeintel-qa: Clean up debugging output (#38895)

This commit is contained in:
Eric Fritz 2022-07-15 12:40:15 -05:00 committed by GitHub
parent 579963a160
commit 64ca7ba975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 19 deletions

View File

@ -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)
}
}

View File

@ -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,

View File

@ -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]
}

View File

@ -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") {