Inspector
BlogTry for free
  • Concepts
    • Introduction
    • Metrics
    • Notification Channels
      • Email
      • Slack
      • Telegram
      • Microsoft Teams
      • Google Chat
      • Webhook
      • Discord
      • Pushover
      • PagerDuty
      • Twilio - SMS
    • Filtering Syntax
    • Alerts
    • Grouping Patterns
    • Custom Segments
    • Billing
    • AI Bug Fixer
  • Security and access
    • Access control
    • Two-factor authentication
    • Connected Devices
  • SDK
    • PHP
      • Installation & Set Up
      • Custom Segments
      • Exceptions Monitoring
      • Configuration
    • Laravel / Lumen
      • Upgrade Guide
      • Installation & Set-up
      • Http Requests Monitoring
      • Configuration
      • Exception Monitoring
      • Laravel Vapor
      • Laravel Octane
      • Laravel Nova Tool
      • Group by service name
    • Symfony
      • Installation
      • Configuration
      • Exception Monitoring
    • CodeIgniter
      • Installation
      • Configuration
      • Exception Monitoring
    • Drupal
    • Spring Boot
    • Slim
    • NodeJS
      • Configurations
      • Custom Segments
      • Exception monitoring
      • Autowiring
    • ExpressJs
    • Fastify
    • Python
    • Django
      • Installation & Set Up
      • Custom Segments
      • Error Monitoring
  • REST API
    • Authentication
    • Apps
    • Platforms
    • Transactions
    • Segments
    • Analytics
Powered by GitBook
On this page
  • Requirements
  • Install
  • Initialization
  • Transactions
  • Add Segments
  • Errors Monitoring
  • Put it all together
  1. SDK

Python

Code Execution Monitoring for Python applications.

PreviousFastifyNextDjango

Last updated 3 months ago

Requirements

  • Python >= 3.x

Install

Install the latest version of the package from PyPI:

pip install inspector-python

Initialization

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)

To get a new Ingestion Key you need an Inspector account. 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.

/*
 * 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...

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:

inspector.start_segment('name', 'label')

// Your code block here

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.

Errors Monitoring

Inspector allows you to report exceptions for monitoring and alerting pourpose:

try:
    // Your code here...
    raise ValueError('Test Error')
except Exception as e:
    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.

Put it all together

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.

Sign Up