Python

This is a plugin library to enable logging to Rapid7 Insight from the Python Logger. Additionally this plugin allows the user to get an overview of methods being executed, their execution time, as well as CPU and Memory statistics. Note that this plugin is asynchronous.

basic

Setup

Log in to InsightOps and create a new log by clicking the Add New Log button. Now click Manual Configuration button from the list of options. Give your log a name of your choice, select Token TCP and finally click the Register new log button. A token will be displayed in green. Please record it as we will use it later to configure the library.

Install the library

The library can be installed using the following pip command.

1
pip install r7insight_python

You can also place logentries in your requirements.txt file

Insert your Logging Code

Use the following snippet as an example usage of the library.

python
1
#!/usr/bin/env python
2
3
import logging
4
from r7insight import R7InsightHandler
5
6
7
log = logging.getLogger('r7insight')
8
log.setLevel(logging.INFO)
9
test = R7InsightHandler(TOKEN, REGION)
10
11
log.addHandler(test)
12
13
log.warn("Warning message")
14
log.info("Info message")
15
16
sleep(10)

Please note that this is an asynchronous appender.

The parameter you need to fill in is TOKEN which is the token we copied earlier. It connects this logger with the file on InsightOps. If you haven’t taken note of the token when creating your log, you can retrieve it by logging in to InsightOps. Once logged in, browse to the log you created, click the "settings" icon and the token is available

By default the appender will attempt to send it’s logs over SSL over port 443

basic

Application Metrics

The python client also allows the user to get an overview of methods being executed, their execution time, as well as CPU and Memory statistics.This is achieved by using the metric() decorator as seen below.

python
1
import time
2
import logging
3
from r7insight import R7InsightHandler, metrics
4
5
6
TEST = metrics.Metric(METRIC_TOKEN, REGION)
7
8
@TEST.metric()
9
def function_one(t):
10
"""A dummy function that takes some time."""
11
time.sleep(t)
12
13
if __name__ == '__main__':
14
function_one(1)
15

This will result in the following output.

1
2
Tue Aug 18 09:44:37 IST 2015 : INFO, function_name=function_one execution_time=1.00419783592 cpu=4.0 cpu_count=4 memory=svmem(total=8589934592L, available=4230602752L, percent=50.7, used=5016002560L, free=3571707904L, active=3454431232L, inactive=658894848L, wired=902676480L)
3