File: request_hook_tut.rst

package info (click to toggle)
python-boto 2.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,584 kB
  • ctags: 10,521
  • sloc: python: 78,553; makefile: 123
file content (61 lines) | stat: -rw-r--r-- 2,166 bytes parent folder | download | duplicates (11)
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
.. _request_hook_tut.rst:

======================================
An Introduction to boto's request hook
======================================

This tutorial shows you how to use the request hook for data gathering.

It is often important to measure things we do as developers to better
understand application performance and the interactions between components
of the system. Boto plays a key role in some of those interactions as any
client library would.

We'll go over how to use the request hook to do some simple request logging.

Creating a connection
---------------------

For this example, let's use the EC2 interface as an example. Any connection
will work (IAM, SQS, etc..)::

    >>> from boto import ec2
    >>> conn = ec2.connect_to_region('us-west-2')

You will be using this conn object for the remainder of the tutorial to send
commands to EC2.

Adding your own hook
--------------------

The hook interface is defined in boto.utils.RequestHook
The method signature looks like::

    def handle_request_data(self, request, response, error=False):

In boto.requestlog.py, there is an implementation of this interface which
is written to handle multiple threads sending data to a single log
writing thread. Exammining this file, you'll see a log file, queue and thread
are created, then as requests are made, the handle_request_data() method is
called. It extracts data from the request and respose object to create a log
message. That's inserted into the queue and handled by the _request_log_worker
thread.

One thing to note is that the boto request object has an additional value
"start_time", which is a datetime.now() as of the time right before the
request was issued. This can be used along with the current time (after the
request) to calculate the duration of the request.

To add this logger to your connection::

    >>> from boto.requestlog import RequestLogger
    >>> conn.set_request_hook(RequestLogger())

That's all you need to do! Now, if you make a request, like::

    >>> conn.get_all_volumes()

The log message produced might look something like this::

    '2014-02-26 21:38:27', '200', '0.791542', '592', 'DescribeVolumes'