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

feat: logging for json unmarshaling errors in Go SDK #79

Closed
wants to merge 2 commits into from

Conversation

MegaportPhilipBrowne
Copy link
Contributor

Contributions

Please read the (Contribution Guidelines)[https://github.com/megaport/megaportgo/wiki/Contributing.md]
prior to lodging Pull Requests (PR).

Description

Please include a summary of the change and which issue is fixed. Please also include any relevant motivation and context.
List any dependencies that are required for this change.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Contributor Agreement

Lodging this Pull Request (PR) indicates agreement with the project's
(Contributor License Agreement)[https://github.com/megaport/megaportgo/wiki/Megaport_Contributor_Licence_Agreement.md].

Please read the Contributor Licence Agreement (CLA) and affirm your acceptance here:

[I have read an accept the CLA]

NOTE If multiple authors have commited to this PR, each one will need to comment on this PR and
agree to the CLA before this PR can be accepted.

@MegaportPhilipBrowne MegaportPhilipBrowne force-pushed the feat/json-marshal-logging branch 2 times, most recently from 8403d6a to 67786f9 Compare October 1, 2024 17:50
Comment on lines +313 to +322
func (c *Client) LogJSONUnmarshalError(ctx context.Context, responseBody string, responseType string, respSchema interface{}, err error) {
expectedJSON, _ := json.Marshal(respSchema)
c.Logger.DebugContext(ctx, "error unmarshaling response body",
slog.String("response_body", responseBody),
slog.String("response_type", responseType),
slog.String("expected_json_schema", fmt.Sprintf("%+v", expectedJSON)),
slog.String("error", err.Error()),
)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better way to approach this would be to add a config option to the logger that would enable logging of all request JSON optionally.

in client.go we have a Do() method that logs every request already, I think what would be good is to add some extra client config to log the response body as part of the existing request logs. I've added the section that does that logging here.

c.Logger.DebugContext(ctx, "completed API request",
		slog.Duration("duration", reqTime),
		slog.Int("status_code", resp.StatusCode),
		slog.String("path", req.URL.EscapedPath()),
		slog.String("api_host", c.BaseURL.Host),
		slog.String("method", req.Method),
		slog.String("trace_id", resp.Header.Get(headerTraceId)),
	)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants