Skip to main content

Thrift protocol analyzer

Project description

Table of Contents

tl;dr

thrift-tools is a library and a set of tools to introspect Apache Thrift traffic.

Installing

You can install thrift-tools via pip:

$ pip install thrift-tools

Or run it from source (if you have the dependencies installed, see below):

$ git clone ...
$ cd thrift-tools
$ sudo FROM_SOURCE=1 bin/thrift-tool --iface=eth0 --port 9091 dump --show-all --pretty
...

Tools

thrift-tool can be used in interactive mode to analyze live thrift messages:

$ sudo thrift-tool --iface eth0 --port 9091 dump --show-all --pretty
[00:39:42:850848] 10.1.8.7:49858 -> 10.1.2.20:3636: method=dosomething, type=call, seqid=1120
header: ()
fields: [   (   'struct',
        1,
        [   ('string', 1, 'something to do'),
            ('i32', 3, 0),
            (   'struct',
                9,
                [   ('i32', 3, 2),
                    ('i32', 14, 0),
                    ('i32', 16, 0),
                    ('i32', 18, 25)])])]
------>[00:39:42:856204] 10.1.2.20:3636 -> 10.1.8.7:49858: method=dosomething, type=reply, seqid=1120
        header: ()
        fields: [   (   'struct',
        0,
        [   ('string', 1, 'did something'),
            ('string', 2, 'did something else'),
            ('string', 3, 'did some other thing'),
            ('string', 4, 'did the last thing'),
            ('i32', 6, 3),
            ('i32', 7, 11),
            ('i32', 8, 0),
            ('i32', 9, 0),
            ('list', 10, [0]),
...

Alternatively, offline pcap files may be introspected:

$ sudo thrift-tool --port 9091 --pcap-file /path/to/myservice.pcap dump
...

Note that you still need to set the right port.

If you are using Finagle, try something like:

$ sudo thrift-tool --iface eth0 --port 9091 dump --show-all --pretty --finagle-thrift
...

JSON output is available for easy filtering & querying via jq. For instance, you can enumerate all the IPs calling the method ‘search’ via:

$ sudo thrift-tool --port 3030 dump --unpaired --json | jq 'select(.method == "search" and .type == "call") | .src'
"10.1.18.5:48534"
"10.1.60.2:52008"
"10.1.10.27:49856"
"10.1.23.24:48116"
"10.1.26.7:60462"
"10.1.11.10:41895"
"10.1.15.13:35285"
"10.1.7.17:39759"
"10.1.1.19:35481"
...

Gathering per method latency stats is available via the stats command:

$ sudo thrift-tool --port 6666 stats --count 100
method      count         avg         min         max         p90         p95         p99        p999
--------  -------  ----------  ----------  ----------  ----------  ----------  ----------  ----------
search2        61  0.00860996  0.00636292  0.0188479   0.010778    0.015192    0.0174422   0.0187074
doc            39  0.00134846  0.00099802  0.00274897  0.00177183  0.00199242  0.00256242  0.00273031
287 unmatched calls

To list all the available options:

$ thrift-tool --help

Note that for servers with high throughput (i.e.: > couple Ks packets per second), it might be hard for thrift-tools to keep up because start of message detection is a bit expensive (and you can only go so fast with Python). For these cases, you are better off saving a pcap file (i.e.: via tcpdump) and then post-processing it, i.e.:

$ tcpdump -nn -t port 3030 -w dump.pcap
$ sudo thrift-tool --port 3030 --pcap-file dump.pcap stats --count 40000
method      count         avg         min         max         p90         p95         p99        p999
--------  -------  ----------  ----------  ----------  ----------  ----------  ----------  ----------
resize      40000  0.00850996  0.00336091  0.0101364   0.008071    0.009132    0.009890   0.01005665

Library

To use thrift-tools from another (Python) application, you can import it via:

from thrift_tools.message_sniffer import MessageSnifferOptions, MessageSniffer

options = MessageSnifferOptions(
    iface='eth0',
    port='3636',
    ip=None,                         # include msgs from all IPs
    pcap_file=None,                  # don't read from a pcap file, live sniff
    protocol=None,                   # auto detect protocol
    finagle_thrift=False,            # apache thrift (not twitter's finagle)
    read_values=True,                # read the values of each msg/struct
    max_queued=20000,                # decent sized queue
    max_message_size=2000,           # 2k messages to keep mem usage frugal
    debug=False                      # don't print parsing errors, etc
    )

def printer(timestamp, src, dst, msg):
  print '%s %s %s %s' % (timestamp, src, dst, msg)

message_sniffer = MessageSniffer(options, printer)

# loop forever
message_sniffer.join()

Of if you want to use a pcap file:

options = MessageSnifferOptions(
    iface='eth0',
    port='3636',
    ip=None,
    pcap_file="/tmp/myservice.pcap",
    protocol=None,
    finagle_thrift=False,
    read_values=True,
    max_queued=20000,
    max_message_size=2000,
    debug=False
    )

...

If you want to filter messages for specific IPs:

options = MessageSnifferOptions(
    iface='eth0',
    port='3636',
    ip=['172.16.24.3', '172.16.24.4'],  # ignores everyone else
    pcap_file="/tmp/myservice.pcap",
    protocol=None,
    finagle_thrift=False,
    read_values=True,
    max_queued=20000,
    max_message_size=2000,
    debug=False
    )

...

See examples/ for more ways to use this library!

Tests

To run the tests:

$ python setup.py nosetests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thrift-tools-0.0.5.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

thrift_tools-0.0.5-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file thrift-tools-0.0.5.tar.gz.

File metadata

  • Download URL: thrift-tools-0.0.5.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for thrift-tools-0.0.5.tar.gz
Algorithm Hash digest
SHA256 e9426c614faae57d7fbfa89f01a7333be0374e474e7916f7f946313cb9b2fead
MD5 7b41d01212b838b7ea2b89225c2b4bb9
BLAKE2b-256 5d64b9671f3d7a37695ea6ac042701028645f6aedd6f6b486417ad9190d0f3da

See more details on using hashes here.

File details

Details for the file thrift_tools-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: thrift_tools-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for thrift_tools-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ed2cda080363fc82c6a9ebc3d3cc0c12de1bd36fdf45f98214a7c4f38b9ed56f
MD5 ecb9416e97cf3b8aa6293bc574511ddf
BLAKE2b-256 89c87afd1fbe33f74ec8e325bc0273d1e7d942c1cbef527cc2065da62700c7da

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page