Looking for advice on ThreadPool separation for middleware and actual event, looking for ContextVars to be passed
還沒有人認領這個 Issue。
評估
研究方向
該 issue 沒有指明任何 repository 檔案或測試;請先追蹤 middleware 和 event/action/view handler 如何在 thread-pool worker 之間進行分派。將相關執行路徑與 Python ContextVar 的行為進行比較,並確定預期的傳播邊界。完成這項工作需要達成共識的設計,並為 middleware 和 handler 之間的 correlation ID 提供相應的涵蓋範圍。
由索引模型根據 Issue 內容生成。
描述
Looking for advice on ThreadPool separation for middleware and actual event, looking for ContextVars to be passed
Reproducible in:
pip freeze | grep slack
python --version
sw_vers && uname -v # or `ver`
The slack_bolt version
slack_bolt==1.20.1
slack_sdk==3.34.0
slackclient==2.9.4
Python runtime version
Python 3.11.10
OS info
ProductName: macOS
ProductVersion: 15.3.1
BuildVersion: 24D70
Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000
Steps to reproduce:
I am attempting to create a trace_id/correlation_id for all slack events/actions/views etc to help with tracking in our structured logs.
- Create Global middleware, contextvar, and LogFilter
Context
from contextvars import ContextVar
from typing import Optional
from slack_bolt import BoltContext
# Middleware
correlation_id: ContextVar[Optional[str]] = ContextVar("correlation_id", default=None)
LogFilter
from logging import Filter, LogRecord
from typing import Optional
from.modules.logging import correlation_id
def _trim_string(string: Optional[str], string_length: Optional[int]) -> Optional[str]:
return string[:string_length] if string_length is not None and string else string
class CorrelationIdFilter(Filter):
"""Logging filter to attached correlation IDs to log records"""
def __init__(
self,
name: str = "",
uuid_length: int | None = None,
default_value: str | None = None,
):
super().__init__(name=name)
self.uuid_length = uuid_length
self.default_value = default_value
def filter(self, record: "LogRecord") -> bool:
"""
Attach a correlation ID to the log record.
Since the correlation ID is defined in the middleware layer, any
log generated from a request after this point can easily be searched
for, if the correlation ID is added to the message, or included as
metadata.
"""
cid = correlation_id.get(self.default_value)
record.correlation_id = _trim_string(cid, self.uuid_length)
return True
Middleware
from.modules.logging import correlation_id
def call(context: BoltContext, logger: Logger, next: Callable[[], None]) -> None:
# Capture the current context
current_context = copy_context()
current_thread = threading.current_thread()
id_value: str | None = correlation_id.get()
if id_value is not None:
logger.info(
f"{__name__} {id_value=} found id in contextvar {current_thread=}"
)
elif context.get("correlation_id", None) is not None:
id_value = context["correlation_id"]
token = correlation_id.set(id_value)
logger.info(f"{__name__} {id_value=} found id in context {current_thread=}")
else:
id_value = uuid4().hex
context["correlation_id"] = id_value
token = correlation_id.set(id_value)
logger.info(f"{__name__} {id_value=} generated new id {current_thread=}")
try:
current_context.run(next)
finally:
if token:
correlation_id.reset(token)
- Since middleware is ran in a separate threadpool worker as the action/event/view, the contextvar isn't shared between them.
- Log filter always print None from the ContextVar due to this...
Expected result:
{"levelname": "INFO", "correlation_id": "34a110e2ba6d4100a099e18fd2fb4c74", "name": "module_name", "message": "log message here", "lineno": 44, "asctime": "2025-03-07 13:47:16,182", "exc_info": null}
Actual result:
{"levelname": "INFO", "correlation_id": null, "name": "module_name", "message": "log message here", "lineno": 44, "asctime": "2025-03-07 13:47:16,182", "exc_info": null}
Requirements
Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.
- 主要語言
- Python
- 星號
- 1.3k
- 分支
- 288
- 平均合併
- 1 天 8 小時
- 30 天內合併 PR
- 10
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
slackapi/bolt-python 的其他 Issue
-
docs enhancement server-side
難度 1/5 1 小時以內 新手友好度 88/100
slackapi/bolt-python#1576 · 1 則留言 ·
-
auto-triage-skip bug security semver:major
難度 2/5 1-3 小時 新手友好度 72/100
slackapi/bolt-python#1447 · 9 則留言 ·
-
難度 3/5 1-2 天 新手友好度 65/100
slackapi/bolt-python#1577 ·
-
area:async auto-triage-skip dependencies
難度 3/5 1-2 天 新手友好度 65/100
slackapi/bolt-python#1472 · 1 則留言 · 1 個 reaction ·
-
auto-triage-skip enhancement
slackapi/bolt-python#1346 · 2 則留言 · 已指派 1 人 ·
查看 slackapi/bolt-python 的全部 Issue
相似的 Issue
-
bug confirmed issue
難度 2/5 1-3 小時 新手友好度 75/100
open-webui/open-webui#30750 · 1 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
-
enhancement
難度 2/5 1-3 小時 新手友好度 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
-
good first issue
難度 1/5 1 小時以內 新手友好度 90/100