In order to capture code coverage on your backend server, you need to run it using our Python test listener agent. Below you’ll find several options depending on your server type.
Note |
---|
The below commands assume the token and session id are located in the working directory. If not, you can provide the location to them file locations using the sl.tokenFile and sl.buildSessionIdFile environment variables |
uWSGI
When working with uWSGI, you need to run uWSGI it with the following flags either in the command line or in the uwsgi configuration file: --enable-threads --single-interpreter --lazy-apps --import python_agent.init
...
If you need to pass the parameters via the CLI, the command line will be similar to this
Code Block |
---|
uwsgi ... --enable-threads --single-interpreter --import python_agent.init |
Code Block |
---|
firstline | 1 |
---|
title | Sample
If using the uWSGI config file option, the content should look like the sample below
Code Block |
---|
[uwsgi]
...
enable-threads = true
single-interpreter = true
import = python_agent.init |
Gunicorn WSGI
When working with Gunicorn WSGI you need to run it with the following a configuration file which contains an import of the SeaLights Python agent in the post_fork section
Code Block |
---|
...
def post_fork(server, worker):
import python_agent.init
... |
Code Block |
---|
firstline | 1 |
---|
title | Sample Gunicorn config file |
---|
|
...
def post_fork(server, worker)
Uvicorn ASGI
Uvicorn does not support importing module to be run before the app is started (as in uwsgi).
in order to overcome this there is a code change need to be done at the source code in order to init Sealights when Uvicorn is loading
Code Block |
---|
...
# Check for an environment variable and import accordingly
if os.getenv('ENABLE_SEALIGHTS', 'false') == 'true':
import python_agent.init
... |
Running the app:
Code Block |
---|
export ENABLE_SEALIGHTS=true
uvicorn app:APP --host 0.0.0.0 --port 9092 --workers 1 --reload |
Expand |
---|
title | Sample code for dynamic update of files |
---|
|
If you don’t want to update your source code, you may evaluate adding these few script lines (or equivalent) in your CI/CD. Usage: ./myscript.sh mypythonfile.py Code Block |
---|
| #!/bin/bash
[[ $1 == *.py && -f $1 ]] || { echo "Error: Provide a valid Python file (.py)"; exit 1; }
# Lines to add as an environment variable
LINES_TO_ADD=$(cat <<'EOF'
# Check for an environment variable and import accordingly
if os.getenv('ENABLE_SEALIGHTS', 'false') == 'true':
import python_agent.init
EOF
)
# Create a backup and modify the file
cp "$1" "$1.bak" && sed -i "/^import\|^from /a\\$LINES_TO_ADD" "$1" && echo "[Sealights] Lines inserted into $1, backup created at $1.bak." |
|
Other servers (Generic solution)
For other servers, run with the SeaLights Python agent and the run flags
Code Block |
---|
firstline | 1 |
---|
title | Sample Usage |
---|
sl-python run {--cov-report /path/to/report.xml --labid Lab1} python <your server and args...> |
Info |
title |
---|
The first parameter after the run command should be an executable file |