Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add a metric which increments when a request is received #2965

Merged
merged 3 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
)
)

requests_counter = metrics.register_counter(
"requests_received",
labels=["method", "servlet", ],
)

outgoing_responses_counter = metrics.register_counter(
"responses",
labels=["method", "code"],
Expand Down Expand Up @@ -146,7 +151,8 @@ def wrapped_request_handler(self, request):
# at the servlet name. For most requests that name will be
# JsonResource (or a subclass), and JsonResource._async_render
# will update it once it picks a servlet.
request_metrics.start(self.clock, name=self.__class__.__name__)
servlet_name = self.__class__.__name__
request_metrics.start(self.clock, name=servlet_name)

request_context.request = request_id
with request.processing():
Expand All @@ -155,6 +161,7 @@ def wrapped_request_handler(self, request):
if include_metrics:
yield request_handler(self, request, request_metrics)
else:
requests_counter.inc(request.method, servlet_name)
yield request_handler(self, request)
except CodeMessageException as e:
code = e.code
Expand Down Expand Up @@ -286,6 +293,7 @@ def _async_render(self, request, request_metrics):
servlet_classname = "%r" % callback

request_metrics.name = servlet_classname
requests_counter.inc(request.method, servlet_classname)

# Now trigger the callback. If it returns a response, we send it
# here. If it throws an exception, that is handled by the wrapper
Expand Down Expand Up @@ -342,7 +350,7 @@ def _send_response(self, request, code, response_json_object,


def _options_handler(request):
return {}
return 200, {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should have been in dbe80a2, but I'm a crank

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just finding it very confusing to try to guess whether the {} is a response body, or parsed URL segments, or what - especially _get_handler_for_request seems to append its own {} tuple alongside the returned handler... just a snidget of pydoc feels like it would be useful for confused bears

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(especially as it looks like you were confused too, based on missing the 200)



def _unrecognised_request_handler(request):
Expand Down
16 changes: 16 additions & 0 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,31 @@ def _register(self, metric_class, name, *args, **kwargs):
return metric

def register_counter(self, *args, **kwargs):
"""
Returns:
CounterMetric
"""
return self._register(CounterMetric, *args, **kwargs)

def register_callback(self, *args, **kwargs):
"""
Returns:
CallbackMetric
"""
return self._register(CallbackMetric, *args, **kwargs)

def register_distribution(self, *args, **kwargs):
"""
Returns:
DistributionMetric
"""
return self._register(DistributionMetric, *args, **kwargs)

def register_cache(self, *args, **kwargs):
"""
Returns:
CacheMetric
"""
return self._register(CacheMetric, *args, **kwargs)


Expand Down