Skip to content

feat: Log API requests made by CLI #1615

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

Closed
wants to merge 3 commits into from

Conversation

dwahler
Copy link
Contributor

@dwahler dwahler commented May 20, 2022

Basic support for logging API request URLs, behind a --log-requests flag.

Sample usage:

coder@my-dev:~/coder$ ./coder --log-requests show dev
GET http://localhost:3000/api/v2/users/me/organizations 200
GET http://localhost:3000/api/v2/organizations/529f78a9-2413-4ca3-80a3-f4d46ba2f663/workspaces/me/dev 200
GET http://localhost:3000/api/v2/workspacebuilds/82f5c5b4-7a83-4154-a225-a54c74d3e185/resources 200
┌──────────────────────────────────────────────────────────┐
│ RESOURCE                    STATUS       ACCESS          │
├──────────────────────────────────────────────────────────┤
│ docker_container.workspace  ephemeral                    │
│ └─ dev (linux, amd64)       ⦿ connected   coder ssh dev  │
├──────────────────────────────────────────────────────────┤
│ docker_volume.home_volume   ephemeral                    │
└──────────────────────────────────────────────────────────┘

Fixes #1565

@f0ssel
Copy link
Contributor

f0ssel commented May 20, 2022

Thoughts on 'verbose' for the flag? I assume people will use this during scenarios where you would usually also reach for that flag.

displayedStatusCode = strconv.Itoa(response.StatusCode)
}

_, _ = fmt.Fprintf(lrt.Writer, "%s %s %s\n", request.Method, request.URL.String(), displayedStatusCode)
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 it could be nice to add something here that clarifies this isn't a webserver access log, and instead an outbound client log.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I added a sending request: prefix to make this a bit clearer.

@mafredri
Copy link
Member

Thoughts on 'verbose' for the flag? I assume people will use this during scenarios where you would usually also reach for that flag.

I think a --debug flag could also be OK for the request logging (can imply --verbose).

Or in relation to --verbose, I opened #1543 a while ago and would be nice to have -v. Perhaps -vv, -vvv to increase verbosity level (not sure if feasible with Cobra)?

}
}

func (lrt loggingRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Do you want add optionally showing body payloads?

@dwahler
Copy link
Contributor Author

dwahler commented May 20, 2022

Or in relation to --verbose, I opened #1543 a while ago and would be nice to have -v. Perhaps -vv, -vvv to increase verbosity level (not sure if feasible with Cobra)?

Turns out pflag does support this! I updated it so that -v just prints the URLs/status codes, -vv prints request bodies, and -vvv prints response bodies.

@dwahler
Copy link
Contributor Author

dwahler commented May 20, 2022

Seems like it's a bit tricky to format for request/response bodies in a readable way. For now I'm just printing them on a single line with a leading tab to help visually separate them. The body content is unquoted to make it easier to copy and paste.

Also, I'm only printing the body if it appears to be plain text (content type of text/* or application/json); for anything else, I'm just printing the type and size.

@f0ssel
Copy link
Contributor

f0ssel commented May 31, 2022

@dwahler since this got pushed back, would you mind closing for now until this work get's picked back up?

@dwahler dwahler closed this May 31, 2022
@github-actions github-actions bot deleted the david/1565-cli-request-tracing branch February 4, 2023 20:04
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.

Print API requests made by CLI
4 participants