Python
Code Execution Monitoring for Python applications.
Requirements
Python >= 3.x
Install
Install the latest version of the package from PyPI:
Initialization
Here's a code example of how Inspector is normally initialized in a Python script:
To get a new Ingestion Key you need an Inspector account. Sign Up if haven't one yet.
Transactions
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.
Add Segments
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:
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 |
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.
Errors Monitoring
Inspector allows you to report exceptions for monitoring and alerting pourpose:
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.
Put it all together
Below you can find the best strategy to securely monitor a code block in your Python script.
The finally
statement ensures that you end the segment even if an error occurs.
Last updated