Content-Length: 770517 | pFad | http://github.com/PowerShell/PowerShell/pull/25510

4E WebRequest Cmdlets: Improve Verbose and Debug Logging Level Messaging by JustinGrote · Pull Request #25510 · PowerShell/PowerShell · GitHub
Skip to content

WebRequest Cmdlets: Improve Verbose and Debug Logging Level Messaging #25510

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

JustinGrote
Copy link
Contributor

@JustinGrote JustinGrote commented May 5, 2025

PR Summary

  • Moves debug message processing to separate methods to reduce GetResponse cognitive load
  • Verbose messages redesigned to a one-line summary suited for watching the HTTP transactional flow of a script/module/etc.
  • Debug messages redesigned to show query, header and body content, and intelligently hide binary content to prevent trashing the console/logs
  • Messages are conditionally generated based on Preference setting for performance.
  • Humanize the content payload byte length (thanks @kilasuit)
  • For Invoke-Restmethod, show the response encoding detail as a debug message
  • Removes an unused do/while loop that never triggered until some cleanup was done.

Examples

image
image

PR Context

Closes #25492

PR Checklist

@JustinGrote JustinGrote marked this pull request as draft May 5, 2025 16:29
@JustinGrote JustinGrote changed the title WebRequest Cmdlets: Improve Verbose and Debug Logging Levels and include Request Content WebRequest Cmdlets: Improve Verbose and Debug Logging Level Messaging May 5, 2025
@JustinGrote JustinGrote marked this pull request as ready for review May 5, 2025 23:02
@JustinGrote
Copy link
Contributor Author

JustinGrote commented May 5, 2025

@iSazonov ready to review!

EDIT: I lied, but now codefactor stuff is fixed.

@iSazonov
Copy link
Collaborator

iSazonov commented May 6, 2025

I wonder how many code you update. 😄 I expect you add only content output.

I like you create helper methods. But now output is too large and we must generate the output only it is requested.
I don't find method(s) to check the condition and we could create it here.
For start see how internal void WriteDebug(DebugRecord record, bool overrideInquire = false) does the check.
We can get ((MshCommandRuntime)this.CommandRuntime).DebugPreference and reuse code from internal bool WriteHelper_ShouldWrite
(All in src\System.Management.Automation\engine\MshCommandRuntime.cs)

The same for verbose. But I wouldn't waste time on verbose right now at all. But it's up to you.

Also I'd prefer to use standard TosString() for request and response. Why do we create custom implementation?

@JustinGrote
Copy link
Contributor Author

JustinGrote commented May 6, 2025

@iSazonov I had this discussion with @SeeminglyScience on discord and it is noted there isn't an ideal reliable way to check preference (if you check the first commit I was testing for it prior) and he recommended not establishing it as a pattern but rather this should be something the runtime provides as part of the PSCmdlet abstract class.

In testing the code has a non-visible impact even on large payloads if Verbose/Debug aren't specified.

I moved away from the Request/Response message toString in order to match the format as noted, and reduce redundant information between Verbose and Debug. I put the user experience in favor of the direct implementation. We can revert that if required, but I would lobby for this, especially since I try/catch potential edge cases (haven't hit any so far in testing)

EDIT: Also I made very sure not to alter existing Webcmdlet behavior, the additional areas I changed were just additive where they made sense in context, I'm very aware this is some hot-path stuff and I'm keeping the scope just to the output aspects.

@iSazonov
Copy link
Collaborator

iSazonov commented May 6, 2025

@JustinGrote Yes, we haven't std methods for the checks. I think it's important to have them because later we can add even more output (for example certificates)
One way is to create them. So I open #25514. If you could ask him in Discord to review it, that would be great.
Another way is to use simple workaround only for interactive scenario. We could check switches by more reliable way than you did - Debug.IsPresent/Verbose.IsPresent. We could use this if my PR will be blocked/rejected.

@JustinGrote
Copy link
Contributor Author

Sounds good. Do we want to merge this PR as-is for the next preview and add the conditional logic later, or wait for #25514?

@iSazonov
Copy link
Collaborator

iSazonov commented May 7, 2025

Let's wait for what Patrick says. If that PR is not approved, then let's look at switch checks. What I definitely would not like is to create these strings without a condition.
We can discuss the details for now.

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Mainly style comments.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label May 7, 2025
@iSazonov iSazonov self-assigned this May 7, 2025
@JustinGrote
Copy link
Contributor Author

JustinGrote commented May 7, 2025

@iSazonov thank you for the review, I will incorporate the recently merged PR and your feedback hopefully sometime this week.

@JustinGrote JustinGrote requested a review from iSazonov May 8, 2025 18:18
@JustinGrote
Copy link
Contributor Author

@iSazonov feedback incorporated and it is now conditional using the new internal IsEnabled checks

@JustinGrote JustinGrote force-pushed the justingrote/issue25492 branch from 64271c7 to 42b6d6a Compare May 8, 2025 20:13
@JustinGrote
Copy link
Contributor Author

@iSazonov changes requested implemented (I went with a nested foreach for the headers to make that easier to read), and made CodeFactor happy. I also squash rebased to a single commit so this should be mergeable now. Let me know any further issues.

@JustinGrote JustinGrote force-pushed the justingrote/issue25492 branch from 42b6d6a to 53143d9 Compare May 8, 2025 20:14
@JustinGrote JustinGrote requested a review from iSazonov May 8, 2025 22:11
@JustinGrote JustinGrote force-pushed the justingrote/issue25492 branch 3 times, most recently from 0b31ad7 to d50453c Compare May 9, 2025 19:17
@JustinGrote
Copy link
Contributor Author

@iSazonov static refactor reverted. CodeFactor however is now complaining about complex methods I didn't touch. I tried a rebase/reset and confirmed I didn't touch those methods as far as I can tell. Any idea what's going on? Is it the nullable maybe?

@JustinGrote JustinGrote requested a review from iSazonov May 9, 2025 19:21
iSazonov
iSazonov previously approved these changes May 10, 2025
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Please update the PR description to follow latest changes.

@iSazonov iSazonov dismissed their stale review May 10, 2025 06:02

accidentally

@JustinGrote
Copy link
Contributor Author

@iSazonov incorporated feedback. Codefactor still complaining about complex methods I didn't touch.

@iSazonov
Copy link
Collaborator

@iSazonov incorporated feedback. Codefactor still complaining about complex methods I didn't touch.

@JustinGrote Please ignore such issues.

@JustinGrote JustinGrote requested a review from iSazonov May 10, 2025 18:34
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Code LGTM. Waiting community feedback...

@JustinGrote
Copy link
Contributor Author

I'll investigate the test failures.

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

I see request is disposed in failed tests.

@iSazonov iSazonov self-requested a review May 12, 2025 04:08
@JustinGrote
Copy link
Contributor Author

JustinGrote commented May 12, 2025

@iSazonov good catch, that was it. Wonder why it is disposed though? It was literally assigned just prior, it wasn't a scoped using or anything I can see. Also fixed an inconsistent Write method name

- Adds a clean single-line format for Verbose and a more detailed output for Debug
- Messages are optimized to only be generated if Verbose or Debug have been specified
- Byte lengths are humanized for better readability
- Intelligently decodes only non-binary request/response bodies
Closes PowerShell#25492
@JustinGrote JustinGrote force-pushed the justingrote/issue25492 branch from 60af40c to da5229c Compare May 12, 2025 16:51
@JustinGrote
Copy link
Contributor Author

@iSazonov I rebased to latest master and I think that made codefactor mad, you probably need to exclude whatever you did previously for the complex methods.

@iSazonov
Copy link
Collaborator

I rebased to latest master and I think that made codefactor mad, you probably need to exclude whatever you did previously for the complex methods.

Line 1416 currentRequest.Dispose();

@iSazonov
Copy link
Collaborator

@JustinGrote I see some tests still hang ...

@JustinGrote
Copy link
Contributor Author

@iSazonov I was having trouble getting my local testing to work specifically for webcmdlets last night but I will try again this evening (US Pacific time)

@JustinGrote
Copy link
Contributor Author

JustinGrote commented May 12, 2025

@iSazonov tests fixed (at least locally on my machine), the -Debug parameter was causing inquire and since Pester is run interactively, was waiting on the dialog, switched to using debugpreference.

Also adjusted encoding test to match on Codepage since we are providing it anyways, should be a much less brittle test.

@iSazonov
Copy link
Collaborator

@JustinGrote Still hang. I guess VerbosePreference is inquire.

@JustinGrote
Copy link
Contributor Author

Probably related to this, I couldn't get it to reproduce in Linux but let me do it in Windows.
image

All retries are done with recursive GetResponse added by PowerShell#19229.

The do loop was a leftover from the old way of doing things, it never actually gets triggered, and redirects previously had internal GetResponse that bypassed it, but when I unified the logic to unify the verbose messages, it now follows the flow "properly" and got stuck in the do/while instead of return the response, causing it to reuse the request and trigger an exception.
Fixes test "Invoke-WebRequest -PassThru -OutFile -Verbose File Name reflects the downloaded file name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WebRequest Cmdlets: Improve Verbose and Debug Logging Levels and include Request Content
2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/PowerShell/PowerShell/pull/25510

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy