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.
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.
1pip 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 python23import logging4from r7insight import R7InsightHandler567log = logging.getLogger('r7insight')8log.setLevel(logging.INFO)9test = R7InsightHandler(TOKEN, REGION)1011log.addHandler(test)1213log.warn("Warning message")14log.info("Info message")1516sleep(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
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
1import time2import logging3from r7insight import R7InsightHandler, metrics456TEST = metrics.Metric(METRIC_TOKEN, REGION)78@TEST.metric()9def function_one(t):10"""A dummy function that takes some time."""11time.sleep(t)1213if __name__ == '__main__':14function_one(1)15
This will result in the following output.
12Tue 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