elastic / elastic/apm-agent-python

Error during to sending traces data to server somehow causes python server failure

オープン
#1,804 コメント 8 件 リアクション 0 件 担当者 0 名 GitHub で見る
agent-python community triage
主要言語
Python
スター
431
フォーク
239
平均マージ
5日 10時間
マージ済み PR(30日)
7

説明

**Describe the bug**:
I was faced with a very strange and weird issue. When ELK server fails and responds with 500 error, a current HTTP request fails too and my server responds with 500 status and plain text from ELK response. And this error doesn't pass through any middleware, I have a couple of them, one for sentry integration and another for error message transforming, and none of them was triggered.

This is a full logs from my app:

```python
Failed to submit message: 'HTTP 500: \r\n500 Internal Server Error\r\n\r\n

500 Internal Server Error

\r\n
nginx/1.20.1\r\n\r\n\r\n'
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/elasticapm/transport/base.py", line 279, in _flush
self.send(data, forced_flush=forced_flush)
File "/usr/local/lib/python3.11/site-packages/elasticapm/transport/http.py", line 114, in send
raise TransportException(message, data, print_trace=print_trace)
elasticapm.transport.exceptions.TransportException: HTTP 500:
500 Internal Server Error

500 Internal Server Error



nginx/1.20.1


```

**To Reproduce**

Unfortunately, I can't reproduce it in my dev configuration. It happens only in production environment.
But I guess it happens like that:
1. A python server receives HTTP request from a user for example on root path "/"
2. Around the same time the elk-apm lib tries to send some data to elk server
3. Server responds with 500 error
4. ???
5. Somehow this error passed through to end user, and the user sees 500 error with plain text from the original exception

**Environment (please complete the following information)**
- OS: Debian GNU/Linux 11 (bullseye)
- Python version: 3.11.3
- Framework and version: Fastapi 0.92.0
- APM Server version:
- Agent version: 6.15.1

**Additional context**

- Agent config options

Click to expand

```
APM_ENABLED: 'True'
APM_DEBUG: 'False'
APM_SERVICE_NAME: "project"
APM_ENVIRONMENT: "production"
APM_CLOUD_PROVIDER: "aws"
APM_TRANSACTION_IGNORE_URLS: "[]"
APM_SERVER_URL: "https://some.host"
APM_SERVER_TIMEOUT: "10s"
APM_SAMPLE_RATE: "1.0"
APM_LOG_LEVEL: "info"
APM_TRANSPORT_CLASS: "elasticapm.transport.http.Transport"
```

- `requirements.txt`:

Click to expand

```
alembic==1.9.4
anyio==3.6.2
asyncpg==0.27.0
authlib==1.2.0
certifi==2022.12.7
cffi==1.15.1
click==8.1.3
cryptography==39.0.2
dnspython==2.3.0
elastic-apm==6.15.1
email-validator==1.3.1
fastapi==0.92.0
greenlet==2.0.2
h11==0.14.0
httpcore==0.16.3
httpx==0.23.3
idna==3.4
jinja2==3.1.2
mako==1.2.4
markupsafe==2.1.2
prometheus-client==0.16.0
prometheus-fastapi-instrumentator==5.10.0
psycopg==3.1.8
pycparser==2.21
pydantic[email]==1.10.5
pyjwt==2.6.0
pyotp==2.8.0
python-multipart==0.0.6
rfc3986[idna2008]==1.5.0
sentry-sdk==1.16.0
sniffio==1.3.0
sqlalchemy==2.0.4
starlette==0.25.0
typing-extensions==4.5.0
urllib3==1.26.14
uvicorn==0.20.0
wrapt==1.15.0
```

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

elasticapm/transport/base.py の _flush と elasticapm/transport/http.py の send から始め、失敗した APM 送信がどのように FastAPI リクエストに到達し得るかを追跡します。HTTP 500 を返す APM サーバーを使って本番環境のタイミングを再現し、例外が報告されたミドルウェアをバイパスするか確認します。失敗経路が分離され、回帰テストでカバーされていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
fastapi, python
領域
api, backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。