benoitc / gunicorn

gunicorn 'Green Unicorn' is a WSGI HTTP Server for UNIX, fast clients and sleepy applications.
http://www.gunicorn.org
Other
9.82k stars 1.75k forks source link

How to programmatically get the number of current worker connections? #3152

Open andrii-korotkov-verkada opened 8 months ago

andrii-korotkov-verkada commented 8 months ago

Hello, I hope your day is going well. I'm looking to add some monitoring for services using gunicorn for the number of worker connections. This is to know when the workers are approaching the limit configured using --worker-connections flag and when it needs to be adjusted. I'm curious if there is some function like get_current_number_of_gunicorn_worker_connections() I can call in a background thread publishing the metrics? Also if there's a function is_running_gunicorn_worker() to skip setups which won't enable gunicorn. Thank you!

benoitc commented 8 months ago

use the metric?

andrii-korotkov-verkada commented 8 months ago

Which metric is available for this?

aamirawan7584 commented 2 months ago

I don't think so its directly available in Gunicorn but I think you can use pre_request and post-request hooks and maintain a global counter for number of connection which can be utilized later.