Code Execution Monitoring for Python applications.
- Python >= 3.x
Install the latest version of the package from PyPI:
pip install inspector-python
Here's a code example of how Inspector is normally initialized in a Python script:
from inspector import Configuration, Inspector
config = Configuration('xxxxxxxxxxxxxxxxxxx')
inspector = Inspector(config)
A Transaction represents an execution cycle and it can contains one or hundreds of segments.
It should be started as soon as possible in your application to better represent the time your script takes to complete its job (e.g. fulfill an HTTP request, execute a cron job).
Typically the transaction should be started in the entry point of your application.
* A transaction should start as soon as possible,
* typically in the "index.php" file of your application.
inspector.start_transaction('my python script')
// Continue with the script...
Segments represents the tasks performed during the current execution cycle.
Thanks to Inspector you can put everything you want in the transactions timeline getting a real-time feedback about the execution of a code block inside your application.
Some ideas on how to use custom segments:
- Http call to external services (webhooks, integrations, etc.);
- Functions that deal with files (pdf, excel, images);
- Data manipulation processes (import/export, data aggregation, etc).
The code snippet below shows how to monitor a code block using segment:
// Your code block here
Here is an example of how the segment will appear in the timeline:
As showed in the example above a new segment is built with two input parameters:
This is the master category of your segments
Human readable label or specific task's name that will be used as label inside the timeline. Otherwise
Think about how database queries is reported. There is one master category like
mysqlbut each query has its own custom label that simply is an extract of the sql code.
- mysql: master type
- select * from table: task label
In this way you can mark toghether the statements related to the same topic or service, using something like
mysqlas type parameter and use the
specific query stringor
task nameas label for each statements of this type.
Inspector allows you to report exceptions for monitoring and alerting pourpose:
// Your code here...
raise ValueError('Test Error')
except Exception as e:
If something goes wrong in your code you will be alerted in real time in your inbox and the exception will be monitored for all subsequent occurrences.
Below you can find the best strategy to securely monitor a code block in your Python script.
# Your code here...
except Exception as e:
finallystatement ensures that you end the segment even if an error occurs.