Skip to content

Installing the Python Agent

Installing the Python agent

The Lightrun agent is at the core of the Lightrun platform. It's the component that communicates requests for runtime observability to your running code, and gathers and relays the requested data back to the Lightrun Server, and eventually the developer's IDE.

Before running the agent, it must be installed on the system where your code to be monitored is running, and its credentials must be declared either inside the application code, or in an agent.config file.

Version Support

The instructions below apply to Python v2.7 and Python v3.6 onwards If you're interested in support for other versions, please reach out and let us know!

System requirements

Please check the Python agent system requirements here.

Lightrun can be installed both directly and inside of a Docker container.

Direct installation

  1. Install the python agent by running python -m pip install lightrun.

  2. Import Lightrun inside your main function, by adding the following code at the beginning of the function.

    try:
         import lightrun
         lightrun.enable(company_key="<COMPANY_SECRET>")
    except ImportError as e:
         print("Error importing Lightrun: ", e)
    

    Getting Company Details

    You can get your <COMPANY_SECRET> and <server_url> key by logging into the Management Portal and inspecting the Set up an agent section.

    Note

    Other configuration parameters can also be passed here. For example, add the server_url parameter to the lightrun.enable command when using Lightrun on-prem.

    try:
        import lightrun
        lightrun.enable(company_key="<COMPANY_SECRET>", com_lightrun_server="<server_url>")
    except ImportError as e:
        print("Error importing Lightrun: ", e)
    
  3. Run the application as you normally would; for example, python app.py.

Important

  • Lightrun actions in Python must only be applied to a function before the function is called. Actions inserted into a Python function while the function is currently executing will only take effect once the function is exited and called again. This also means that you can not use Lightrun actions in a Python program's __main__ method as it is already running when the Lightrun agent initializes.
  • If your Python program doesn’t have a main() method and all the code is in one scope, you will not be able to use Lightrun effectively. It is necessary to introduce a main() method that triggers the other methods that you want to debug with Lightrun.

    For example, you won't be able to debug with Lightrun in the following code:

    def main():
        // code which does not include calls to other methods -- Lightrun actions won't work
    
        // ONLY code in methods which are called anew after the Lightrun action has been placed will have a chance to be observed
    
    if __name__ == '__main__':
        main()
    

    To use Lightrun, you need to have other methods that are triggered by the main() method.

    def echo():
        //  some logic/algorithm
        // Lightrun actions will work in this method, for invocations of this method that happen AFTER the Lightrun action is created
    
    def main():
        // Lightrun actions won't work in this method
        echo()
    
    if __name__ == '__main__':
        main()
    

Run as command line argument

You can also install a Lightrun agent to your application by specifying your Lightrun credentials as environment variables when you start your Python application from your command-line interface (CLI).

Note

This option does not work for Gunicorn applications.

  1. ​Open a terminal and change to the working directory where your project is located.
  2. Install Lightrun in your application’s folder.

    python -m pip install lightrun
    
  3. Run your application with your Lightrun credentials.

    python -m lightrun --com_lightrun_server=<server_url> --company_key=<COMPANY_SECRET> -- app.py
    

Change <server_url> to your Lightrun server URL, app.py to the name of your python file, and <COMPANY_SECRET> to your Lightrun company key.

Getting Company Details

You can get your <COMPANY_SECRET> and <server_url> key by logging into the Management Portal and inspecting the Set up an agent section.

Docker installation

Docker containers are ephemeral.

If you were to create a shell into a Docker container (by docker exec -it <container-id>, for example) and add the Lightrun files there, they would disappear the next time the container would spin up due to the ephemeral nature of that container.

Instead, we suggest you install Lightrun by adding it to the underlying Docker image directly, i.e. by "baking" the agent into the image.

This is an example Dockerfile that installs Lightrun with a simple dummy application, assuming the credentials were added to the source code of your application as shown above:

FROM python:3.8
RUN pip install lightrun
COPY prime_main.py /app/prime_main.py
CMD ["python", "/app/prime_main.py"]

If you prefer passing the credentials via the command line, here's how to do it inside a Dockerfile:

FROM python:3.8
RUN pip install lightrun
COPY prime_main.py /app/prime_main.py
CMD ["python", "-m", "lightrun", "--company_secret=<COMPANY_SECRET>", "--", "app/prime_main.py"]

Note: Add the server_url parameter when using Lightrun on-prem.

Important

Installing Lightrun in this fashion does not absolve you of the need to import Lightrun to your application. Please make sure to add Lightrun as an import to your application file.

Lightrun in Alpine Linux

To install a Lightrun agent in Alpine Linux:

  1. Create a Dockerfile and add the following to the file:

    # Stage 0 - base alpine to create a .whl from requirements
    ARG python_base_repo='python'
    ARG python_base_tag='3.9.14-alpine3.16'
    FROM ${python_base_repo}:${python_base_tag} as base
    
    ARG lightrun_key='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx2b'
    ARG server_url='https://app.lightrun.com'
    ARG install_script='python-install-agent.sh'
    ARG company_id='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    
    WORKDIR /wheel
    
    # Install all the build requirements and build the wheel files
    # Any dpenedncy requiring compilation will be built from source
    RUN apk add --update --no-cache gcc musl-dev libffi-dev curl && \
        LIGHTRUN_KEY=${lightrun_key} sh -c "$(curl -k -L "${server_url}/download/company/${company_id}/${install_script}?platform=alpine")" && \
        whl_ver=`python --version | grep -oE [3]\.[[:digit:]]{1,3}\.[[:digit:]]{1,3} | cut -d. -f1,2 | tr -d '.'` && \
        whl_file=`basename agent/lightrun-*${whl_ver}*.whl` && \
        mkdir ./wheels && cp -vfp agent/${whl_file} ./wheels && \
        python -m pip install --upgrade pip johnnydep && \
        johnnydep ./wheels/${whl_file} --verbose 0 | tail -n +3 | grep -v lightrun | tr -cd '\11\12\15\40-\176' | awk '{print $1}' > requirements.txt && \
        python -m pip wheel -r requirements.txt --wheel-dir=/wheel/wheels
    
    # Stage 1 - use a python base
    #   Copy wheels from base (stage 0) and install the Lightrun agent
    #   The agent installation is done using the built wheels (not from PyPI) 
    FROM ${python_base_repo}:${python_base_tag}
    
    ARG arg_user='lightrun'
    
    # Copy all the compiled wheel dependencies
    COPY --from=base /wheel /wheel
    WORKDIR /home/${arg_user}
    
    # Install a standard C++ library and doas, and add a new user
    RUN apk add --no-cache doas libstdc++ && \
        adduser -h /home/${arg_user} -D -s /bin/sh -g "${arg_user} ${arg_user}" ${arg_user} && \
        addgroup ${arg_user} wheel && \
        echo "permit nopass keepenv :${arg_user}" | tee -a /etc/doas.d/doas.conf && \
        rm -rf /var/cache/apk/*
    
    USER ${arg_user}
    
    # Run the pip install command as a non-root user
    RUN doas -u ${arg_user} python -m pip install --user --no-cache-dir --no-index --find-links=/wheel/wheels /wheel/wheels/lightrun*.whl
    
    ENV PATH="/home/$arg_user/.local/bin:${PATH}"
    
  2. Build the Dockerfile with the following command.

    docker build \
        --build-arg lightrun_key="<lightrun_secret_key>" \
        --build-arg company_id="<lightrun_company_id>" \
        --build-arg python_base_tag="<python_base_tag>" \
        -t python-alpine-lightrun .
    

    Change <lightrun_secret_key> to your Lightrun company key, <lightrun_company_id> to your Lightrun company ID, and <python_base_tag> to your preferred Python Alpine tag.

    Getting Company Details

    1. You can get your <COMPANY_SECRET> (<lightrun_secret_key>) key by logging into the Management Portal and inspecting the Set up an agent section.
    2. You can get your <lightrun_company_id> by inspecting your Management Portal dashboard URL.
      https://app.lightrun.com/company/<lightrun_company_id>
      
    3. Configure the server_url build arg when using Lightrun on-prem.
      --build-arg server_url="<server_url>" \
      
  3. Run the docker container.

    docker run \
        --rm \
        -it \
        --name alpine-agent \
        python-alpine-lightrun \
        /bin/sh
    

  4. Put your source code into the docker container.

  5. Add the Lightrun agent to your source code by following the instructions in your Lightrun Management Portal.

    Note

    Installing the Lightrun agent is not required.

    Python Agent


Last update: February 7, 2023