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

Expand logging support for HTTP traffic #60

Merged
merged 7 commits into from Aug 10, 2022
Merged

Expand logging support for HTTP traffic #60

merged 7 commits into from Aug 10, 2022

Conversation

mislav
Copy link
Contributor

@mislav mislav commented Aug 2, 2022

The central idea of this change is that GH_DEBUG environment variable is respected the same way it is in gh CLI.

  • Respect the GH_DEBUG environment variable by default, but add the option to skip it;
  • Add option to opt into colorized logging;
  • Add option to explicitly log HTTP headers and bodies—the new default is off;
  • Colorize JSON payloads when logging HTTP bodies;
  • Log form-encoded payloads;
  • Pretty-print GraphQL queries when logging HTTP requests.

- Respect the GH_DEBUG environment variable by default, but add the option to skip it;
- Add option to opt into colorized logging;
- Add option to explicitly log HTTP headers and bodies—the new default is off;
- Colorize JSON payloads when logging HTTP bodies;
- Log form-encoded payloads;
- Pretty-print GraphQL queries when logging HTTP requests.
Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

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

Overall the code looks really good to me. I left a couple questions/comments mostly around documentation.

// LogIgnoreEnv disables respecting the GH_DEBUG environment variable for logging.
LogIgnoreEnv bool

// LogColorize enables colorized logging to Log for display in a terminal.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// LogColorize enables colorized logging to Log for display in a terminal.
// LogColorize enables colorized logging to Log for display in a terminal.
// Default is no coloring.

With the comments I have been trying to describe the default state for each options as I generally find it useful when I look at documentation.

pkg/api/client.go Outdated Show resolved Hide resolved
@@ -0,0 +1,143 @@
package jsonpretty
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want this package to be exported? If so, can we add a comment to the top here describing the purpose and functionality of the package?

@@ -38,6 +38,15 @@ type ClientOptions struct {
// Default is no logging.
Log io.Writer

// LogIgnoreEnv disables respecting the GH_DEBUG environment variable for logging.
LogIgnoreEnv bool
Copy link
Contributor

Choose a reason for hiding this comment

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

After thinking about this more, I am not sure that this option is necessary. I understand its purpose, but can't think of a good use case where we want to allow an extension author to ignore GH_DEBUG.

Additionally, I think our users would find the behavior of this option a bit confusing without further comments describing its behavior.

Copy link
Contributor Author

@mislav mislav Aug 9, 2022

Choose a reason for hiding this comment

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

After thinking about this more, I am not sure that this option is necessary. I understand its purpose, but can't think of a good use case where we want to allow an extension author to ignore GH_DEBUG.

Thanks for chiming in. One consumer of go-gh that wants to use this option is the CLI itself, since GitHub CLI already parses environment variables itself and wants to prevent underlying libraries to parse the same environment variables, lest some inconsistencies or conflicts arise.

Additionally, an extension author might want to disable env-controlled logging if it already offers the user granular controls for logging.

As a general rule of thumb, if a generic Go library automatically respects environment variables, it's a good idea to offer a way for the consumer of said library to skip respecting environment variables in cases where that is not desired. At the end of the day, environment variables are essentially global variables shared across modules and even processes, and to mitigate their potential harms, developers should have ways to put up boundaries to limit the propagation (or effect) of these variables.

I have added some extra documentation detailing this configuration option.

Copy link
Contributor

Choose a reason for hiding this comment

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

That all makes sense to me, my only thought was that if an extension author wanted to ignore GH_DEBUG they could provide a value for the Log option making LogIgnoreEnv feel a bit redundant.

@mislav mislav requested a review from samcoe August 9, 2022 20:51
Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the requested changes 🙇

@mislav mislav merged commit d7a6f56 into trunk Aug 10, 2022
@mislav mislav deleted the mislav/logging branch August 10, 2022 08:58
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.

None yet

2 participants