|
| 1 | +[[troubleshooting]] |
| 2 | +== Troubleshooting |
| 3 | + |
| 4 | +Below are some resources and tips for troubleshooting and debugging the |
| 5 | +python agent. |
| 6 | + |
| 7 | +* <<easy-fixes>> |
| 8 | +* <<django-test>> |
| 9 | +* <<agent-logging>> |
| 10 | + |
| 11 | +[float] |
| 12 | +[[easy-fixes]] |
| 13 | +=== Easy Fixes |
| 14 | + |
| 15 | +Before you try anything else, go through the following sections to ensure that |
| 16 | +the agent is configured correctly. This is not an exhaustive list, but rather |
| 17 | +a list of common problems that users run into. |
| 18 | + |
| 19 | +[float] |
| 20 | +[[debug-mode]] |
| 21 | +==== Debug Mode |
| 22 | + |
| 23 | +Most frameworks support a debug mode. Generally, this mode is intended for |
| 24 | +non-production environments and provides detailed error messages and logging of |
| 25 | +potentially sensitive data. Because of these security issues, the agent will |
| 26 | +not collect traces if the app is in debug mode by default. |
| 27 | + |
| 28 | +You can override this behavior with the <<config-debug,`DEBUG`>> configuration. |
| 29 | + |
| 30 | +Note that configuration of the agent should occur before creation of any |
| 31 | +`ElasticAPM` objects: |
| 32 | + |
| 33 | +[source,python] |
| 34 | +---- |
| 35 | +app = Flask(__name__) |
| 36 | +app.config["ELASTIC_APM"] = {"DEBUG": True} |
| 37 | +apm = ElasticAPM(app, service_name="flask-app") |
| 38 | +---- |
| 39 | + |
| 40 | +[float] |
| 41 | +[[psutil-metrics]] |
| 42 | +==== `psutil` for Metrics |
| 43 | + |
| 44 | +To get CPU and system metrics on non-Linux systems, `psutil` must be |
| 45 | +installed. The agent should automatically show a warning on start if it is |
| 46 | +not installed, but sometimes this warning can be suppressed. Install `psutil` |
| 47 | +and metrics should be collected by the agent and sent to the APM Server. |
| 48 | + |
| 49 | +[source,bash] |
| 50 | +---- |
| 51 | +python3 -m pip install psutil |
| 52 | +---- |
| 53 | + |
| 54 | +[float] |
| 55 | +[[apm-server-credentials]] |
| 56 | +==== Credential issues |
| 57 | + |
| 58 | +In order for the agent to send data to the APM Server, it may need an |
| 59 | +<<config-api-key,`API_KEY`>> or a <<config-secret-token,`SECRET_TOKEN`>>. Double |
| 60 | +check your APM Server settings and make sure that your credentials are |
| 61 | +configured correctly. Additionally, check that <<config-server-url,`SERVER_URL`>> |
| 62 | +is correct. |
| 63 | + |
| 64 | +[float] |
| 65 | +[[django-test]] |
| 66 | +=== Django `check` and `test` |
| 67 | + |
| 68 | +When used with Django, the agent provides two management commands to help debug |
| 69 | +common issues. Head over to the <<django-troubleshooting,Django troubleshooting section>> |
| 70 | +for more information. |
| 71 | + |
| 72 | +[float] |
| 73 | +[[agent-logging]] |
| 74 | +=== Agent logging |
| 75 | + |
| 76 | +To get the agent to log more data, all that is needed is a |
| 77 | +https://docs.python.org/3/library/logging.html#handler-objects[Handler] which |
| 78 | +is attached either to the `elasticapm` logger or to the root logger. |
| 79 | + |
| 80 | +[float] |
| 81 | +[[django-agent-logging]] |
| 82 | +==== Django |
| 83 | + |
| 84 | +The simplest way to log more data from the agent is to add a console logging |
| 85 | +Handler to the `elasticapm` logger. Here's a (very simplified) example: |
| 86 | + |
| 87 | +[source,python] |
| 88 | +---- |
| 89 | +LOGGING = { |
| 90 | + 'handlers': { |
| 91 | + 'console': { |
| 92 | + 'level': 'DEBUG', |
| 93 | + 'class': 'logging.StreamHandler' |
| 94 | + } |
| 95 | + }, |
| 96 | + 'loggers': { |
| 97 | + 'elasticapm': { |
| 98 | + 'level': 'DEBUG', |
| 99 | + 'handlers': ['console'] |
| 100 | + }, |
| 101 | + }, |
| 102 | +} |
| 103 | +---- |
| 104 | + |
| 105 | +[float] |
| 106 | +[[flask-agent-logging]] |
| 107 | +==== Flask |
| 108 | + |
| 109 | +Flask https://flask.palletsprojects.com/en/1.1.x/logging/[recommends using `dictConfig()`] |
| 110 | +to set up logging. If you're using this format, adding logging for the agent |
| 111 | +will be very similar to the <<django-agent-logging,instructions for Django above>>. |
| 112 | + |
| 113 | +Otherwise, you can use the <<generic-agent-logging,generic instructions below>>. |
| 114 | + |
| 115 | +[float] |
| 116 | +[[generic-agent-logging]] |
| 117 | +==== Generic instructions |
| 118 | + |
| 119 | +Creating a console Handler and adding it to the `elasticapm` logger is easy: |
| 120 | + |
| 121 | +[source,python] |
| 122 | +---- |
| 123 | +import logging |
| 124 | +
|
| 125 | +elastic_apm_logger = logging.getLogger("elasticapm") |
| 126 | +console_handler = logging.StreamHandler() |
| 127 | +console_handler.setLevel(logging.DEBUG) |
| 128 | +elastic_apm_logger.addHandler(console_handler) |
| 129 | +---- |
| 130 | + |
| 131 | +You can also just add the console Handler to the root logger. This will apply |
| 132 | +that handler to all log messages from all modules. |
| 133 | + |
| 134 | +[source,python] |
| 135 | +---- |
| 136 | +import logging |
| 137 | +
|
| 138 | +logger = logging.getLogger() |
| 139 | +console_handler = logging.StreamHandler() |
| 140 | +console_handler.setLevel(logging.DEBUG) |
| 141 | +logger.addHandler(console_handler) |
| 142 | +---- |
| 143 | + |
| 144 | +See the https://docs.python.org/3/library/logging.html[python logging docs] |
| 145 | +for more details about Handlers (and information on how to format your logs |
| 146 | +using Formatters). |
0 commit comments