-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Looking for advice on ThreadPool separation for middleware and actual event, looking for ContextVars to be passed #1267
Comments
The context var gets set but then all events/actions/views need to update the ContextVar, which isn't ideal. |
Hi @EasyAsABC123 thanks for writing in 💯 I'm not familiar with ContextVar but if I'm understanding this correctly it may be easier to create an You can assign this @app.use
def auth_acme(client, context, logger, next):
id_value = uuid4().hex
context["correlation_id"] = id_value
logger.info(f"New request with id {id_value} started")
# Pass control to the next middleware
next()
# Update the view on submission
@app.view("view_1")
def handle_submission(ack, body, context, logger):
# The build_new_view() method returns a modal view
ack(response_action="update", view=build_new_view(body))
logger.info(f"{context['correlation_id']} view request acknowledged") Would this be a viable workaround for you? |
I was hoping to avoid passing the correlation_id into each logging.info/error call, so I have a LogFilter where I am setting the result.correlation_id to the ContextVar, as a current workaround I wrote a function wrapper for each app.event or app.action function, those functions just have to take the context variable that slack bolt passes… So flow wise GlobalMiddleware - sets the correlation_id ContextVar and the context that slack bolt passes, because slack bold doesn’t execute the middleware in the same thread pool worker as the event function, I then have to wrap these functions and pull out the context[“correlation_id”] and set the ContextVar if it is there and not already set for this thread. With that done the LogFilter can pull the correlation_id: ContextVar and all logging gets the correlation_id in it. Since I put correlation_id into the log formatter as well… Mainly curious if the middleware could be changed to execute in the same thread pool worker as the event functions, or at least for the thread executor if copy_context().run could wrap the calls to have the same ContextVar variables available from middleware to event function. The wrapper solution is working but just adds another layer where errors might occur. It feels worth it for me to easily trace all logging gets messages to a single event trigger from slack. I get that switch thread executors might be too big of an ask though. |
Looking for advice on ThreadPool separation for middleware and actual event, looking for ContextVars to be passed
Reproducible in:
The
slack_bolt
versionslack_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.
Context
LogFilter
Middleware
Expected result:
Actual result:
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.
The text was updated successfully, but these errors were encountered: