mirror of
https://github.com/element-hq/synapse
synced 2024-08-19 22:00:22 +00:00
Make the in flight requests metrics thread safe
This commit is contained in:
parent
1058d14127
commit
b01a755498
1 changed files with 10 additions and 3 deletions
|
@ -15,6 +15,7 @@
|
|||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import threading
|
||||
|
||||
from prometheus_client.core import Counter, Histogram
|
||||
|
||||
|
@ -111,6 +112,9 @@ in_flight_requests_db_sched_duration = Counter(
|
|||
# The set of all in flight requests, set[RequestMetrics]
|
||||
_in_flight_requests = set()
|
||||
|
||||
# Protects the _in_flight_requests set from concurrent accesss
|
||||
_in_flight_reqeuests_lock = threading.Lock()
|
||||
|
||||
|
||||
def _get_in_flight_counts():
|
||||
"""Returns a count of all in flight requests by (method, server_name)
|
||||
|
@ -120,6 +124,7 @@ def _get_in_flight_counts():
|
|||
"""
|
||||
# Cast to a list to prevent it changing while the Prometheus
|
||||
# thread is collecting metrics
|
||||
with _in_flight_reqeuests_lock:
|
||||
reqs = list(_in_flight_requests)
|
||||
|
||||
for rm in reqs:
|
||||
|
@ -154,9 +159,11 @@ class RequestMetrics(object):
|
|||
# to the "in flight" metrics.
|
||||
self._request_stats = self.start_context.get_resource_usage()
|
||||
|
||||
with _in_flight_reqeuests_lock:
|
||||
_in_flight_requests.add(self)
|
||||
|
||||
def stop(self, time_sec, request):
|
||||
with _in_flight_reqeuests_lock:
|
||||
_in_flight_requests.discard(self)
|
||||
|
||||
context = LoggingContext.current_context()
|
||||
|
|
Loading…
Reference in a new issue