Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
13a8dfba
Unverified
Commit
13a8dfba
authored
6 years ago
by
Erik Johnston
Committed by
GitHub
6 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #3252 from matrix-org/erikj/in_flight_requests
Add in flight request metrics
parents
413482f5
c435b0b4
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/http/request_metrics.py
+159
-1
159 additions, 1 deletion
synapse/http/request_metrics.py
synapse/http/site.py
+3
-1
3 additions, 1 deletion
synapse/http/site.py
with
162 additions
and
2 deletions
synapse/http/request_metrics.py
+
159
−
1
View file @
13a8dfba
...
@@ -98,14 +98,87 @@ response_size = metrics.register_counter(
...
@@ -98,14 +98,87 @@ response_size = metrics.register_counter(
"
response_size
"
,
labels
=
[
"
method
"
,
"
servlet
"
,
"
tag
"
]
"
response_size
"
,
labels
=
[
"
method
"
,
"
servlet
"
,
"
tag
"
]
)
)
# In flight metrics are incremented while the requests are in flight, rather
# than when the response was written.
in_flight_requests_ru_utime
=
metrics
.
register_counter
(
"
in_flight_requests_ru_utime_seconds
"
,
labels
=
[
"
method
"
,
"
servlet
"
],
)
in_flight_requests_ru_stime
=
metrics
.
register_counter
(
"
in_flight_requests_ru_stime_seconds
"
,
labels
=
[
"
method
"
,
"
servlet
"
],
)
in_flight_requests_db_txn_count
=
metrics
.
register_counter
(
"
in_flight_requests_db_txn_count
"
,
labels
=
[
"
method
"
,
"
servlet
"
],
)
# seconds spent waiting for db txns, excluding scheduling time, when processing
# this request
in_flight_requests_db_txn_duration
=
metrics
.
register_counter
(
"
in_flight_requests_db_txn_duration_seconds
"
,
labels
=
[
"
method
"
,
"
servlet
"
],
)
# seconds spent waiting for a db connection, when processing this request
in_flight_requests_db_sched_duration
=
metrics
.
register_counter
(
"
in_flight_requests_db_sched_duration_seconds
"
,
labels
=
[
"
method
"
,
"
servlet
"
]
)
# The set of all in flight requests, set[RequestMetrics]
_in_flight_requests
=
set
()
def
_collect_in_flight
():
"""
Called just before metrics are collected, so we use it to update all
the in flight request metrics
"""
for
rm
in
_in_flight_requests
:
rm
.
update_metrics
()
metrics
.
register_collector
(
_collect_in_flight
)
def
_get_in_flight_counts
():
"""
Returns a count of all in flight requests by (method, server_name)
Returns:
dict[tuple[str, str], int]
"""
# Map from (method, name) -> int, the number of in flight requests of that
# type
counts
=
{}
for
rm
in
_in_flight_requests
:
key
=
(
rm
.
method
,
rm
.
name
,)
counts
[
key
]
=
counts
.
get
(
key
,
0
)
+
1
return
counts
metrics
.
register_callback
(
"
in_flight_requests_count
"
,
_get_in_flight_counts
,
labels
=
[
"
method
"
,
"
servlet
"
]
)
class
RequestMetrics
(
object
):
class
RequestMetrics
(
object
):
def
start
(
self
,
time_msec
,
name
):
def
start
(
self
,
time_msec
,
name
,
method
):
self
.
start
=
time_msec
self
.
start
=
time_msec
self
.
start_context
=
LoggingContext
.
current_context
()
self
.
start_context
=
LoggingContext
.
current_context
()
self
.
name
=
name
self
.
name
=
name
self
.
method
=
method
self
.
_request_stats
=
_RequestStats
.
from_context
(
self
.
start_context
)
_in_flight_requests
.
add
(
self
)
def
stop
(
self
,
time_msec
,
request
):
def
stop
(
self
,
time_msec
,
request
):
_in_flight_requests
.
discard
(
self
)
context
=
LoggingContext
.
current_context
()
context
=
LoggingContext
.
current_context
()
tag
=
""
tag
=
""
...
@@ -147,3 +220,88 @@ class RequestMetrics(object):
...
@@ -147,3 +220,88 @@ class RequestMetrics(object):
)
)
response_size
.
inc_by
(
request
.
sentLength
,
request
.
method
,
self
.
name
,
tag
)
response_size
.
inc_by
(
request
.
sentLength
,
request
.
method
,
self
.
name
,
tag
)
# 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.
"""
diff
=
self
.
_request_stats
.
update
(
self
.
start_context
)
in_flight_requests_ru_utime
.
inc_by
(
diff
.
ru_utime
,
self
.
method
,
self
.
name
,
)
in_flight_requests_ru_stime
.
inc_by
(
diff
.
ru_stime
,
self
.
method
,
self
.
name
,
)
in_flight_requests_db_txn_count
.
inc_by
(
diff
.
db_txn_count
,
self
.
method
,
self
.
name
,
)
in_flight_requests_db_txn_duration
.
inc_by
(
diff
.
db_txn_duration_ms
/
1000.
,
self
.
method
,
self
.
name
,
)
in_flight_requests_db_sched_duration
.
inc_by
(
diff
.
db_sched_duration_ms
/
1000.
,
self
.
method
,
self
.
name
,
)
class
_RequestStats
(
object
):
"""
Keeps tracks of various metrics for an in flight request.
"""
__slots__
=
[
"
ru_utime
"
,
"
ru_stime
"
,
"
db_txn_count
"
,
"
db_txn_duration_ms
"
,
"
db_sched_duration_ms
"
,
]
def
__init__
(
self
,
ru_utime
,
ru_stime
,
db_txn_count
,
db_txn_duration_ms
,
db_sched_duration_ms
):
self
.
ru_utime
=
ru_utime
self
.
ru_stime
=
ru_stime
self
.
db_txn_count
=
db_txn_count
self
.
db_txn_duration_ms
=
db_txn_duration_ms
self
.
db_sched_duration_ms
=
db_sched_duration_ms
@staticmethod
def
from_context
(
context
):
ru_utime
,
ru_stime
=
context
.
get_resource_usage
()
return
_RequestStats
(
ru_utime
,
ru_stime
,
context
.
db_txn_count
,
context
.
db_txn_duration_ms
,
context
.
db_sched_duration_ms
,
)
def
update
(
self
,
context
):
"""
Updates the current values and returns the difference between the
old and new values.
Returns:
_RequestStats: The difference between the old and new values
"""
new
=
_RequestStats
.
from_context
(
context
)
diff
=
_RequestStats
(
new
.
ru_utime
-
self
.
ru_utime
,
new
.
ru_stime
-
self
.
ru_stime
,
new
.
db_txn_count
-
self
.
db_txn_count
,
new
.
db_txn_duration_ms
-
self
.
db_txn_duration_ms
,
new
.
db_sched_duration_ms
-
self
.
db_sched_duration_ms
,
)
self
.
ru_utime
=
new
.
ru_utime
self
.
ru_stime
=
new
.
ru_stime
self
.
db_txn_count
=
new
.
db_txn_count
self
.
db_txn_duration_ms
=
new
.
db_txn_duration_ms
self
.
db_sched_duration_ms
=
new
.
db_sched_duration_ms
return
diff
This diff is collapsed.
Click to expand it.
synapse/http/site.py
+
3
−
1
View file @
13a8dfba
...
@@ -85,7 +85,9 @@ class SynapseRequest(Request):
...
@@ -85,7 +85,9 @@ class SynapseRequest(Request):
def
_started_processing
(
self
,
servlet_name
):
def
_started_processing
(
self
,
servlet_name
):
self
.
start_time
=
int
(
time
.
time
()
*
1000
)
self
.
start_time
=
int
(
time
.
time
()
*
1000
)
self
.
request_metrics
=
RequestMetrics
()
self
.
request_metrics
=
RequestMetrics
()
self
.
request_metrics
.
start
(
self
.
start_time
,
name
=
servlet_name
)
self
.
request_metrics
.
start
(
self
.
start_time
,
name
=
servlet_name
,
method
=
self
.
method
,
)
self
.
site
.
access_logger
.
info
(
self
.
site
.
access_logger
.
info
(
"
%s - %s - Received request: %s %s
"
,
"
%s - %s - Received request: %s %s
"
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment