2020-08-11 01:47:54 +02:00
|
|
|
# Logging and error reporting
|
2016-05-05 01:16:31 +02:00
|
|
|
|
2017-09-24 18:43:30 +02:00
|
|
|
Having a good system for logging error reporting is essential to
|
2021-08-20 21:53:28 +02:00
|
|
|
making a large project like Zulip successful. Without reliable error
|
2017-09-24 18:43:30 +02:00
|
|
|
reporting, one has to rely solely on bug reports from users in order
|
|
|
|
to produce a working product.
|
2016-05-05 01:16:31 +02:00
|
|
|
|
2017-09-24 18:43:30 +02:00
|
|
|
Our goal as a project is to have zero known 500 errors on the backend
|
2021-08-20 21:53:28 +02:00
|
|
|
and zero known JavaScript exceptions on the frontend. While there
|
2017-09-24 18:43:30 +02:00
|
|
|
will always be new bugs being introduced, that goal is impossible
|
|
|
|
without an efficient and effective error reporting framework.
|
|
|
|
|
2021-05-06 04:08:57 +02:00
|
|
|
We provide integration with [Sentry][sentry] to make it easier for
|
|
|
|
very large installations like zulip.com to manage their exceptions and
|
|
|
|
ensure they are all tracked down, but our default email-based system
|
|
|
|
is great for small installations.
|
2017-09-24 18:43:30 +02:00
|
|
|
|
|
|
|
## Backend error reporting
|
|
|
|
|
|
|
|
The [Django][django-errors] framework provides much of the
|
|
|
|
infrastructure needed by our error reporting system:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- The ability to send emails to the server's administrators with any
|
2021-08-20 21:53:28 +02:00
|
|
|
500 errors, using the `mail_admins` function. We enhance these data
|
2017-09-24 18:43:30 +02:00
|
|
|
with extra details (like what user was involved in the error) in
|
|
|
|
`zerver/logging_handlers.py`, and then send them to the
|
|
|
|
administrator in `zerver/lib/error_notify.py` (which also supports
|
|
|
|
sending Zulips to a stream about production errors).
|
2021-08-20 21:45:39 +02:00
|
|
|
- The ability to rate-limit certain errors to avoid sending hundreds
|
2017-09-24 18:43:30 +02:00
|
|
|
of emails in an outage (see `_RateLimitFilter` in
|
|
|
|
`zerver/lib/logging_util.py`)
|
2021-08-20 21:45:39 +02:00
|
|
|
- A nice framework for filtering passwords and other important user
|
2017-09-24 18:43:30 +02:00
|
|
|
data from the exception details, which we use in
|
|
|
|
`zerver/filters.py`.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Middleware for handling `JsonableError`, which is our standard
|
2021-06-30 18:46:10 +02:00
|
|
|
system for API code to return a JSON-format HTTP error response.
|
2017-09-24 18:43:30 +02:00
|
|
|
|
|
|
|
Since 500 errors in any Zulip server are usually a problem the server
|
|
|
|
administrator should investigate and/or report upstream, we have this
|
|
|
|
email reporting system configured to report errors by default.
|
|
|
|
|
2021-05-06 04:08:57 +02:00
|
|
|
Zulip's optional [Sentry][sentry] integration will aggregate errors to
|
|
|
|
show which users and realms are affected, any logging which happened
|
|
|
|
prior to the exception, local variables in each frame of the
|
|
|
|
exception, and the full request headers which triggered it.
|
|
|
|
|
2017-09-24 18:43:30 +02:00
|
|
|
### Backend logging
|
|
|
|
|
|
|
|
[Django's logging system][django-logging] uses the standard
|
2021-08-20 21:53:28 +02:00
|
|
|
[Python logging infrastructure][python-logging]. We have configured
|
2017-09-24 18:43:30 +02:00
|
|
|
them so that `logging.exception` and `logging.error` get emailed to
|
|
|
|
the server maintainer, while `logging.warning` will just appear in
|
2021-08-20 21:53:28 +02:00
|
|
|
`/var/log/zulip/errors.log`. Lower log levels just appear in the main
|
2017-09-24 18:43:30 +02:00
|
|
|
server log (as well as in the log for corresponding process, be it
|
|
|
|
`django.log` for the main Django processes or the appropriate
|
|
|
|
`events_*` log file for a queue worker).
|
|
|
|
|
|
|
|
#### Backend logging format
|
|
|
|
|
|
|
|
The main Zulip server log contains a line for each backend request.
|
|
|
|
It also contains warnings, errors, and the full tracebacks for any
|
2021-08-20 21:53:28 +02:00
|
|
|
Python exceptions. In production, it goes to
|
2017-09-24 18:43:30 +02:00
|
|
|
`/var/log/zulip/server.log`; in development, it goes to the terminal
|
|
|
|
where you run `run-dev.py`.
|
|
|
|
|
|
|
|
In development, it's good to keep an eye on the `run-dev.py` console
|
|
|
|
as you work on backend changes, since it's a great way to notice bugs
|
|
|
|
you just introduced.
|
|
|
|
|
|
|
|
In production, one usually wants to look at `errors.log` for errors
|
|
|
|
since the main server log can be very verbose, but the main server log
|
|
|
|
can be extremely valuable for investigating performance problems.
|
2016-05-05 01:16:31 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```text
|
2020-03-24 12:45:29 +01:00
|
|
|
2016-05-20 14:50:22.056 INFO [zr] 127.0.0.1 GET 302 528ms (db: 1ms/1q) (+start: 123ms) / (unauth@zulip via ?)
|
2016-05-05 01:16:31 +02:00
|
|
|
[20/May/2016 14:50:22]"GET / HTTP/1.0" 302 0
|
2020-03-24 12:45:29 +01:00
|
|
|
2016-05-20 14:50:22.272 INFO [zr] 127.0.0.1 GET 200 124ms (db: 3ms/2q) /login/ (unauth@zulip via ?)
|
|
|
|
2016-05-20 14:50:26.333 INFO [zr] 127.0.0.1 POST 302 37ms (db: 6ms/7q) /accounts/login/local/ (unauth@zulip via ?)
|
2016-05-05 01:16:31 +02:00
|
|
|
[20/May/2016 14:50:26]"POST /accounts/login/local/ HTTP/1.0" 302 0
|
2020-03-24 12:45:29 +01:00
|
|
|
2016-05-20 14:50:26.538 INFO [zr] 127.0.0.1 POST 200 12ms (db: 1ms/2q) (+start: 53ms) /api/v1/events/internal [1463769771:0/0] (8@zulip via internal)
|
|
|
|
2016-05-20 14:50:26.657 INFO [zr] 127.0.0.1 POST 200 10ms (+start: 8ms) /api/v1/events/internal [1463769771:0/0] (8@zulip via internal)
|
|
|
|
2016-05-20 14:50:26.959 INFO [zr] 127.0.0.1 GET 200 588ms (db: 26ms/21q) / [1463769771:0] (8@zulip via website)
|
2016-05-05 01:16:31 +02:00
|
|
|
```
|
|
|
|
|
2017-09-24 18:43:30 +02:00
|
|
|
The format of this output is:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Timestamp
|
|
|
|
- Log level
|
|
|
|
- Logger name, abbreviated as "zr" for these Zulip request logs
|
|
|
|
- IP address
|
|
|
|
- HTTP method
|
|
|
|
- HTTP status code
|
|
|
|
- Time to process
|
|
|
|
- (Optional perf data details, e.g. database time/queries, memcached
|
2021-08-20 22:54:08 +02:00
|
|
|
time/queries, Django process startup time, Markdown processing time,
|
|
|
|
etc.)
|
2021-08-20 21:45:39 +02:00
|
|
|
- Endpoint/URL from zproject/urls.py
|
|
|
|
- "email via client" showing user account involved (if logged in) and
|
2021-08-20 22:54:08 +02:00
|
|
|
the type of client they used ("web", "Android", etc.).
|
2017-09-24 18:43:30 +02:00
|
|
|
|
|
|
|
The performance data details are particularly useful for investigating
|
|
|
|
performance problems, since one can see at a glance whether a slow
|
2020-08-11 01:47:49 +02:00
|
|
|
request was caused by delays in the database, in the Markdown
|
2017-09-24 18:43:30 +02:00
|
|
|
processor, in memcached, or in other Python code.
|
|
|
|
|
|
|
|
One useful thing to note, however, is that the database time is only
|
|
|
|
the time spent connecting to and receiving a response from the
|
2021-08-20 21:53:28 +02:00
|
|
|
database. Especially when response are large, there can often be a
|
2017-09-24 18:43:30 +02:00
|
|
|
great deal of Python processing overhead to marshall the data from the
|
|
|
|
database into Django objects that is not accounted for in these
|
|
|
|
numbers.
|
|
|
|
|
|
|
|
## Blueslip frontend error reporting
|
|
|
|
|
|
|
|
We have a custom library, called `blueslip` (named after the form used
|
|
|
|
at MIT to report problems with the facilities), that takes care of
|
2021-08-20 21:53:28 +02:00
|
|
|
reporting JavaScript errors. In production, this means emailing the
|
2017-09-24 18:43:30 +02:00
|
|
|
server administrators (though the setting controlling this,
|
|
|
|
`BROWSER_ERROR_REPORTING`, is disabled by default, since most problems
|
|
|
|
are unlikely to be addressable by a system administrator, and it's
|
|
|
|
very hard to make JavaScript errors not at least somewhat spammy due
|
|
|
|
to the variety of browser versions and sets of extensions that someone
|
2021-08-20 21:53:28 +02:00
|
|
|
might use). In development, this means displaying a highly visible
|
2017-09-24 18:43:30 +02:00
|
|
|
overlay over the message view area, to make exceptions in testing a
|
|
|
|
new feature hard to miss.
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Blueslip is implemented in `static/js/blueslip.js`.
|
|
|
|
- In order to capture essentially any error occurring in the browser,
|
2020-04-19 07:37:22 +02:00
|
|
|
Blueslip listens for the `error` event on `window`, and has methods
|
|
|
|
for being manually triggered by Zulip JavaScript code for warnings
|
|
|
|
and assertion failures.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Blueslip keeps a log of all the notices it has received during a
|
2017-09-24 18:43:30 +02:00
|
|
|
browser session, and includes them in reports to the server, so that
|
2021-08-20 21:53:28 +02:00
|
|
|
one can see cases where exceptions chained together. You can print
|
2021-09-08 00:23:24 +02:00
|
|
|
this log from the browser console using
|
|
|
|
`blueslip = require("./static/js/blueslip"); blueslip.get_log()`.
|
2017-09-24 18:43:30 +02:00
|
|
|
|
|
|
|
Blueslip supports several error levels:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- `throw new Error(…)`: For fatal errors that cannot be easily
|
2021-08-20 21:53:28 +02:00
|
|
|
recovered from. We try to avoid using it, since it kills the
|
2020-09-24 07:56:29 +02:00
|
|
|
current JS thread, rather than returning execution to the caller.
|
2021-08-20 21:45:39 +02:00
|
|
|
- `blueslip.error`: For logging of events that are definitely caused
|
2017-09-24 18:43:30 +02:00
|
|
|
by a bug and thus sufficiently important to be reported, but where
|
|
|
|
we can handle the error without creating major user-facing problems
|
|
|
|
(e.g. an exception when handling a presence update).
|
2021-08-20 21:45:39 +02:00
|
|
|
- `blueslip.warn`: For logging of events that are a problem but not
|
2021-08-20 21:53:28 +02:00
|
|
|
important enough to send an email about in production. They are,
|
2017-09-24 18:43:30 +02:00
|
|
|
however, highlighted in the JS console in development.
|
2021-08-20 21:45:39 +02:00
|
|
|
- `blueslip.log` (and `blueslip.info`): Logged to the JS console in
|
2021-08-20 21:53:28 +02:00
|
|
|
development and also in the blueslip log in production. Useful for
|
2017-09-24 18:43:30 +02:00
|
|
|
data that might help discern what state the browser was in during an
|
|
|
|
error (e.g. whether the user was in a narrow).
|
2021-08-20 21:45:39 +02:00
|
|
|
- `blueslip.debug`: Similar to `blueslip.log`, but are not printed to
|
2017-09-24 18:43:30 +02:00
|
|
|
the JS console in development.
|
|
|
|
|
2017-09-24 18:58:51 +02:00
|
|
|
## Frontend performance reporting
|
|
|
|
|
|
|
|
In order to make it easier to debug potential performance problems in
|
|
|
|
the critically latency-sensitive message sending code pathway, we log
|
|
|
|
and report to the server the following whenever a message is sent:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- The time the user triggered the message (aka the start time).
|
|
|
|
- The time the `send_message` response returned from the server.
|
|
|
|
- The time the message was received by the browser from the
|
2017-09-24 18:58:51 +02:00
|
|
|
`get_events` protocol (these last two race with each other).
|
2021-08-20 21:45:39 +02:00
|
|
|
- Whether the message was locally echoed.
|
|
|
|
- If so, whether there was a disparity between the echoed content and
|
2017-09-24 18:58:51 +02:00
|
|
|
the server-rendered content, which can be used for statistics on how
|
2019-09-30 19:37:56 +02:00
|
|
|
effective our [local echo system](../subsystems/markdown.md) is.
|
2017-09-24 18:58:51 +02:00
|
|
|
|
|
|
|
The code is all in `zerver/lib/report.py` and `static/js/sent_messages.js`.
|
|
|
|
|
|
|
|
We have similar reporting for the time it takes to narrow / switch to
|
|
|
|
a new view:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- The time the action was initiated
|
|
|
|
- The time when the updated message feed was visible to the user
|
|
|
|
- The time when the browser was idle again after switching views
|
2017-09-24 18:58:51 +02:00
|
|
|
(intended to catch issues where we generate a lot of deferred work).
|
|
|
|
|
2021-11-05 20:09:57 +01:00
|
|
|
[django-errors]: https://docs.djangoproject.com/en/3.2/howto/error-reporting/
|
2017-09-24 18:43:30 +02:00
|
|
|
[python-logging]: https://docs.python.org/3/library/logging.html
|
2021-11-05 20:09:57 +01:00
|
|
|
[django-logging]: https://docs.djangoproject.com/en/3.2/topics/logging/
|
2017-09-24 18:43:30 +02:00
|
|
|
[sentry]: https://sentry.io
|