2018-05-09 14:55:03 -04: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-21 20:48:57 -04:00
|
|
|
from prometheus_client.core import Counter, Histogram
|
|
|
|
|
2018-05-09 14:55:03 -04:00
|
|
|
from synapse.util.logcontext import LoggingContext
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# total number of responses served, split by method/servlet/tag
|
2018-05-21 20:48:57 -04:00
|
|
|
response_count = Counter("synapse_http_server_response_count", "", ["method", "servlet", "tag"])
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
requests_counter = Counter("synapse_http_server_requests_received", "", ["method", "servlet"])
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
outgoing_responses_counter = Counter("synapse_http_server_responses", "", ["method", "code"])
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
response_timer = Histogram("synapse_http_server_response_time_seconds", "", ["method", "servlet", "tag"])
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
response_ru_utime = Counter("synapse_http_server_response_ru_utime_seconds", "", ["method", "servlet", "tag"])
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
response_ru_stime = Counter("synapse_http_server_response_ru_stime_seconds", "", ["method", "servlet", "tag"])
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -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-21 20:48:57 -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-21 20:48:57 -04:00
|
|
|
response_db_sched_duration = Counter("synapse_http_request_response_db_sched_duration_seconds", "", ["method", "servlet", "tag"]
|
2018-05-09 14:55:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# size in bytes of the response written
|
2018-05-21 20:48:57 -04:00
|
|
|
response_size = Counter("synapse_http_request_response_size", "", ["method", "servlet", "tag"]
|
2018-05-09 14:55:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class RequestMetrics(object):
|
2018-05-09 18:03:11 -04:00
|
|
|
def start(self, time_msec, name):
|
|
|
|
self.start = time_msec
|
2018-05-09 14:55:03 -04:00
|
|
|
self.start_context = LoggingContext.current_context()
|
|
|
|
self.name = name
|
|
|
|
|
2018-05-09 18:03:11 -04:00
|
|
|
def stop(self, time_msec, request):
|
2018-05-09 14:55:03 -04: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-05-21 20:48:57 -04:00
|
|
|
outgoing_responses_counter.labels(request.method, str(request.code)).inc()
|
2018-05-09 18:44:22 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
response_count.labels(request.method, self.name, tag).inc()
|
2018-05-09 14:55:03 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
response_timer.labels(request.method, self.name, tag).observe(time_msec - self.start)
|
2018-05-09 14:55:03 -04:00
|
|
|
|
|
|
|
ru_utime, ru_stime = context.get_resource_usage()
|
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
response_ru_utime.labels(request.method, self.name, tag).inc(ru_utime)
|
|
|
|
response_ru_stime.labels(request.method, self.name, tag).inc(ru_stime)
|
|
|
|
response_db_txn_count.labels(request.method, self.name, tag).inc(context.db_txn_count)
|
|
|
|
response_db_txn_duration.labels(request.method, self.name, tag).inc(context.db_txn_duration_ms / 1000.)
|
|
|
|
response_db_sched_duration.labels(request.method, self.name, tag).inc(
|
|
|
|
context.db_sched_duration_ms / 1000.)
|
|
|
|
|
|
|
|
response_size.labels(request.method, self.name, tag).inc(request.sentLength)
|