Python
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:
1
from inspector import Configuration, Inspector
2
3
config = Configuration('xxxxxxxxxxxxxxxxxxx')
4
5
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:
1
inspector.start_segment('name', 'label')
2
3
// Your code block here
4
5
inspector.segment().end()
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:
Parameter | Required | Description |
type | YES | This is the master category of your segments |
label | NO | Human readable label or specific task's name that will be used as label inside the timeline. Otherwise type is used. |
Think about how database queries is reported. There is one master category like
mysql
but 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
mysql
as type parameter and use the specific query string
or task name
as label for each statements of this type.Inspector allows you to report exceptions for monitoring and alerting pourpose:
1
try:
2
// Your code here...
3
raise ValueError('Test Error')
4
except Exception as e:
5
inspector.report_exception(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.
inspector.start_segment('name', 'label')
try:
# Your code here...
except Exception as e:
inspector.report_exception(e)
finally:
inspector.segment().end()
The
finally
statement ensures that you end the segment even if an error occurs.Last modified 2mo ago