Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
ba700074
Commit
ba700074
authored
4 years ago
by
Richard van der Hoff
Browse files
Options
Downloads
Patches
Plain Diff
Expose a `get_resource_usage` method in `Measure`
parent
937393ab
Loading
Loading
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/util/metrics.py
+21
-10
21 additions, 10 deletions
synapse/util/metrics.py
with
21 additions
and
10 deletions
synapse/util/metrics.py
+
21
−
10
View file @
ba700074
...
...
@@ -19,7 +19,11 @@ from typing import Any, Callable, Optional, TypeVar, cast
from
prometheus_client
import
Counter
from
synapse.logging.context
import
LoggingContext
,
current_context
from
synapse.logging.context
import
(
ContextResourceUsage
,
LoggingContext
,
current_context
,
)
from
synapse.metrics
import
InFlightGauge
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -104,27 +108,27 @@ class Measure:
def
__init__
(
self
,
clock
,
name
):
self
.
clock
=
clock
self
.
name
=
name
self
.
_logging_context
=
None
parent_context
=
current_context
()
self
.
_logging_context
=
LoggingContext
(
"
Measure[%s]
"
%
(
self
.
name
,),
parent_context
)
self
.
start
=
None
def
__enter__
(
self
):
if
self
.
_logging_context
:
def
__enter__
(
self
)
->
"
Measure
"
:
if
self
.
start
is
not
None
:
raise
RuntimeError
(
"
Measure() objects cannot be re-used
"
)
self
.
start
=
self
.
clock
.
time
()
parent_context
=
current_context
()
self
.
_logging_context
=
LoggingContext
(
"
Measure[%s]
"
%
(
self
.
name
,),
parent_context
)
self
.
_logging_context
.
__enter__
()
in_flight
.
register
((
self
.
name
,),
self
.
_update_in_flight
)
return
self
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
if
not
self
.
_logging_context
:
if
self
.
start
is
None
:
raise
RuntimeError
(
"
Measure() block exited without being entered
"
)
duration
=
self
.
clock
.
time
()
-
self
.
start
usage
=
self
.
_logging_context
.
get_resource_usage
()
usage
=
self
.
get_resource_usage
()
in_flight
.
unregister
((
self
.
name
,),
self
.
_update_in_flight
)
self
.
_logging_context
.
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
...
...
@@ -140,6 +144,13 @@ class Measure:
except
ValueError
:
logger
.
warning
(
"
Failed to save metrics! Usage: %s
"
,
usage
)
def
get_resource_usage
(
self
)
->
ContextResourceUsage
:
"""
Get the resources used within this Measure block
If the Measure block is still active, returns the resource usage so far.
"""
return
self
.
_logging_context
.
get_resource_usage
()
def
_update_in_flight
(
self
,
metrics
):
"""
Gets called when processing in flight metrics
"""
...
...
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