Running Your Backend Server with SeaLights
The SeaLights agent loads inside the Python process at startup to capture code coverage. It needs SL_TOKEN, SL_BUILDSESSIONIDFILE, and SL_LABID to report that coverage correctly.
TL;DR — which method should I use?
Method 1: Python startup hook
You control the runtime environment; use it by default because it's non-intrusive (needs no code changes).
python app.py, custom runner, Uvicorn (standalone)
Method 2: sl-python wrapper
You cannot change the image or PYTHONPATH; it requires modifying the launch command.
Any
Method 3: Server-specific integration
You run Gunicorn or uWSG servers that fork workers after startup.
uWSGI, Gunicorn
Required SeaLights Configuration
Before starting the application, provide these three runtime values:
SeaLights reads these variables only when the process starts. Restart or redeploy after changing them.
SL_TOKEN— the SeaLights agent token. Supply it from a secret store.SL_BUILDSESSIONIDFILE— the path to the generated build session ID file. A prior SeaLights CLI or CI step generates the build session ID.Keep it alongside the application when possible.
Use
./buildSessionId.txtlocally or/app/buildSessionId.txtin an image.Kubernetes injection uses
/sealights/buildSessionId.txt.
SL_LABID— identifies the test environment, such asstagingoruatwhere this run's coverage rolls up.Every process, container, worker, and replica in that testing environment must use the same value.
In addition, SeaLights assigns each runtime instance a unique Agent ID.
For a quick local check, install the agent:
The examples below use quick-install commands. For production deployments, follow the instructions in "Downloading the Python Agent" and your organization's delivery practices.
Method 1 — Python startup hook (recommended)
Create a sitecustomize.py file containing:
Python automatically imports sitecustomize.py during startup. Its directory must be discoverable through PYTHONPATH or Python's site search path.
This is the recommended default. It requires no application code changes.
Local validation or POC
Use this approach to validate instrumentation locally. It suits proof-of-concept work and troubleshooting. Do not use it as production installation guidance.
Bash
SEALIGHTS_TOKEN must already be exported from your secret store. This script reads it; it does not provide it.
PowerShell
SEALIGHTS_TOKEN must already be exported from your secret store. This script reads it; it does not provide it.
Prefer Kubernetes injection over the Dockerfile approach when you do not own the image build.
Use this when you already have an application image. The init container writes the agent and sitecustomize.py to a shared volume. The application loads them through PYTHONPATH.
This pattern is recommended when you own the image build. The image contains the SeaLights agent configuration and artifact-specific settings (such as the BSID file location), while the deployment supplies environment-specific configuration such as the token and Lab ID.
Dockerfile
Kubernetes runtime configuration
Method 2 — sl-python wrapper
The agent ships with an sl-python CLI that wraps any Python invocation. Use this when you cannot modify the application source or PYTHONPATH, but you can change the launch command.
The first argument after run must be an executable.
TODO: confirm signal-forwarding behavior of sl-python run.
Method 3 — Server-specific integration
Use this method only for Gunicorn or uWSGI. These servers fork workers after startup. Each worker must load the agent after it forks.
Load the agent from Gunicorn's post_fork hook:
Gunicorn runs post_fork for every worker. This ensures each worker loads the agent.
Provide SL_TOKEN, SL_BUILDSESSIONIDFILE, and SL_LABID through the Gunicorn process environment.
Load the agent in every worker:
Provide SL_TOKEN, SL_BUILDSESSIONIDFILE, and SL_LABID through the uWSGI process environment.
Troubleshooting & FAQ
See these checks when coverage does not appear.
Coverage not appearing?
General checks
Confirm the agent and
SL_TOKENare available.Confirm
SL_BUILDSESSIONIDFILEpoints to an existing file.Verify the runtime through the Live Agents API.
Method 1 — Python startup hook
Confirm
sitecustomize.pyexists.Confirm
PYTHONPATHpoints to its directory.
Method 3 — Gunicorn and uWSGI
For Gunicorn, confirm
post_forkimportspython_agent.init.For uWSGI, confirm
lazy-apps,single-interpreter, andenable-threadsare enabled.
Kubernetes runtime and permission checks
Match the init container's Python version to the application runtime.
For non-root containers, ensure the application can read the shared volume:
See also
Use CI-managed Python Agent Injection when deployment configuration is your only change point.
Last updated
Was this helpful?

