1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright 2014-2016 OpenMarket Ltd
# 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]
#
#
import logging
import threading
import traceback
from typing import Mapping
from prometheus_client.core import Counter, Histogram
from synapse.logging.context import current_context
from synapse.metrics import SERVER_NAME_LABEL, LaterGauge
logger = logging.getLogger(__name__)
# total number of responses served, split by method/servlet/tag
response_count = Counter(
"synapse_http_server_response_count",
"",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
requests_counter = Counter(
"synapse_http_server_requests_received",
"",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
outgoing_responses_counter = Counter(
"synapse_http_server_responses",
"",
labelnames=["method", "code", SERVER_NAME_LABEL],
)
response_timer = Histogram(
"synapse_http_server_response_time_seconds",
"sec",
labelnames=["method", "servlet", "tag", "code", SERVER_NAME_LABEL],
)
response_ru_utime = Counter(
"synapse_http_server_response_ru_utime_seconds",
"sec",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
response_ru_stime = Counter(
"synapse_http_server_response_ru_stime_seconds",
"sec",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
response_db_txn_count = Counter(
"synapse_http_server_response_db_txn_count",
"",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
# seconds spent waiting for db txns, excluding scheduling time, when processing
# this request
response_db_txn_duration = Counter(
"synapse_http_server_response_db_txn_duration_seconds",
"",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
# seconds spent waiting for a db connection, when processing this request
response_db_sched_duration = Counter(
"synapse_http_server_response_db_sched_duration_seconds",
"",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
# size in bytes of the response written
response_size = Counter(
"synapse_http_server_response_size",
"",
labelnames=["method", "servlet", "tag", SERVER_NAME_LABEL],
)
# In flight metrics are incremented while the requests are in flight, rather
# than when the response was written.
in_flight_requests_ru_utime = Counter(
"synapse_http_server_in_flight_requests_ru_utime_seconds",
"",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
in_flight_requests_ru_stime = Counter(
"synapse_http_server_in_flight_requests_ru_stime_seconds",
"",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
in_flight_requests_db_txn_count = Counter(
"synapse_http_server_in_flight_requests_db_txn_count",
"",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
# seconds spent waiting for db txns, excluding scheduling time, when processing
# this request
in_flight_requests_db_txn_duration = Counter(
"synapse_http_server_in_flight_requests_db_txn_duration_seconds",
"",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
# seconds spent waiting for a db connection, when processing this request
in_flight_requests_db_sched_duration = Counter(
"synapse_http_server_in_flight_requests_db_sched_duration_seconds",
"",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
_in_flight_requests: set["RequestMetrics"] = set()
# Protects the _in_flight_requests set from concurrent access
_in_flight_requests_lock = threading.Lock()
def _get_in_flight_counts() -> Mapping[tuple[str, ...], int]:
"""Returns a count of all in flight requests by (method, server_name)"""
# Cast to a list to prevent it changing while the Prometheus
# thread is collecting metrics
with _in_flight_requests_lock:
request_metrics = list(_in_flight_requests)
for request_metric in request_metrics:
request_metric.update_metrics()
# Map from (method, name) -> int, the number of in flight requests of that
# 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] = {}
for request_metric in request_metrics:
key = (
request_metric.method,
request_metric.name,
request_metric.our_server_name,
)
counts[key] = counts.get(key, 0) + 1
return counts
in_flight_requests = LaterGauge(
name="synapse_http_server_in_flight_requests_count",
desc="",
labelnames=["method", "servlet", SERVER_NAME_LABEL],
)
in_flight_requests.register_hook(
homeserver_instance_id=None, hook=_get_in_flight_counts
)
class RequestMetrics:
def __init__(self, our_server_name: str) -> None:
"""
Args:
our_server_name: Our homeserver name (used to label metrics) (`hs.hostname`)
"""
self.our_server_name = our_server_name
def start(self, time_sec: float, name: str, method: str) -> None:
self.start_ts = time_sec
self.start_context = current_context()
self.name = name
self.method = method
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()),
)
with _in_flight_requests_lock:
_in_flight_requests.add(self)
def stop(self, time_sec: float, response_code: int, sent_bytes: int) -> None:
with _in_flight_requests_lock:
_in_flight_requests.discard(self)
context = current_context()
tag = ""
if context:
tag = context.tag
if context != self.start_context:
logger.error(
"Context have unexpectedly changed %r, %r",
context,
self.start_context,
)
return
else:
logger.error(
"Trying to stop RequestMetrics in the sentinel context.\n%s",
"".join(traceback.format_stack()),
)
return
response_code_str = str(response_code)
outgoing_responses_counter.labels(
method=self.method,
code=response_code_str,
**{SERVER_NAME_LABEL: self.our_server_name},
).inc()
response_base_labels = {
"method": self.method,
"servlet": self.name,
"tag": tag,
SERVER_NAME_LABEL: self.our_server_name,
}
response_count.labels(**response_base_labels).inc()
response_timer.labels(
code=response_code_str,
**response_base_labels,
).observe(time_sec - self.start_ts)
resource_usage = context.get_resource_usage()
response_ru_utime.labels(**response_base_labels).inc(resource_usage.ru_utime)
response_ru_stime.labels(**response_base_labels).inc(resource_usage.ru_stime)
response_db_txn_count.labels(**response_base_labels).inc(
resource_usage.db_txn_count
)
response_db_txn_duration.labels(**response_base_labels).inc(
resource_usage.db_txn_duration_sec
)
response_db_sched_duration.labels(**response_base_labels).inc(
resource_usage.db_sched_duration_sec
)
response_size.labels(**response_base_labels).inc(sent_bytes)
# 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()
def update_metrics(self) -> None:
"""Updates the in flight metrics with values from this request."""
if not self.start_context:
logger.error(
"Tried to update a RequestMetric from the sentinel context.\n%s",
"".join(traceback.format_stack()),
)
return
new_stats = self.start_context.get_resource_usage()
diff = new_stats - self._request_stats
self._request_stats = new_stats
in_flight_labels = {
"method": self.method,
"servlet": self.name,
SERVER_NAME_LABEL: self.our_server_name,
}
# 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(**in_flight_labels).inc(
max(diff.ru_utime, 0)
)
in_flight_requests_ru_stime.labels(**in_flight_labels).inc(
max(diff.ru_stime, 0)
)
in_flight_requests_db_txn_count.labels(**in_flight_labels).inc(
diff.db_txn_count
)
in_flight_requests_db_txn_duration.labels(**in_flight_labels).inc(
diff.db_txn_duration_sec
)
in_flight_requests_db_sched_duration.labels(**in_flight_labels).inc(
diff.db_sched_duration_sec
)
|