elastic / elastic/apm-agent-python

Support for setting the execution context after import time to support late gevent patching

Đang mở
#894 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
agent-python feature
Ngôn ngữ chính
Python
Star
431
Fork
239
Merge trung bình
5 ngày 10 giờ
Pull request đã merge (30 ngày)
7

Mô tả

**Is your feature request related to a problem? Please describe.**
I'm working with a legacy codebase that imports some code, forks and does gevent monkey patching, then imports a bunch more code. I'm running into an issue where `elasticapm.traces.execution_context` is initialized at import time during the first phase, using the not-monkeypatched `threading.local` / `contextvars`, which causes transactions / spans to be linked to the wrong requests.

**Describe the solution you'd like**
Since fixing my codebase to not import the logging code is rather involved, I am looking for a safe way to either re-initialize the current execution context or assign it to a newly constructed instance. Ideally, I would be able to do something like

```py
set_execution_context(ThreadLocalContext())
```

after I know gevent has done its patching. This would also open the door for using custom execution context implementations, if someone ever wanted to do that.

This seems like it would be a bit tricky to implement in the library as it is now, since there is a lot of
```py
from elasticapm.traces import execution_context
```
which AFAIK makes it impractical to do something like `elasticapm.traces.execution_context = Blah()` due to the multiple names [thing](https://docs.python.org/3/library/unittest.mock.html#where-to-patch).

**Describe alternatives you've considered**
I've converted our setup code to use only local imports for elasticapm (in functions executed after I know gevent has finished patching), which works okay. It's a little fragile, though - if anyone ever imports elasticapm without the proper guards things could break again.

I think this might be specific to our codebase - we should really be patching gevent before we import things anyway. Feel free to close if this doesn't seem worth the effort, since I have a pretty solid work-around. Love the library and great work. 😄

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách truy vết cách elasticapm.traces.execution_context được khởi tạo và cách các tên được import của nó được sử dụng trong toàn bộ package. Điều tra một cách an toàn để thay thế hoặc khởi tạo lại context đó sau khi gevent patching, sau đó xác minh rằng các transaction và span sử dụng context mới mà không làm hỏng các import hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
observability-sre
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.