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
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
No related branches found
No related tags found
No related merge requests found
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
...
@@ -19,7 +19,11 @@ from typing import Any, Callable, Optional, TypeVar, cast
from
prometheus_client
import
Counter
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
from
synapse.metrics
import
InFlightGauge
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
...
@@ -104,27 +108,27 @@ class Measure:
...
@@ -104,27 +108,27 @@ class Measure:
def
__init__
(
self
,
clock
,
name
):
def
__init__
(
self
,
clock
,
name
):
self
.
clock
=
clock
self
.
clock
=
clock
self
.
name
=
name
self
.
name
=
name
self
.
_logging_context
=
None
parent_context
=
current_context
()
self
.
_logging_context
=
LoggingContext
(
"
Measure[%s]
"
%
(
self
.
name
,),
parent_context
)
self
.
start
=
None
self
.
start
=
None
def
__enter__
(
self
):
def
__enter__
(
self
)
->
"
Measure
"
:
if
self
.
_logging_context
:
if
self
.
start
is
not
None
:
raise
RuntimeError
(
"
Measure() objects cannot be re-used
"
)
raise
RuntimeError
(
"
Measure() objects cannot be re-used
"
)
self
.
start
=
self
.
clock
.
time
()
self
.
start
=
self
.
clock
.
time
()
parent_context
=
current_context
()
self
.
_logging_context
=
LoggingContext
(
"
Measure[%s]
"
%
(
self
.
name
,),
parent_context
)
self
.
_logging_context
.
__enter__
()
self
.
_logging_context
.
__enter__
()
in_flight
.
register
((
self
.
name
,),
self
.
_update_in_flight
)
in_flight
.
register
((
self
.
name
,),
self
.
_update_in_flight
)
return
self
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
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
"
)
raise
RuntimeError
(
"
Measure() block exited without being entered
"
)
duration
=
self
.
clock
.
time
()
-
self
.
start
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
)
in_flight
.
unregister
((
self
.
name
,),
self
.
_update_in_flight
)
self
.
_logging_context
.
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
self
.
_logging_context
.
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
...
@@ -140,6 +144,13 @@ class Measure:
...
@@ -140,6 +144,13 @@ class Measure:
except
ValueError
:
except
ValueError
:
logger
.
warning
(
"
Failed to save metrics! Usage: %s
"
,
usage
)
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
):
def
_update_in_flight
(
self
,
metrics
):
"""
Gets called when processing in flight 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