summaryrefslogtreecommitdiff
path: root/docs/advanced.rst
blob: 5f8951523921c5ced45a9ffd5355d03d82611aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
Advanced Usage
==============

This covers some advanced usage scenarios for raven Python.

Alternative Installations
-------------------------

If you want to use the latest git version you can get it from `the github
repository <https://github.com/getsentry/raven-python>`_::

    git clone https://github.com/getsentry/raven-python
    pip install raven-python

Certain additional features can be installed by defining the feature when
``pip`` installing it.  For instance to install all dependencies needed to
use the Flask integration, you can depend on ``raven[flask]``::

    pip install raven[flask]

For more information refer to the individual integration documentation.

.. _python-client-config:

Configuring the Client
----------------------

Settings are specified as part of the initialization of the client.  The
client is a class that can be instantiated with a specific configuration
and all reporting can then happen from the instance of that object.
Typically an instance is created somewhere globally and then imported as
necessary.

.. code-block:: python

    from raven import Client

    # Read configuration from the ``SENTRY_DSN`` environment variable
    client = Client()

    # Manually specify a DSN
    client = Client('___DSN___')


A reasonably configured client should generally include a few additional
settings:

.. code-block:: python

    import os
    import raven

    client = raven.Client(
        dsn='___DSN___',

        # inform the client which parts of code are yours
        # include_paths=['my.app']
        include_paths=[__name__.split('.', 1)[0]],

        # pass along the version of your application
        # release='1.0.0'
        # release=raven.fetch_package_version('my-app')
        release=raven.fetch_git_sha(os.path.dirname(__file__)),
    )

.. versionadded:: 5.2.0
   The *fetch_package_version* and *fetch_git_sha* helpers.


Client Arguments
----------------

The following are valid arguments which may be passed to the Raven client:

.. describe:: dsn

    A Sentry compatible DSN as mentioned before::

        dsn = '___DSN___'

.. describe:: transport

    The HTTP transport class to use. By default this is an asynchronous worker
    thread that runs in-process.

    For more information see :doc:`transports`.

.. describe:: site

    An optional, arbitrary string to identify this client installation::

        site = 'my site name'

.. describe:: name

    This will override the ``server_name`` value for this installation.
    Defaults to ``socket.gethostname()``::

        name = 'sentry_rocks_' + socket.gethostname()

.. describe:: release

    The version of your application. This will map up into a Release in
    Sentry::

        release = '1.0.3'

.. describe:: environment

    The environment your application is running in::

        environment = 'staging'

.. describe:: tags

    Default tags to send with events::

        tags = {'site': 'foo.com'}

.. describe:: repos

    This describes local repositories that are reflected in your source code::

        repos = {
            'raven': {
                # the name of the repository as registered in Sentry
                'name': 'getsentry/raven-python',
                # the prefix where the local source code is found in the repo
                'prefix': 'src',
            }
        }

    The repository key can either be a module name or the absolute path. When
    a module name is given it will be automatically converted to its absolute path.

    For more information, see the :doc:`repos interface <../../../clientdev/interfaces/repos>`
    docs.

.. describe:: exclude_paths

    Extending this allow you to ignore module prefixes when we attempt to
    discover which function an error comes from (typically a view)::

        exclude_paths = [
            'django',
            'sentry',
            'raven',
            'lxml.objectify',
        ]

.. describe:: include_paths

    For example, in Django this defaults to your list of ``INSTALLED_APPS``,
    and is used for drilling down where an exception is located::

        include_paths = [
            'django',
            'sentry',
            'raven',
            'lxml.objectify',
        ]

.. describe:: ignore_exceptions

    A list of exceptions to ignore::

        ignore_exceptions = [
            'Http404',
            'django.exceptions.http.Http404',
            'django.exceptions.*',
            ValueError,
        ]

    Each item can be either a string or a class.
    String declaration is strict (ie. does not work for child exceptions)
    whereas class declaration handle inheritance (ie. child exceptions are also ignored).

.. describe:: sample_rate

    The sampling factor to apply to events. A value of 0.00 will deny sending
    any events, and a value of 1.00 will send 100% of events.

    .. code-block:: python

        # send 50% of events
        sample_rate = 0.5

.. describe:: list_max_length

    The maximum number of items a list-like container should store.

    If an iterable is longer than the specified length, the left-most
    elements up to length will be kept.

    .. note:: This affects sets as well, which are unordered.

    ::

        list_max_length = 50

.. describe:: string_max_length

    The maximum characters of a string that should be stored.

    If a string is longer than the given length, it will be truncated down
    to the specified size::

        string_max_length = 200

.. describe:: auto_log_stacks

    Should Raven automatically log frame stacks (including locals) for all
    calls as it would for exceptions::

        auto_log_stacks = True

.. describe:: processors

    A list of processors to apply to events before sending them to the
    Sentry server. Useful for sending additional global state data or
    sanitizing data that you want to keep off of the server::

        processors = (
            'raven.processors.SanitizePasswordsProcessor',
        )

Sanitizing Data
---------------

Several processors are included with Raven to assist in data
sanitiziation. These are configured with the ``processors`` value.

.. describe:: raven.processors.SanitizePasswordsProcessor

   Removes all keys which resemble ``password``, ``secret``, or
   ``api_key`` within stacktrace contexts, HTTP bits (such as cookies,
   POST data, the querystring, and environment), and extra data.

.. describe:: raven.processors.RemoveStackLocalsProcessor

   Removes all stacktrace context variables. This will cripple the
   functionality of Sentry, as you'll only get raw tracebacks, but it will
   ensure no local scoped information is available to the server.

.. describe:: raven.processors.RemovePostDataProcessor

   Removes the ``body`` of all HTTP data.

Custom Grouping Behavior
------------------------

In some cases you may see issues where Sentry groups multiple events together
when they should be separate entities. In other cases, Sentry simply doesn't
group events together because they're so sporadic that they never look the same.

Both of these problems can be addressed by specifying the ``fingerprint``
attribute.

For example, if you have HTTP 404 (page not found) errors, and you'd prefer they
deduplicate by taking into account the URL:

.. code-block:: python

    client.captureException(fingerprint=['{{ default }}', 'http://my-url/'])

.. sentry:edition:: hosted, on-premise

    For more information, see :ref:`custom-grouping`.

Sampling Messages
-----------------

There are two ways to sample messages:

- Add sample_rate to the Client object - This sends a percentage of messages the reaching the Client to Sentry

.. code-block:: python

    client = Client('___DSN___', sample_rate=0.5) # send 50% of events

- Sample individual messages

.. code-block:: python

    client = Client('___DSN___') # No sample_rate provided

    try:
        1 / 0
    except ZeroDivisionError:
        client.captureException(sample_rate=0.5) # Send 50% of this event

Alternatively, if you have SentryHandler configured in your logging stack,
you can send ``sample_rate`` in the ``extra`` kwarg in each log like this

.. code-block:: python

    some_logger.warning('foo', extra={'sample_rate': 0.5}) # Send 50% of this event

A Note on uWSGI
---------------

If you're using uWSGI you will need to add ``enable-threads`` to the
default invocation, or you will need to switch off of the threaded default
transport.