-
Notifications
You must be signed in to change notification settings - Fork 33
Profiling middleware #1228
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
LDiazN
wants to merge
8
commits into
master
Choose a base branch
from
profiling-middleware
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Profiling middleware #1228
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
deedbe9
Add profiling library and settings to ooniprobe
LDiazN 737c808
Add initial version of profiling middleware
LDiazN f7b678b
Add whitelisting to profiling
LDiazN aa6012b
Add profiling to ooniprobe
LDiazN 00da337
Fix bad arg name
LDiazN a7b3887
fix variable name bug
LDiazN 4ba4002
Add tests for profiling middleware
LDiazN 7804232
Add profiling middleware tests
LDiazN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| from starlette.middleware.base import BaseHTTPMiddleware | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
| from pathlib import Path | ||
| import logging | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| class ProfileMiddleware(BaseHTTPMiddleware): | ||
| """ | ||
| Profiles a request, generating an html report on disk | ||
| """ | ||
|
|
||
| def __init__(self, app, profiling_active : bool, report_path : str, whitelist: tuple[str]): | ||
| """ | ||
| - profiling_active: whether profiling is enabled | ||
| - report_path: local disk path where the report is written | ||
| - whitelist: path prefixes to profile (only matching requests are profiled) | ||
| """ | ||
| super().__init__(app) | ||
| self.profiling_active = profiling_active | ||
| self.report_path = report_path | ||
| self.whitelist = whitelist | ||
|
|
||
| async def dispatch(self, request: Request, call_next) -> Response: | ||
|
|
||
| if not self.profiling_active or not self.should_profile(request): | ||
| return await call_next(request) | ||
|
|
||
| # Pyinstrument is only available on development modes | ||
| from pyinstrument import Profiler | ||
|
|
||
| log.debug(f"Profiling: {request.url.path}") | ||
|
|
||
| profiler = Profiler() | ||
| profiler.start() | ||
| response = await call_next(request) | ||
| profiler.stop() | ||
|
|
||
| # Save report to a file | ||
| report = profiler.output_html() | ||
| report_path = Path(self.report_path) | ||
| report_path.parent.mkdir(exist_ok=True) | ||
| report_path.touch(exist_ok=True) | ||
|
|
||
| with report_path.open("w") as f: | ||
| f.write(report) | ||
| log.debug(f"Report saved to: {report_path.absolute()}") | ||
|
|
||
| return response | ||
|
|
||
| def should_profile(self, request: Request) -> bool: | ||
| return request.url.path.startswith(self.whitelist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,7 @@ dependencies = [ | |
| "pytest-asyncio", | ||
| "freezegun", | ||
| "pytest-docker", | ||
| "pyinstrument" | ||
| ] | ||
| path = ".venv/" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ooniapi/services/ooniprobe/tests/test_profiling_middleware.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from .utils import getj | ||
| import pytest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_profiling_enabled(client, db, tmp_path, profiling_enabled): | ||
| report_path: Path = tmp_path / "report.html" | ||
|
|
||
| assert not report_path.exists() | ||
|
|
||
| getj(client, "/api/v1/manifest") | ||
|
|
||
| assert report_path.exists() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_profiling_disabled(client, db, tmp_path, profiling_disabled): | ||
| report_path: Path = tmp_path / "report.html" | ||
|
|
||
| assert not report_path.exists() | ||
|
|
||
| getj(client, "/api/v1/manifest") | ||
|
|
||
| assert not report_path.exists() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this impact how the s3 reprocessor will handle files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, actually the
tnvariable didn't even exist. It was a silent bug that hasn't triggered bc the fastpath has been quite stable lately.test_nameis the new name of the same variable