From 7d72e44eb9660eaeab017897853cf3ec18fd06c6 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Tue, 24 Feb 2015 19:51:21 +0000 Subject: Add vector counters to HTTP clients and servers; count the requests by method and responses by method and response code --- synapse/http/client.py | 23 ++++++++++++++++++++++- synapse/http/matrixfederationclient.py | 28 +++++++++++++++++++++++++--- synapse/http/server.py | 14 ++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/synapse/http/client.py b/synapse/http/client.py index d61e53bd94..e40e82e80b 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -15,6 +15,7 @@ from synapse.api.errors import CodeMessageException from syutil.jsonutil import encode_canonical_json +import synapse.metrics from twisted.internet import defer, reactor from twisted.web.client import ( @@ -31,6 +32,15 @@ import urllib logger = logging.getLogger(__name__) +metrics = synapse.metrics.get_metrics_for(__name__) + +outgoing_requests_counter = metrics.register_counter("outgoing_requests", + keys=["method"], +) +incoming_responses_counter = metrics.register_counter("incoming_responses", + keys=["method","code"], +) + class SimpleHttpClient(object): """ @@ -48,7 +58,18 @@ class SimpleHttpClient(object): def request(self, method, *args, **kwargs): # A small wrapper around self.agent.request() so we can easily attach # counters to it - return self.agent.request(method, *args, **kwargs) + outgoing_requests_counter.inc(method) + d = self.agent.request(method, *args, **kwargs) + + def _cb(response): + incoming_responses_counter.inc(method, response.code) + return response + def _eb(failure): + incoming_responses_counter.inc(method, "ERR") + return failure + d.addCallbacks(_cb, _eb) + + return d @defer.inlineCallbacks def post_urlencoded_get_json(self, uri, args={}): diff --git a/synapse/http/matrixfederationclient.py b/synapse/http/matrixfederationclient.py index 7db001cc63..0091527693 100644 --- a/synapse/http/matrixfederationclient.py +++ b/synapse/http/matrixfederationclient.py @@ -23,6 +23,7 @@ from twisted.web._newclient import ResponseDone from synapse.http.endpoint import matrix_federation_endpoint from synapse.util.async import sleep from synapse.util.logcontext import PreserveLoggingContext +import synapse.metrics from syutil.jsonutil import encode_canonical_json @@ -40,6 +41,15 @@ import urlparse logger = logging.getLogger(__name__) +metrics = synapse.metrics.get_metrics_for(__name__) + +outgoing_requests_counter = metrics.register_counter("outgoing_requests", + keys=["method"], +) +incoming_responses_counter = metrics.register_counter("incoming_responses", + keys=["method","code"], +) + class MatrixFederationHttpAgent(_AgentBase): @@ -49,6 +59,8 @@ class MatrixFederationHttpAgent(_AgentBase): def request(self, destination, endpoint, method, path, params, query, headers, body_producer): + outgoing_requests_counter.inc(method) + host = b"" port = 0 fragment = b"" @@ -59,9 +71,19 @@ class MatrixFederationHttpAgent(_AgentBase): # Set the connection pool key to be the destination. key = destination - return self._requestWithEndpoint(key, endpoint, method, parsed_URI, - headers, body_producer, - parsed_URI.originForm) + d = self._requestWithEndpoint(key, endpoint, method, parsed_URI, + headers, body_producer, + parsed_URI.originForm) + + def _cb(response): + incoming_responses_counter.inc(method, response.code) + return response + def _eb(failure): + incoming_responses_counter.inc(method, "ERR") + return failure + d.addCallbacks(_cb, _eb) + + return d class MatrixFederationHttpClient(object): diff --git a/synapse/http/server.py b/synapse/http/server.py index 767c3ef79b..ac893bb40c 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -18,6 +18,7 @@ from synapse.api.errors import ( cs_exception, SynapseError, CodeMessageException, UnrecognizedRequestError ) from synapse.util.logcontext import LoggingContext +import synapse.metrics from syutil.jsonutil import ( encode_canonical_json, encode_pretty_printed_json @@ -34,6 +35,15 @@ import urllib logger = logging.getLogger(__name__) +metrics = synapse.metrics.get_metrics_for(__name__) + +incoming_requests_counter = metrics.register_counter("incoming_requests", + keys=["method"], +) +outgoing_responses_counter = metrics.register_counter("outgoing_responses", + keys=["method","code"], +) + class HttpServer(object): """ Interface for registering callbacks on a HTTP server @@ -112,6 +122,8 @@ class JsonResource(HttpServer, resource.Resource): This checks if anyone has registered a callback for that method and path. """ + incoming_requests_counter.inc(request.method) + code = None start = self.clock.time_msec() try: @@ -190,6 +202,8 @@ class JsonResource(HttpServer, resource.Resource): request) return + outgoing_responses_counter.inc(request.method, str(code)) + # TODO: Only enable CORS for the requests that need it. respond_with_json( request, code, response_json_object, -- cgit 1.4.1