selenium
selenium copied to clipboard
[dotnet] Add Execution Context
User description
Description
Added Context to support and fix BUG https://github.com/SeleniumHQ/selenium/issues/13920
Motivation and Context
I have encountered the same issue and wanting to debug it more but unfortunately there's not enough error logs to let me debug it
Types of changes
- [x] 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 change)
Checklist
- [x] I have read the contributing document.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [x] All new and existing tests passed.
PR Type
Bug fix, Enhancement
Description
- Added detailed logging for HTTP request and response bodies in
HttpCommandExecutor. - Enhanced trace logging to include the content of HTTP requests and responses.
- Aimed to improve debugging capabilities by providing more comprehensive error logs.
Changes walkthrough 📝
| Relevant files | |||
|---|---|---|---|
| Enhancement |
|
💡 PR-Agent usage: Comment
/helpon the PR to get a list of all available PR-Agent tools and their descriptions
PR Description updated to latest commit (https://github.com/SeleniumHQ/selenium/commit/a6c82f6496a712131caae8d5935985084619d1aa)
- [ ] Copy walkthrough table to "Files Changed" Tab
PR Review 🔍
| ⏱️ Estimated effort to review [1-5] |
2, because the changes are localized to a single file and involve adding logging functionality which is straightforward. The logic is not complex, and the changes are well-contained. |
| 🧪 Relevant tests |
No |
| ⚡ Possible issues |
Performance Concern: The addition of logging for HTTP request and response bodies could potentially lead to performance degradation, especially if the content size is large. This might slow down the execution context significantly. |
|
Sensitive Data Exposure: Logging HTTP response and request bodies might inadvertently expose sensitive data in the logs, which could be a security risk if logs are not properly secured or if sensitive data is not adequately masked. | |
| 🔒 Security concerns |
Sensitive Data Exposure: The new logging functionality could potentially expose sensitive information contained in HTTP requests or responses if not properly handled. This includes personal data, authentication tokens, or other sensitive information that might be part of the HTTP body. |
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Possible bug |
✅ Add a null check for responseMessage.Content before reading the response bodySuggestion Impact:The suggestion was implemented by adding a null check for responseMessage.Content before attempting to read the response body and logging it.code diff:
Consider adding a null check for dotnet/src/webdriver/Remote/HttpCommandExecutor.cs [301-302]
Suggestion importance[1-10]: 10Why: This suggestion correctly identifies a potential | 10 |
| Performance |
✅ Use ConfigureAwait(false) when awaiting ReadAsStringAsync calls to avoid potential performance issuesSuggestion Impact:The suggestion to use ConfigureAwait(false) when awaiting ReadAsStringAsync calls was implemented in multiple places in the commit.code diff:
To avoid potential performance issues, consider using dotnet/src/webdriver/Remote/HttpCommandExecutor.cs [291-292]
Suggestion importance[1-10]: 8Why: Using | 8 |
| Maintainability |
✅ Extract the logging logic into a separate method for better readability and maintainabilitySuggestion Impact:The logging logic for HTTP requests and responses was extracted into a separate method within a new DiagnosticsHttpHandler class, improving readability and maintainability.code diff:
To improve readability and maintainability, consider extracting the logging logic into a dotnet/src/webdriver/Remote/HttpCommandExecutor.cs [286-294]
Suggestion importance[1-10]: 7Why: Extracting the logging logic into a separate method improves readability and maintainability, but it is not a critical change. The suggestion correctly identifies the relevant code block. | 7 |
| Best practice |
✅ Remove the unnecessary blank line at the end of the methodSuggestion Impact:The unnecessary blank line at the end of the method was removed, keeping the code clean and consistent.code diff:
Remove the unnecessary blank line at the end of the method to keep the code clean and dotnet/src/webdriver/Remote/HttpCommandExecutor.cs [314-315]
Suggestion importance[1-10]: 4Why: While removing unnecessary blank lines can improve code cleanliness, it is a minor stylistic improvement and not crucial to the functionality or readability of the code. | 4 |
@SeleniumHQ/selenium-committers should we include http request/response body of internal wire protocol to internal logs?
@SeleniumHQ/selenium-committers should we include http request/response body of internal wire protocol to internal logs?
Hi @nvborisenko I'll wait for everyone's opinion on whether we should include HTTP request/response bodies in internal logs. Let's gather everyone's thoughts about this then later on we can decide whether to include it or not. Thanks for looking up
Hey! Thank you for your contribution. However, a typical concern with logging HTTP responses is the body size. For example, the response size might be huge when fetching the web page source. It might be a good idea to log the HTTP response body if the status code is not 2xx. Basically, logging in detail on error situations only.
Hey! Thank you for your contribution. However, a typical concern with logging HTTP responses is the body size. For example, the response size might be huge when fetching the web page source. It might be a good idea to log the HTTP response body if the status code is not 2xx. Basically, logging in detail on error situations only.
That's a great valid point. I will take it into consideration and update the PR accordingly. Thanks @pujagani
Hey! Thank you for your contribution. However, a typical concern with logging HTTP responses is the body size. For example, the response size might be huge when fetching the web page source. It might be a good idea to log the HTTP response body if the status code is not 2xx. Basically, logging in detail on error situations only.
That's a great valid point. I will take it into consideration and update the PR accordingly. Thanks @pujagani
Hey! Thank you for your contribution. However, a typical concern with logging HTTP responses is the body size. For example, the response size might be huge when fetching the web page source. It might be a good idea to log the HTTP response body if the status code is not 2xx. Basically, logging in detail on error situations only.
Hi @pujagani updated the PR. Please see the changes. Thank you!
LGTM. Thank you! @nvborisenko is the C# expert. Please help review it further.
Stop reviewing it please, this code even cannot be compiled. Does anybody know what is IHttpInterceptor? When I mention http requests delegating I mean https://learn.microsoft.com/en-us/dotnet/api/system.net.http.delegatinghandler?view=net-8.0
@nvborisenko I see that there's some attributes and files are being ignored and not being pushed this is what's causing the error in the build. I will try another work around for my solution.
UPDATE:
- Apologies for the continuous build error now, I have updated my PR to handle https://learn.microsoft.com/en-us/dotnet/api/system.net.http.delegatinghandler?view=net-8.0. Thanks
Any update with this :) ?
Thanks, now it is something closer to be reviewed.
@nvborisenko Fix and consider all the suggestions and comments.
Please see the latest commit
@ChrstnAnsl this is exactly what we wanted to see and can accept, thank you! Please just fix minor issues.
BTW do you want to improve the logic here little bit? Now in logs we will see 2 log entries for http request: the first one is like >> POST to https://url ... and the second one is request body content. What if we can merge it and put http request details as a single log entry like:
POST to https://url ...
{
...
}
where { ... } is request body content.
Hi @nvborisenko adjusted the logic and fix the last minor comment. Please see the latest commit. Thank you
Let me help here slightly, I am going to push new commits if you don't mind.
Sure, go ahead! Thanks
I am done, could you please review?
Hi @nvborisenko LGTM!
In logs we see:
10:36:40.134 TRACE HttpCommandExecutor: >> Method: POST, RequestUri: 'http://localhost:37361/session/e44b09c547486f5ea4c198895969e659/url', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers:
{
Accept: application/json; charset=utf-8
User-Agent: selenium/4.23.0-nightly202406201609
User-Agent: (.net linux)
Content-Type: application/json; charset=utf-8
}
{"url":"http://localhost:43323/common/temp/page7006338836466627400.html"}
10:36:40.197 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Cache-Control: no-cache
Content-Length: 14
Content-Type: application/json; charset=utf-8
}
Looks good to me.