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

refactor: introduce http get utility; use it in mariner provider #376

Merged
merged 15 commits into from
Nov 1, 2023

Conversation

willmurphyscode
Copy link
Contributor

@willmurphyscode willmurphyscode commented Oct 30, 2023

Create a wrapper for requests.get at utils/http.py. The wrapper should always log the URL on failures, and should handle retries. Unit test the wrapper, and use it in the mariner provider.

Follow up PRs will introduce the wrapper into other providers.

See #310

Every time the application uses requests.get, issue a log message at
INFO level containing the requested URL.

Signed-off-by: Will Murphy <[email protected]>
@willmurphyscode willmurphyscode added the run-pr-quality-gate Triggers running of quality gate on PRs label Oct 30, 2023
Copy link
Contributor

@wagoodman wagoodman left a comment

Choose a reason for hiding this comment

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

There might be an opportunity for refactoring downloading files from a single util function that we can reuse everywhere. That way logging is done the same way, at the same level, can be stubbed out the same way, etc.

wagoodman

This comment was marked as outdated.

@wagoodman wagoodman dismissed their stale review October 30, 2023 20:21

based on an offline conversation, this seems like the right time to invest in the common util function to get default retry, logging, and raise behavior for the common requests.get()

@willmurphyscode willmurphyscode changed the title log network requests in more places refactor: always make http calls from single utility; unify logging and retrying Oct 31, 2023
@willmurphyscode willmurphyscode marked this pull request as draft October 31, 2023 14:57
@willmurphyscode willmurphyscode removed the run-pr-quality-gate Triggers running of quality gate on PRs label Nov 1, 2023
@willmurphyscode willmurphyscode added the run-pr-quality-gate Triggers running of quality gate on PRs label Nov 1, 2023
@willmurphyscode willmurphyscode changed the title refactor: always make http calls from single utility; unify logging and retrying refactor: introduce http get utility; use it in mariner provider Nov 1, 2023
Signed-off-by: Will Murphy <[email protected]>
@willmurphyscode willmurphyscode marked this pull request as ready for review November 1, 2023 14:46
timeout: int = DEFAULT_TIMEOUT,
**kwargs: Any,
) -> requests.Response:
logger.debug(f"attempting get request on {url}")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit/suggestion: since this will show up a lot, the smaller the better

Suggested change
logger.debug(f"attempting get request on {url}")
logger.debug(f"http GET {url}")

Comment on lines 42 to 45
if isinstance(last_exception, BaseException):
logger.error(f"last retry of GET {url} failed with {last_exception}")
raise last_exception
raise Exception("unreachable")
Copy link
Contributor

Choose a reason for hiding this comment

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

why not this?

Suggested change
if isinstance(last_exception, BaseException):
logger.error(f"last retry of GET {url} failed with {last_exception}")
raise last_exception
raise Exception("unreachable")
logger.error(f"last retry of GET {url} failed with {last_exception}")
raise last_exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had that at first, but the type of last_exception is Exception | None, and the static analysis tools can't tell that it's guaranteed not to be None by the time that line is reached, so I have to check that last_exception isn't None before returning it, or static analysis fails. I can change to if last_exception instead of the isinstance, but I haven't figured out how to not need this at all.

@@ -91,7 +91,7 @@ def disable_get_requests(monkeypatch):
def disabled(*args, **kwargs):
raise RuntimeError("requests disabled but HTTP GET attempted")

monkeypatch.setattr(parser.requests, "get", disabled)
monkeypatch.setattr(utils.http, "get", disabled)
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be something like this:

@pytest.fixture()
def disable_get_requests(monkeypatch):
    def disabled(*args, **kwargs):
        raise RuntimeError("requests disabled but HTTP GET attempted")
    from vunnel import utils
    monkeypatch.setattr(utils.http, "get", disabled)

and put into conftest.py generically for anyone to use

Copy link
Contributor

@wagoodman wagoodman left a comment

Choose a reason for hiding this comment

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

🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-pr-quality-gate Triggers running of quality gate on PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants