Skip to content
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

chore: include request id in debug logs and diagnostics #581

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func NewClientWithConfig(config *Config) (*Client, error) {
// if enabled we log all requests and responses to sterr
client.httpClient.Logger = log.New(os.Stderr, "", log.LstdFlags)
client.httpClient.ResponseLogHook = func(l retryablehttp.Logger, resp *http.Response) {
l.Printf("[DEBUG] Response: %s %s", resp.Request.Method, resp.Request.URL.String())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to say "Request" because we weren't actually logging much about the response, just the request. I added the request ID and the status as well.

l.Printf("[DEBUG] Request: %s \"%s\" %s %s", resp.Request.Method, resp.Request.URL.String(), resp.Status, resp.Header.Get("Request-Id"))
}
}

Expand Down
7 changes: 7 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type DetailedError struct {
Status int `json:"status,omitempty"`
// The error message
Message string `json:"error,omitempty"`
// ID unique ID of the HTTP request that caused this error.
ID string `json:"request_id,omitempty"`
// Type is a URI used to uniquely identify the type of error.
Type string `json:"type,omitempty"`
// Title is a human-readable summary that explains the type of the problem.
Expand Down Expand Up @@ -87,6 +89,8 @@ func ErrorFromResponse(r *http.Response) error {
return errors.New("invalid response")
}

requestID := r.Header.Get("Request-Id")

switch r.Header.Get("Content-Type") {
case jsonapi.MediaType:
var detailedError DetailedError
Expand All @@ -97,10 +101,12 @@ func ErrorFromResponse(r *http.Response) error {
return DetailedError{
Status: r.StatusCode,
Message: r.Status,
ID: requestID,
}
}

detailedError = DetailedError{
ID: requestID,
Status: r.StatusCode,
Title: errPayload.Errors[0].Title,
}
Expand Down Expand Up @@ -130,6 +136,7 @@ func ErrorFromResponse(r *http.Response) error {
if err := json.NewDecoder(r.Body).Decode(&detailedError); err != nil {
// If we can't decode the error, return a generic error
return DetailedError{
ID: requestID,
Status: r.StatusCode,
Message: r.Status,
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions honeycombio/type_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func diagFromDetailedErr(err honeycombio.DetailedError) diag.Diagnostics {
detail += d.Field + " "
}
detail += d.Description
detail += fmt.Sprintf(" - ID: %s", err.ID)

diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand Down
4 changes: 2 additions & 2 deletions internal/helper/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func AddDiagnosticOnError(diag *diag.Diagnostics, summary string, err error) boo
}

func (d DetailedErrorDiagnostic) Detail() string {
response := fmt.Sprintf("ID: %s\n", d.e.ID)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're doing some confusing things with looping over the Details of the error to generate the message, and it's otherwise not super consistent as far as I can tell. Super open to other ideas of how to format this.

if len(d.e.Details) > 0 {
var response string
for i, dt := range d.e.Details {
response += dt.Code + " - " + dt.Description

Expand All @@ -65,7 +65,7 @@ func (d DetailedErrorDiagnostic) Detail() string {
return response
}

return d.e.Message
return response + d.e.Message
}

func (d DetailedErrorDiagnostic) Summary() string {
Expand Down