2018-05-09 20:55:03 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2018-05-22 02:48:57 +02:00
|
|
|
from prometheus_client.core import Counter, Histogram
|
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.metrics import LaterGauge
|
2018-05-09 20:55:03 +02:00
|
|
|
from synapse.util.logcontext import LoggingContext
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# total number of responses served, split by method/servlet/tag
|
2018-05-22 23:54:22 +02:00
|
|
|
response_count = Counter(
|
|
|
|
"synapse_http_server_response_count", "", ["method", "servlet", "tag"]
|
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
requests_counter = Counter(
|
|
|
|
"synapse_http_server_requests_received", "", ["method", "servlet"]
|
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
outgoing_responses_counter = Counter(
|
|
|
|
"synapse_http_server_responses", "", ["method", "code"]
|
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
response_timer = Histogram(
|
2018-07-18 15:19:00 +02:00
|
|
|
"synapse_http_server_response_time_seconds", "sec",
|
|
|
|
["method", "servlet", "tag", "code"],
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
response_ru_utime = Counter(
|
2018-05-28 11:10:27 +02:00
|
|
|
"synapse_http_server_response_ru_utime_seconds", "sec", ["method", "servlet", "tag"]
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
response_ru_stime = Counter(
|
2018-05-28 11:10:27 +02:00
|
|
|
"synapse_http_server_response_ru_stime_seconds", "sec", ["method", "servlet", "tag"]
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
response_db_txn_count = Counter(
|
|
|
|
"synapse_http_server_response_db_txn_count", "", ["method", "servlet", "tag"]
|
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
|
|
|
# seconds spent waiting for db txns, excluding scheduling time, when processing
|
|
|
|
# this request
|
2018-05-22 23:54:22 +02:00
|
|
|
response_db_txn_duration = Counter(
|
|
|
|
"synapse_http_server_response_db_txn_duration_seconds",
|
|
|
|
"",
|
|
|
|
["method", "servlet", "tag"],
|
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
|
|
|
# seconds spent waiting for a db connection, when processing this request
|
2018-05-22 23:54:22 +02:00
|
|
|
response_db_sched_duration = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_response_db_sched_duration_seconds",
|
2018-05-22 23:54:22 +02:00
|
|
|
"",
|
|
|
|
["method", "servlet", "tag"],
|
2018-05-09 20:55:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# size in bytes of the response written
|
2018-05-22 23:54:22 +02:00
|
|
|
response_size = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_response_size", "", ["method", "servlet", "tag"]
|
2018-05-09 20:55:03 +02:00
|
|
|
)
|
|
|
|
|
2018-05-21 17:03:39 +02:00
|
|
|
# In flight metrics are incremented while the requests are in flight, rather
|
|
|
|
# than when the response was written.
|
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_ru_utime = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_in_flight_requests_ru_utime_seconds",
|
2018-05-22 23:54:22 +02:00
|
|
|
"",
|
|
|
|
["method", "servlet"],
|
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_ru_stime = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_in_flight_requests_ru_stime_seconds",
|
2018-05-22 23:54:22 +02:00
|
|
|
"",
|
|
|
|
["method", "servlet"],
|
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_db_txn_count = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_in_flight_requests_db_txn_count", "", ["method", "servlet"]
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
|
|
|
# seconds spent waiting for db txns, excluding scheduling time, when processing
|
|
|
|
# this request
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_db_txn_duration = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_in_flight_requests_db_txn_duration_seconds",
|
2018-05-22 23:54:22 +02:00
|
|
|
"",
|
|
|
|
["method", "servlet"],
|
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
|
|
|
# seconds spent waiting for a db connection, when processing this request
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_db_sched_duration = Counter(
|
2018-05-23 20:03:51 +02:00
|
|
|
"synapse_http_server_in_flight_requests_db_sched_duration_seconds",
|
2018-05-22 23:54:22 +02:00
|
|
|
"",
|
|
|
|
["method", "servlet"],
|
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 10:31:53 +02:00
|
|
|
# The set of all in flight requests, set[RequestMetrics]
|
2018-05-21 17:03:39 +02:00
|
|
|
_in_flight_requests = set()
|
|
|
|
|
|
|
|
|
2018-05-22 10:31:53 +02:00
|
|
|
def _get_in_flight_counts():
|
|
|
|
"""Returns a count of all in flight requests by (method, server_name)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict[tuple[str, str], int]
|
|
|
|
"""
|
2018-06-20 12:18:57 +02:00
|
|
|
# Cast to a list to prevent it changing while the Prometheus
|
|
|
|
# thread is collecting metrics
|
|
|
|
reqs = list(_in_flight_requests)
|
|
|
|
|
|
|
|
for rm in reqs:
|
2018-05-22 17:56:03 +02:00
|
|
|
rm.update_metrics()
|
2018-05-22 10:31:53 +02:00
|
|
|
|
2018-05-21 17:03:39 +02:00
|
|
|
# Map from (method, name) -> int, the number of in flight requests of that
|
|
|
|
# type
|
|
|
|
counts = {}
|
2018-06-20 12:18:57 +02:00
|
|
|
for rm in reqs:
|
2018-05-21 17:03:39 +02:00
|
|
|
key = (rm.method, rm.name,)
|
|
|
|
counts[key] = counts.get(key, 0) + 1
|
|
|
|
|
2018-05-22 10:31:53 +02:00
|
|
|
return counts
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 17:56:03 +02:00
|
|
|
|
2018-05-22 17:50:26 +02:00
|
|
|
LaterGauge(
|
2018-06-20 12:18:57 +02:00
|
|
|
"synapse_http_server_in_flight_requests_count",
|
2018-05-22 23:54:22 +02:00
|
|
|
"",
|
2018-05-22 17:50:26 +02:00
|
|
|
["method", "servlet"],
|
2018-05-22 23:54:22 +02:00
|
|
|
_get_in_flight_counts,
|
2018-05-22 10:31:53 +02:00
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-09 20:55:03 +02:00
|
|
|
|
|
|
|
class RequestMetrics(object):
|
2018-05-28 11:39:27 +02:00
|
|
|
def start(self, time_sec, name, method):
|
|
|
|
self.start = time_sec
|
2018-05-09 20:55:03 +02:00
|
|
|
self.start_context = LoggingContext.current_context()
|
|
|
|
self.name = name
|
2018-05-21 17:03:39 +02:00
|
|
|
self.method = method
|
|
|
|
|
2018-07-10 14:56:07 +02:00
|
|
|
# _request_stats records resource usage that we have already added
|
|
|
|
# to the "in flight" metrics.
|
|
|
|
self._request_stats = self.start_context.get_resource_usage()
|
2018-05-21 17:03:39 +02:00
|
|
|
|
|
|
|
_in_flight_requests.add(self)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-05-28 11:39:27 +02:00
|
|
|
def stop(self, time_sec, request):
|
2018-05-21 17:03:39 +02:00
|
|
|
_in_flight_requests.discard(self)
|
|
|
|
|
2018-05-09 20:55:03 +02:00
|
|
|
context = LoggingContext.current_context()
|
|
|
|
|
|
|
|
tag = ""
|
|
|
|
if context:
|
|
|
|
tag = context.tag
|
|
|
|
|
|
|
|
if context != self.start_context:
|
|
|
|
logger.warn(
|
|
|
|
"Context have unexpectedly changed %r, %r",
|
|
|
|
context, self.start_context
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2018-07-18 14:59:36 +02:00
|
|
|
response_code = str(request.code)
|
|
|
|
|
|
|
|
outgoing_responses_counter.labels(request.method, response_code).inc()
|
2018-05-10 00:44:22 +02:00
|
|
|
|
2018-05-22 02:48:57 +02:00
|
|
|
response_count.labels(request.method, self.name, tag).inc()
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-07-18 14:59:36 +02:00
|
|
|
response_timer.labels(request.method, self.name, tag, response_code).observe(
|
2018-05-28 11:39:27 +02:00
|
|
|
time_sec - self.start
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-07-10 14:56:07 +02:00
|
|
|
resource_usage = context.get_resource_usage()
|
2018-05-09 20:55:03 +02:00
|
|
|
|
2018-07-10 14:56:07 +02:00
|
|
|
response_ru_utime.labels(request.method, self.name, tag).inc(
|
|
|
|
resource_usage.ru_utime,
|
|
|
|
)
|
|
|
|
response_ru_stime.labels(request.method, self.name, tag).inc(
|
|
|
|
resource_usage.ru_stime,
|
|
|
|
)
|
2018-05-22 23:54:22 +02:00
|
|
|
response_db_txn_count.labels(request.method, self.name, tag).inc(
|
2018-07-10 14:56:07 +02:00
|
|
|
resource_usage.db_txn_count
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
|
|
|
response_db_txn_duration.labels(request.method, self.name, tag).inc(
|
2018-07-10 14:56:07 +02:00
|
|
|
resource_usage.db_txn_duration_sec
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-22 02:48:57 +02:00
|
|
|
response_db_sched_duration.labels(request.method, self.name, tag).inc(
|
2018-07-10 14:56:07 +02:00
|
|
|
resource_usage.db_sched_duration_sec
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-22 02:48:57 +02:00
|
|
|
|
|
|
|
response_size.labels(request.method, self.name, tag).inc(request.sentLength)
|
2018-05-21 17:03:39 +02: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()
|
|
|
|
|
|
|
|
def update_metrics(self):
|
|
|
|
"""Updates the in flight metrics with values from this request.
|
|
|
|
"""
|
2018-07-10 14:56:07 +02:00
|
|
|
new_stats = self.start_context.get_resource_usage()
|
|
|
|
|
|
|
|
diff = new_stats - self._request_stats
|
|
|
|
self._request_stats = new_stats
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 17:50:26 +02:00
|
|
|
in_flight_requests_ru_utime.labels(self.method, self.name).inc(diff.ru_utime)
|
|
|
|
in_flight_requests_ru_stime.labels(self.method, self.name).inc(diff.ru_stime)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_db_txn_count.labels(self.method, self.name).inc(
|
|
|
|
diff.db_txn_count
|
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_db_txn_duration.labels(self.method, self.name).inc(
|
2018-05-28 11:39:27 +02:00
|
|
|
diff.db_txn_duration_sec
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|
2018-05-21 17:03:39 +02:00
|
|
|
|
2018-05-22 23:54:22 +02:00
|
|
|
in_flight_requests_db_sched_duration.labels(self.method, self.name).inc(
|
2018-05-28 11:39:27 +02:00
|
|
|
diff.db_sched_duration_sec
|
2018-05-22 23:54:22 +02:00
|
|
|
)
|