This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Report per request metrics for all of the things using request_handler #756
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,12 @@ | |
_next_request_id = 0 | ||
|
||
|
||
def request_handler(request_handler): | ||
def request_handler(report_metrics=True): | ||
"""Decorator for ``wrap_request_handler``""" | ||
return lambda request_handler: wrap_request_handler(request_handler, report_metrics) | ||
|
||
|
||
def wrap_request_handler(request_handler, report_metrics): | ||
"""Wraps a method that acts as a request handler with the necessary logging | ||
and exception handling. | ||
|
||
|
@@ -96,6 +101,10 @@ def wrapped_request_handler(self, request): | |
global _next_request_id | ||
request_id = "%s-%s" % (request.method, _next_request_id) | ||
_next_request_id += 1 | ||
if report_metrics: | ||
request_metrics = RequestMetrics() | ||
request_metrics.start(self.clock) | ||
|
||
with LoggingContext(request_id) as request_context: | ||
request_context.request = request_id | ||
with request.processing(): | ||
|
@@ -133,6 +142,13 @@ def wrapped_request_handler(self, request): | |
}, | ||
send_cors=True | ||
) | ||
finally: | ||
try: | ||
request_metrics.stop( | ||
self.clock, request, self.__class__.__name__ | ||
) | ||
except: | ||
pass | ||
return wrapped_request_handler | ||
|
||
|
||
|
@@ -197,19 +213,19 @@ def render(self, request): | |
self._async_render(request) | ||
return server.NOT_DONE_YET | ||
|
||
@request_handler | ||
@request_handler(report_metrics=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can this not be True? |
||
@defer.inlineCallbacks | ||
def _async_render(self, request): | ||
""" This gets called from render() every time someone sends us a request. | ||
This checks if anyone has registered a callback for that method and | ||
path. | ||
""" | ||
start = self.clock.time_msec() | ||
if request.method == "OPTIONS": | ||
self._send_response(request, 200, {}) | ||
return | ||
|
||
start_context = LoggingContext.current_context() | ||
request_metrics = RequestMetrics() | ||
request_metrics.start(self.clock) | ||
|
||
# Loop through all the registered callbacks to check if the method | ||
# and path regex match | ||
|
@@ -241,40 +257,7 @@ def _async_render(self, request): | |
self._send_response(request, code, response) | ||
|
||
try: | ||
context = LoggingContext.current_context() | ||
|
||
tag = "" | ||
if context: | ||
tag = context.tag | ||
|
||
if context != start_context: | ||
logger.warn( | ||
"Context have unexpectedly changed %r, %r", | ||
context, self.start_context | ||
) | ||
return | ||
|
||
incoming_requests_counter.inc(request.method, servlet_classname, tag) | ||
|
||
response_timer.inc_by( | ||
self.clock.time_msec() - start, request.method, | ||
servlet_classname, tag | ||
) | ||
|
||
ru_utime, ru_stime = context.get_resource_usage() | ||
|
||
response_ru_utime.inc_by( | ||
ru_utime, request.method, servlet_classname, tag | ||
) | ||
response_ru_stime.inc_by( | ||
ru_stime, request.method, servlet_classname, tag | ||
) | ||
response_db_txn_count.inc_by( | ||
context.db_txn_count, request.method, servlet_classname, tag | ||
) | ||
response_db_txn_duration.inc_by( | ||
context.db_txn_duration, request.method, servlet_classname, tag | ||
) | ||
request_metrics.stop(self.clock, request, servlet_classname) | ||
except: | ||
pass | ||
|
||
|
@@ -307,6 +290,48 @@ def _send_response(self, request, code, response_json_object, | |
) | ||
|
||
|
||
class RequestMetrics(object): | ||
def start(self, clock): | ||
self.start = clock.time_msec() | ||
self.start_context = LoggingContext.current_context() | ||
|
||
def stop(self, clock, request, servlet_classname): | ||
context = LoggingContext.current_context() | ||
|
||
tag = "" | ||
if context: | ||
tag = context.tag | ||
|
||
if context != start_context: | ||
logger.warn( | ||
"Context have unexpectedly changed %r, %r", | ||
context, self.start_context | ||
) | ||
return | ||
|
||
incoming_requests_counter.inc(request.method, servlet_classname, tag) | ||
|
||
response_timer.inc_by( | ||
self.clock.time_msec() - start, request.method, | ||
servlet_classname, tag | ||
) | ||
|
||
ru_utime, ru_stime = context.get_resource_usage() | ||
|
||
response_ru_utime.inc_by( | ||
ru_utime, request.method, servlet_classname, tag | ||
) | ||
response_ru_stime.inc_by( | ||
ru_stime, request.method, servlet_classname, tag | ||
) | ||
response_db_txn_count.inc_by( | ||
context.db_txn_count, request.method, servlet_classname, tag | ||
) | ||
response_db_txn_duration.inc_by( | ||
context.db_txn_duration, request.method, servlet_classname, tag | ||
) | ||
|
||
|
||
class RootRedirect(resource.Resource): | ||
"""Redirects the root '/' path to another path.""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww.
Can we not instead just check if
report_metrics
is true?