We are now, simply, Redis
Now we have our statistics stored in Redis—what next? More specifically, now that we
have information about (for example) access time on every page, how do we discover
which pages take a long time on average to generate? Or how do we know when it
takes significantly longer to generate a page than it did on previous occasions? The
simple answer is that we need to store more information in a way that lets us discover
when both situations happen, which we’ll explore in this section.
If we want to record access times, then we need to calculate access times. We can
spend our time adding access time calculations in various places and then adding
code to record the access times, or we can implement something to help us to calculate
and record the access times. That same helper could then also make that information
available in (for example) a ZSET of the slowest pages to access on average, and
could even report on pages that take a long time to access compared to other times
that page was accessed.
To help us calculate and record access times, we’ll write a Python context manager1
that will wrap our code that we want to calculate and record access times for.
This context manager will get the current time, let the wrapped code execute, and
then calculate the total time of execution, record it in Redis, and also update a ZSET of
the highest access time contexts. The next listing shows our context manager for performing
this set of operations.
There’s some magic going on in the access_time() context manager, and it’ll probably
help to see it in use to understand what’s going on. The following code shows the
access_time() context manager being used to record access times of web pages that
are served through a similar kind of callback method as part of a middleware layer or
plugin that was used in our examples from chapter 2:
After seeing the example, even if you don’t yet understand how to create a context
manager, you should at least know how to use one. In this example, we used the access
time context manager to calculate the total time to generate a web page. This context
manager could also be used to record the time it takes to make a database query or
the amount of time it takes to render a template. As an exercise, can you think of
other types of context managers that could record statistics that would be useful? Or
can you add reporting of access times that are more than two standard deviations
above average to the recent_log()?
GATHERING STATISTICS AND COUNTERS IN THE REAL WORLDI know that we just
spent several pages talking about how to gather fairly important statistics about
how our production systems operate, but let me remind you that there are preexisting
software packages designed for collecting and plotting counters and statistics. My personal favorite is Graphite (http://graphite.wikidot.com/), which you should probably download and install before spending too much time building your own data-plotting library.
Now that we’ve been recording diverse and important information about the state of
our application into Redis, knowing more about our visitors can help us to answer
1 In Python, a context manager is a specially defined function or class that will have parts of it executed before
and after a given block of code is executed. This allows, for example, the easy opening and automatic closing