Add Python agent to a Python application service

Adding a Python agent to a Python application service

To monitor a Python application using Site24x7 APM, you need to integrate the Site24x7 Python agent as a system service. Follow the steps below to install the S24x7DataExporter, add the agent to a custom directory, and configure the environment.
Note: Ensure all prerequisites are met before installing the Python agent.

Step 1: Install the S247DataExporter

If the S24x7DataExporter is not already installed, follow these steps:
  1. Download the installation script.
  2.         
        wget -O InstallDataExporter.sh
        https://staticdownloads.site24x7.com/apminsight/S247DataExporter/linux/InstallDataExporter.sh
                
  3. Execute the script with your Site24x7 license key.
  4.     
        sudo sh InstallDataExporter.sh -license.key "your_device_key"
        
Notes
Note: Replace "your_device_key" with your account's unique license key. You can get it from your Site24x7 account by navigating to Admin > Developer > Device Key.

Step 2: Install the Python agent in a custom directory

  1. Install the Site24x7 Python agent using the --target option:
  2.                 
        pip install apminsight --target /custom/path
        
  3. Verify the installation:
  4.     pip show apminsight
        

Step 3: Add environment variables to the system service file

In your systemd service file (e.g., /etc/systemd/system/your-app.service), insert the following Environment line before the ExecStart directive.
    
    Environment="S247_LICENSE_KEY=your_device_key" "APM_APP_NAME=your_app_name" "PYTHONPATH=/custom/path/apminsight:/custom/path/apminsight/bootstrap"
    ExecStart=/usr/bin/python3 /path/to/your/flask/app/app.py
            

Notes
Note: 
1. Replace your_device_key with the unique license key (see step 1).
2. Replace your_app_name with the custom name for your application monitor as it will appear in the Site24x7 portal.
3. Ensure custom/path matches the directory used during agent installation in step 2.

Step 4: Start the application service

  1. Reload systemd and start your Python application service to activate the installed agent.
  2. Trigger a few transactions in your Python application to initiate data collection. 
Once the service is running and transactions are triggered, the Python agent will begin sending telemetry data to Site24x7. You can then view the metrics in the Site24x7 > APM > Applications section.

An example systemd service file
    
    [Unit]
    Description=Flask Application
    After=network.target

    [Service]
    User=your-username
    Group=your-username
    WorkingDirectory=/custom/path
    Environment="S247_LICENSE_KEY=your_device_key" "APM_APP_NAME=your_app_name" "PYTHONPATH=/home/apminsight:/home/apminsight/bootstrap"
    ExecStart=/usr/bin/python3 /path/to/your/flask/app/app.py
    Restart=always

    [Install]
    WantedBy=multi-user.target