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

Added the use of Lambdas in settings.py to the README. #77

Merged
merged 2 commits into from
Sep 20, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Silk is a live profiling and inspection tool for the Django framework. Silk inte

* [Requirements](#requirements)
* [Installation](#installation)
* [Features](#features)
* [Features](#features)
* [Configuration](#configuration)
* [Authentication/Authorisation](#authentication-authorisation)
* [Request/Response bodies](#request-response-bodies)
Expand All @@ -38,7 +38,7 @@ pip install django-silk
In `settings.py` add the following:

```python
MIDDLEWARE_CLASSES = (
MIDDLEWARE_CLASSES = (
...
'silk.middleware.SilkyMiddleware',
...
Expand Down Expand Up @@ -90,7 +90,7 @@ Silk primarily consists of:

* Middleware for intercepting Requests/Responses
* A wrapper around SQL execution for profiling of database queries
* A context manager/decorator for profiling blocks of code and functions either manually or dynamically.
* A context manager/decorator for profiling blocks of code and functions either manually or dynamically.
* A user interface for inspection and visualisation of the above.

### Request Inspection
Expand All @@ -116,7 +116,7 @@ Further details on each request are also available by clicking the relevant requ

### SQL Inspection

Silk also intercepts SQL queries that are generated by each request. We can get a summary on things like
Silk also intercepts SQL queries that are generated by each request. We can get a summary on things like
the tables involved, number of joins and execution time:

<img src="https://raw.githubusercontent.com/mtford90/silk/master/screenshots/3.png" width="720px"/>
Expand All @@ -125,7 +125,7 @@ Before diving into the stack trace to figure out where this request is coming fr

<img src="https://raw.githubusercontent.com/mtford90/silk/master/screenshots/5.png" width="720px"/>

### Profiling
### Profiling

Turn on the SILKY_PYTHON_PROFILER setting to use Python's built-in cProfile profiler. Each request will be separately profiled and the profiler's output will be available on the request's Profiling page in the Silk UI.

Expand All @@ -134,7 +134,7 @@ SILKY_PYTHON_PROFILER = True
```

Silk can also be used to profile specific blocks of code/functions. It provides a decorator and a context
manager for this purpose.
manager for this purpose.

For example:

Expand Down Expand Up @@ -167,27 +167,27 @@ def post(request, post_id):
'post': p
})

class MyView(View):
@silk_profile(name='View Blog Post')
def get(self, request):
p = Post.objects.get(pk=post_id)
return render_to_response('post.html', {
'post': p
})
class MyView(View):
@silk_profile(name='View Blog Post')
def get(self, request):
p = Post.objects.get(pk=post_id)
return render_to_response('post.html', {
'post': p
})
```

#### Context Manager

Using a context manager means we can add additional context to the name which can be useful for
Using a context manager means we can add additional context to the name which can be useful for
narrowing down slowness to particular database records.

```
def post(request, post_id):
with silk_profile(name='View Blog Post #%d' % self.pk):
p = Post.objects.get(pk=post_id)
return render_to_response('post.html', {
'post': p
})
return render_to_response('post.html', {
'post': p
})
```

#### Dynamic Profiling
Expand Down Expand Up @@ -264,7 +264,7 @@ def foo():
print (2)
print(3)
print(4)
```
```

Note that dynamic profiling behaves in a similar fashion to that of the python mock framework in that
we modify the function in-place e.g:
Expand Down Expand Up @@ -303,7 +303,7 @@ Both are intended for use in replaying the request. The curl command can be used

### Authentication/Authorisation

By default anybody can access the Silk user interface by heading to `/silk/`. To enable your Django
By default anybody can access the Silk user interface by heading to `/silk/`. To enable your Django
auth backend place the following in `settings.py`:

```python
Expand All @@ -322,6 +322,12 @@ def my_custom_perms(user):
SILKY_PERMISSIONS = my_custom_perms
```

You can also use a `lambda`.

```python
SILKY_PERMISSIONS = lambda user: user.is_superuser
```

### Request/Response bodies

By default, Silk will save down the request and response bodies for each request for future viewing
Expand All @@ -342,7 +348,7 @@ the following to your `settings.py`:
SILKY_META = True
```

Silk will then record how long it takes to save everything down to the database at the end of each
Silk will then record how long it takes to save everything down to the database at the end of each
request:

<img src="https://raw.githubusercontent.com/mtford90/silk/master/screenshots/meta.png"/>
Expand All @@ -366,13 +372,19 @@ On high-load sites it may also be helpful to write your own logic for when to in
Note: This setting is mutually exclusive with SILKY_INTERCEPT_PERCENT.

```python

def my_custom_logic(request):
return 'record_requests' in request.session

SILKY_INTERCEPT_FUNC = my_custom_logic # log only session has recording enabled.
```

You can also use a `lambda`.

```python
# log only session has recording enabled.
SILKY_INTERCEPT_FUNC = lambda request: 'record_requests' in request.session
```

### Clearing logged data

A management command will wipe out all logged data:
Expand Down