2018-05-09 14:55:03 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-05-09 14:55:03 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2018-08-20 12:27:52 -04:00
|
|
|
import threading
|
2021-10-28 09:14:42 -04:00
|
|
|
import traceback
|
|
|
|
from typing import Dict, Mapping, Set, Tuple
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
from prometheus_client.core import Counter, Histogram
|
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
from synapse.logging.context import current_context
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.metrics import LaterGauge
|
2018-05-09 14:55:03 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# total number of responses served, split by method/servlet/tag
|
2018-05-22 17:54:22 -04:00
|
|
|
response_count = Counter(
|
|
|
|
"synapse_http_server_response_count", "", ["method", "servlet", "tag"]
|
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
requests_counter = Counter(
|
|
|
|
"synapse_http_server_requests_received", "", ["method", "servlet"]
|
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
outgoing_responses_counter = Counter(
|
|
|
|
"synapse_http_server_responses", "", ["method", "code"]
|
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
response_timer = Histogram(
|
2018-10-19 06:45:45 -04:00
|
|
|
"synapse_http_server_response_time_seconds",
|
|
|
|
"sec",
|
2018-07-18 09:19:00 -04:00
|
|
|
["method", "servlet", "tag", "code"],
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
response_ru_utime = Counter(
|
2018-05-28 05:10:27 -04:00
|
|
|
"synapse_http_server_response_ru_utime_seconds", "sec", ["method", "servlet", "tag"]
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
response_ru_stime = Counter(
|
2018-05-28 05:10:27 -04:00
|
|
|
"synapse_http_server_response_ru_stime_seconds", "sec", ["method", "servlet", "tag"]
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
response_db_txn_count = Counter(
|
|
|
|
"synapse_http_server_response_db_txn_count", "", ["method", "servlet", "tag"]
|
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
|
|
|
# seconds spent waiting for db txns, excluding scheduling time, when processing
|
|
|
|
# this request
|
2018-05-22 17:54:22 -04:00
|
|
|
response_db_txn_duration = Counter(
|
|
|
|
"synapse_http_server_response_db_txn_duration_seconds",
|
|
|
|
"",
|
|
|
|
["method", "servlet", "tag"],
|
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
|
|
|
# seconds spent waiting for a db connection, when processing this request
|
2018-05-22 17:54:22 -04:00
|
|
|
response_db_sched_duration = Counter(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_http_server_response_db_sched_duration_seconds",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
|
|
|
["method", "servlet", "tag"],
|
2018-05-09 14:55:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# size in bytes of the response written
|
2018-05-22 17:54:22 -04:00
|
|
|
response_size = Counter(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_http_server_response_size", "", ["method", "servlet", "tag"]
|
2018-05-09 14:55:03 -04:00
|
|
|
)
|
|
|
|
|
2018-05-21 11:03:39 -04:00
|
|
|
# In flight metrics are incremented while the requests are in flight, rather
|
|
|
|
# than when the response was written.
|
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_ru_utime = Counter(
|
2018-10-19 06:45:45 -04:00
|
|
|
"synapse_http_server_in_flight_requests_ru_utime_seconds", "", ["method", "servlet"]
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_ru_stime = Counter(
|
2018-10-19 06:45:45 -04:00
|
|
|
"synapse_http_server_in_flight_requests_ru_stime_seconds", "", ["method", "servlet"]
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_db_txn_count = Counter(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_http_server_in_flight_requests_db_txn_count", "", ["method", "servlet"]
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
|
|
|
# seconds spent waiting for db txns, excluding scheduling time, when processing
|
|
|
|
# this request
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_db_txn_duration = Counter(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_http_server_in_flight_requests_db_txn_duration_seconds",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
|
|
|
["method", "servlet"],
|
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
|
|
|
# seconds spent waiting for a db connection, when processing this request
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_db_sched_duration = Counter(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_http_server_in_flight_requests_db_sched_duration_seconds",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
|
|
|
["method", "servlet"],
|
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2021-10-28 09:14:42 -04:00
|
|
|
_in_flight_requests: Set["RequestMetrics"] = set()
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2020-10-23 12:38:40 -04:00
|
|
|
# Protects the _in_flight_requests set from concurrent access
|
2018-08-21 11:28:10 -04:00
|
|
|
_in_flight_requests_lock = threading.Lock()
|
2018-08-20 12:27:52 -04:00
|
|
|
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2021-10-28 09:14:42 -04:00
|
|
|
def _get_in_flight_counts() -> Mapping[Tuple[str, ...], int]:
|
|
|
|
"""Returns a count of all in flight requests by (method, server_name)"""
|
2018-06-20 06:18:57 -04:00
|
|
|
# Cast to a list to prevent it changing while the Prometheus
|
|
|
|
# thread is collecting metrics
|
2018-08-21 11:28:10 -04:00
|
|
|
with _in_flight_requests_lock:
|
2018-08-20 12:27:52 -04:00
|
|
|
reqs = list(_in_flight_requests)
|
2018-06-20 06:18:57 -04:00
|
|
|
|
|
|
|
for rm in reqs:
|
2018-05-22 11:56:03 -04:00
|
|
|
rm.update_metrics()
|
2018-05-22 04:31:53 -04:00
|
|
|
|
2018-05-21 11:03:39 -04:00
|
|
|
# Map from (method, name) -> int, the number of in flight requests of that
|
2021-10-28 09:14:42 -04:00
|
|
|
# type. The key type is Tuple[str, str], but we leave the length unspecified
|
|
|
|
# for compatability with LaterGauge's annotations.
|
|
|
|
counts: Dict[Tuple[str, ...], int] = {}
|
2018-06-20 06:18:57 -04:00
|
|
|
for rm in reqs:
|
2018-10-19 06:45:45 -04:00
|
|
|
key = (rm.method, rm.name)
|
2018-05-21 11:03:39 -04:00
|
|
|
counts[key] = counts.get(key, 0) + 1
|
|
|
|
|
2018-05-22 04:31:53 -04:00
|
|
|
return counts
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-22 11:56:03 -04:00
|
|
|
|
2018-05-22 11:50:26 -04:00
|
|
|
LaterGauge(
|
2018-06-20 06:18:57 -04:00
|
|
|
"synapse_http_server_in_flight_requests_count",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
2018-05-22 11:50:26 -04:00
|
|
|
["method", "servlet"],
|
2018-05-22 17:54:22 -04:00
|
|
|
_get_in_flight_counts,
|
2018-05-22 04:31:53 -04:00
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RequestMetrics:
|
2021-10-28 09:14:42 -04:00
|
|
|
def start(self, time_sec: float, name: str, method: str) -> None:
|
|
|
|
self.start_ts = time_sec
|
2020-03-24 10:45:33 -04:00
|
|
|
self.start_context = current_context()
|
2018-05-09 14:55:03 -04:00
|
|
|
self.name = name
|
2018-05-21 11:03:39 -04:00
|
|
|
self.method = method
|
|
|
|
|
2021-10-28 09:14:42 -04:00
|
|
|
if self.start_context:
|
|
|
|
# _request_stats records resource usage that we have already added
|
|
|
|
# to the "in flight" metrics.
|
|
|
|
self._request_stats = self.start_context.get_resource_usage()
|
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
"Tried to start a RequestMetric from the sentinel context.\n%s",
|
|
|
|
"".join(traceback.format_stack()),
|
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-08-21 11:28:10 -04:00
|
|
|
with _in_flight_requests_lock:
|
2018-08-20 12:27:52 -04:00
|
|
|
_in_flight_requests.add(self)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2022-05-12 10:33:50 -04:00
|
|
|
def stop(self, time_sec: float, response_code: int, sent_bytes: int) -> None:
|
2018-08-21 11:28:10 -04:00
|
|
|
with _in_flight_requests_lock:
|
2018-08-20 12:27:52 -04:00
|
|
|
_in_flight_requests.discard(self)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
context = current_context()
|
2018-05-09 14:55:03 -04:00
|
|
|
|
|
|
|
tag = ""
|
|
|
|
if context:
|
|
|
|
tag = context.tag
|
|
|
|
|
|
|
|
if context != self.start_context:
|
2021-10-28 09:14:42 -04:00
|
|
|
logger.error(
|
2018-05-09 14:55:03 -04:00
|
|
|
"Context have unexpectedly changed %r, %r",
|
2018-10-19 06:45:45 -04:00
|
|
|
context,
|
|
|
|
self.start_context,
|
2018-05-09 14:55:03 -04:00
|
|
|
)
|
|
|
|
return
|
2021-10-28 09:14:42 -04:00
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
"Trying to stop RequestMetrics in the sentinel context.\n%s",
|
|
|
|
"".join(traceback.format_stack()),
|
|
|
|
)
|
|
|
|
return
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2022-05-12 10:33:50 -04:00
|
|
|
response_code_str = str(response_code)
|
2018-07-18 08:59:36 -04:00
|
|
|
|
2022-05-12 10:33:50 -04:00
|
|
|
outgoing_responses_counter.labels(self.method, response_code_str).inc()
|
2018-05-09 18:44:22 -04:00
|
|
|
|
2018-09-17 12:16:50 -04:00
|
|
|
response_count.labels(self.method, self.name, tag).inc()
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2022-05-12 10:33:50 -04:00
|
|
|
response_timer.labels(self.method, self.name, tag, response_code_str).observe(
|
2021-10-28 09:14:42 -04:00
|
|
|
time_sec - self.start_ts
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-07-10 08:56:07 -04:00
|
|
|
resource_usage = context.get_resource_usage()
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-09-17 12:16:50 -04:00
|
|
|
response_ru_utime.labels(self.method, self.name, tag).inc(
|
2018-10-19 06:45:45 -04:00
|
|
|
resource_usage.ru_utime
|
2018-07-10 08:56:07 -04:00
|
|
|
)
|
2018-09-17 12:16:50 -04:00
|
|
|
response_ru_stime.labels(self.method, self.name, tag).inc(
|
2018-10-19 06:45:45 -04:00
|
|
|
resource_usage.ru_stime
|
2018-07-10 08:56:07 -04:00
|
|
|
)
|
2018-09-17 12:16:50 -04:00
|
|
|
response_db_txn_count.labels(self.method, self.name, tag).inc(
|
2018-07-10 08:56:07 -04:00
|
|
|
resource_usage.db_txn_count
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-09-17 12:16:50 -04:00
|
|
|
response_db_txn_duration.labels(self.method, self.name, tag).inc(
|
2018-07-10 08:56:07 -04:00
|
|
|
resource_usage.db_txn_duration_sec
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-09-17 12:16:50 -04:00
|
|
|
response_db_sched_duration.labels(self.method, self.name, tag).inc(
|
2018-07-10 08:56:07 -04:00
|
|
|
resource_usage.db_sched_duration_sec
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-21 20:48:57 -04:00
|
|
|
|
2018-09-17 12:16:50 -04:00
|
|
|
response_size.labels(self.method, self.name, tag).inc(sent_bytes)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
|
|
|
# We always call this at the end to ensure that we update the metrics
|
|
|
|
# regardless of whether a call to /metrics while the request was in
|
|
|
|
# flight.
|
|
|
|
self.update_metrics()
|
|
|
|
|
2022-05-12 10:33:50 -04:00
|
|
|
def update_metrics(self) -> None:
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Updates the in flight metrics with values from this request."""
|
2021-10-28 09:14:42 -04:00
|
|
|
if not self.start_context:
|
|
|
|
logger.error(
|
|
|
|
"Tried to update a RequestMetric from the sentinel context.\n%s",
|
|
|
|
"".join(traceback.format_stack()),
|
|
|
|
)
|
|
|
|
return
|
2018-07-10 08:56:07 -04:00
|
|
|
new_stats = self.start_context.get_resource_usage()
|
|
|
|
|
|
|
|
diff = new_stats - self._request_stats
|
|
|
|
self._request_stats = new_stats
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-10-19 06:45:45 -04:00
|
|
|
# max() is used since rapid use of ru_stime/ru_utime can end up with the
|
|
|
|
# count going backwards due to NTP, time smearing, fine-grained
|
|
|
|
# correction, or floating points. Who knows, really?
|
|
|
|
in_flight_requests_ru_utime.labels(self.method, self.name).inc(
|
|
|
|
max(diff.ru_utime, 0)
|
|
|
|
)
|
|
|
|
in_flight_requests_ru_stime.labels(self.method, self.name).inc(
|
|
|
|
max(diff.ru_stime, 0)
|
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_db_txn_count.labels(self.method, self.name).inc(
|
|
|
|
diff.db_txn_count
|
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_db_txn_duration.labels(self.method, self.name).inc(
|
2018-05-28 05:39:27 -04:00
|
|
|
diff.db_txn_duration_sec
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-05-21 11:03:39 -04:00
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
in_flight_requests_db_sched_duration.labels(self.method, self.name).inc(
|
2018-05-28 05:39:27 -04:00
|
|
|
diff.db_sched_duration_sec
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|