Skip to content

Commit

Permalink
Add function support for enabling profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Redzko committed Jan 16, 2020
1 parent ddf40a0 commit ce6f6e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ If we were to apply the dynamic profile to the functions source module `another.
it has already been imported, no profiling would be triggered.


#### Custom Logic for Profiling

Sometimes you may want to dynamically control when the profiler runs. You can write your own logic for when to enable the profiler. To do this add the following to your `settings.py`:

This setting is mutually exclusive with SILKY_PYTHON_PROFILER and will be used over it if present. It will work with SILKY_DYNAMIC_PROFILING.

```python
def my_custom_logic(request):
return 'profile_requests' in request.session

SILKY_PYTHON_PROFILER_FUNC = my_custom_logic # profile only session has recording enabled.
```

You can also use a `lambda`.

```python
# profile only session has recording enabled.
SILKY_PYTHON_PROFILER_FUNC = lambda request: 'profile_requests' in request.session
```

### Code Generation

Silk currently generates two bits of code per request:
Expand Down Expand Up @@ -422,7 +442,7 @@ Note that in the above screenshot, this means that the request took 29ms (22ms f

### Recording a Fraction of Requests

On high-load sites it may be helpful to only record a fraction of the requests that are made.To do this add the following to your `settings.py`:
On high-load sites it may be helpful to only record a fraction of the requests that are made. To do this add the following to your `settings.py`:

Note: This setting is mutually exclusive with SILKY_INTERCEPT_FUNC.

Expand All @@ -432,7 +452,7 @@ SILKY_INTERCEPT_PERCENT = 50 # log only 50% of requests

#### Custom Logic for Recording Requests

On high-load sites it may also be helpful to write your own logic for when to intercept requests.To do this add the following to your `settings.py`:
On high-load sites it may also be helpful to write your own logic for when to intercept requests. To do this add the following to your `settings.py`:

Note: This setting is mutually exclusive with SILKY_INTERCEPT_PERCENT.

Expand Down
7 changes: 6 additions & 1 deletion silk/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ def configure(self, request=None):
self.request = request
self._configure()

if silky_config.SILKY_PYTHON_PROFILER:
if silky_config.SILKY_PYTHON_PROFILER_FUNC:
should_profile = silky_config.SILKY_PYTHON_PROFILER_FUNC(request)
else:
should_profile = silky_config.SILKY_PYTHON_PROFILER

if should_profile:
self.local.pythonprofiler = cProfile.Profile()
self.local.pythonprofiler.enable()

Expand Down
1 change: 1 addition & 0 deletions silk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SilkyConfig(metaclass=Singleton):
'SILKY_INTERCEPT_PERCENT': 100,
'SILKY_INTERCEPT_FUNC': None,
'SILKY_PYTHON_PROFILER': False,
'SILKY_PYTHON_PROFILER_FUNC': None,
'SILKY_STORAGE_CLASS': 'silk.storage.ProfilerResultStorage',
'SILKY_MIDDLEWARE_CLASS': 'silk.middleware.SilkyMiddleware'
}
Expand Down

0 comments on commit ce6f6e7

Please sign in to comment.