|
| 1 | +# SkyWalking Python Agent Command-Line Interface(CLI) |
| 2 | + |
| 3 | +In earlier releases than 0.7.0, you would at least need to add the following lines to your applications to get the agent attached and running. |
| 4 | + |
| 5 | +```python |
| 6 | +from skywalking import agent |
| 7 | +agent.start() |
| 8 | +``` |
| 9 | + |
| 10 | + |
| 11 | +Now the SkyWalking Python agent implements a command-line interface that can be utilized to attach the agent to your |
| 12 | +awesome applications during deployment **without changing any application code**, |
| 13 | +just like the [SkyWalking Java Agent](https://github.com/apache/skywalking). |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +Upon successful [installation of the SkyWalking Python agent via pip](../README.md#install), |
| 18 | +a command-line script `sw-python` is installed in your environment(virtual env preferred). |
| 19 | + |
| 20 | +### The `run` option |
| 21 | + |
| 22 | +Currently, the `sw-python` CLI provides a `run` option, which you can use to execute your applications |
| 23 | +(either begins with the `python` command or Python-based programs like `gunicorn` on your path) |
| 24 | +just like you invoke them normally, plus a prefix, the following example demonstrates the usage. |
| 25 | + |
| 26 | +If your previous command to run your gunicorn application is: |
| 27 | + |
| 28 | +`gunicorn app.wsgi` |
| 29 | + |
| 30 | +Please change it to: |
| 31 | + |
| 32 | +`sw-python run gunicorn app.wsgi` |
| 33 | + |
| 34 | +The SkyWalking Python agent will startup along with your application shortly. |
| 35 | + |
| 36 | +Note that the command does work with multiprocessing and subprocess as long as the `PYTHONPATH` is inherited, |
| 37 | +please configure the environment variables configuration accordingly based on the general documentation. |
| 38 | + |
| 39 | +When executing commands with `sw-python run command`, your command's Python interpreter will pick up the SkyWalking loader module. |
| 40 | + |
| 41 | +It is not safe to attach SkyWalking Agent to those commands that resides in another Python installation |
| 42 | +because incompatible Python versions and mismatched SkyWalking versions can cause problems. |
| 43 | +Therefore, any attempt to pass a command that uses a different Python interpreter/ environment will not bring up |
| 44 | +SkyWalking Python Agent even if another SkyWalking Python agent is installed there(no matter the version), |
| 45 | +and will force exit with an error message indicating the reasoning. |
| 46 | + |
| 47 | +#### Disabling child processes from starting new agents |
| 48 | + |
| 49 | +Sometimes you don't actually need the agent to monitor anything in a child process. |
| 50 | + |
| 51 | +If you do not need the agent to get loaded for application child processes, you can turn off the behavior by setting an environment variable. |
| 52 | + |
| 53 | +`SW_PYTHON_BOOTSTRAP_PROPAGATE` to `False` |
| 54 | + |
| 55 | +Note the auto bootstrap depends on the environment inherited by child processes, |
| 56 | +thus prepending a new sitecustomize path to or removing the loader path from the `PYTHONPATH` could prevent the agent from loading in a child process. |
| 57 | + |
| 58 | +### Configuring the agent |
| 59 | + |
| 60 | +You would normally want to provide additional configurations other than the default ones. |
| 61 | + |
| 62 | +#### Through environment variables |
| 63 | + |
| 64 | +The currently supported method is to provide the environment variables listed |
| 65 | +in [EnvVars Doc](EnvVars.md) as instructed in the [README](../README.md). |
| 66 | + |
| 67 | +#### Through a sw-config.yaml |
| 68 | + |
| 69 | +Currently, only environment variable configuration is supported; an optional `yaml` configuration is to be implemented. |
| 70 | + |
| 71 | +### Enabling CLI DEBUG mode |
| 72 | + |
| 73 | +Note the CLI is a new feature that manipulates the Python interpreter bootstrap behaviour, there could be unsupported cases. |
| 74 | + |
| 75 | +If you encounter unexpected problems, please turn on the DEBUG mode by adding the `-d` or `--debug` flag to your `sw-python` command, as shown below. |
| 76 | + |
| 77 | +From: `sw-python run command` |
| 78 | + |
| 79 | +To: `sw-python -d run command` |
| 80 | + |
| 81 | +Please attach the debug logs to the [SkyWalking Issues](https://github.com/apache/skywalking/issues) section if you believe it is a bug, |
| 82 | +[idea discussions](https://github.com/apache/skywalking/discussions) and [pull requests](https://github.com/apache/skywalking-python/pulls) are always welcomed. |
| 83 | + |
| 84 | +#### Known limitations |
| 85 | + |
| 86 | +1. The CLI may not work properly with arguments that involve double quotation marks in some shells. |
| 87 | +2. The CLI and bootstrapper stdout logs could get messy in Windows shells. |
0 commit comments