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
c1cdd795
Commit
c1cdd795
authored
10 years ago
by
Paul "LeoNerd" Evans
Browse files
Options
Downloads
Patches
Plain Diff
Add an .inc_by() method to CounterMetric; implement DistributionMetric a neater way
parent
63cb7ece
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/metrics/metric.py
+14
-23
14 additions, 23 deletions
synapse/metrics/metric.py
tests/metrics/test_metric.py
+2
-3
2 additions, 3 deletions
tests/metrics/test_metric.py
with
16 additions
and
26 deletions
synapse/metrics/metric.py
+
14
−
23
View file @
c1cdd795
...
@@ -64,7 +64,7 @@ class CounterMetric(BaseMetric):
...
@@ -64,7 +64,7 @@ class CounterMetric(BaseMetric):
if
self
.
is_scalar
():
if
self
.
is_scalar
():
self
.
counts
[()]
=
0
self
.
counts
[()]
=
0
def
inc
(
self
,
*
values
):
def
inc
_by
(
self
,
incr
,
*
values
):
if
len
(
values
)
!=
self
.
dimension
():
if
len
(
values
)
!=
self
.
dimension
():
raise
ValueError
(
"
Expected as many values to inc() as labels (%d)
"
%
raise
ValueError
(
"
Expected as many values to inc() as labels (%d)
"
%
(
self
.
dimension
())
(
self
.
dimension
())
...
@@ -73,9 +73,12 @@ class CounterMetric(BaseMetric):
...
@@ -73,9 +73,12 @@ class CounterMetric(BaseMetric):
# TODO: should assert that the tag values are all strings
# TODO: should assert that the tag values are all strings
if
values
not
in
self
.
counts
:
if
values
not
in
self
.
counts
:
self
.
counts
[
values
]
=
1
self
.
counts
[
values
]
=
incr
else
:
else
:
self
.
counts
[
values
]
+=
1
self
.
counts
[
values
]
+=
incr
def
inc
(
self
,
*
values
):
self
.
inc_by
(
1
,
*
values
)
def
render_item
(
self
,
k
):
def
render_item
(
self
,
k
):
return
[
"
%s%s %d
"
%
(
self
.
name
,
self
.
_render_key
(
k
),
self
.
counts
[
k
])]
return
[
"
%s%s %d
"
%
(
self
.
name
,
self
.
_render_key
(
k
),
self
.
counts
[
k
])]
...
@@ -101,7 +104,7 @@ class CallbackMetric(BaseMetric):
...
@@ -101,7 +104,7 @@ class CallbackMetric(BaseMetric):
for
k
in
sorted
(
value
.
keys
())]
for
k
in
sorted
(
value
.
keys
())]
class
DistributionMetric
(
CounterMetric
):
class
DistributionMetric
(
object
):
"""
A combination of an event counter and an accumulator, which counts
"""
A combination of an event counter and an accumulator, which counts
both the number of events and accumulates the total value. Typically this
both the number of events and accumulates the total value. Typically this
could be used to keep track of method-running times, or other distributions
could be used to keep track of method-running times, or other distributions
...
@@ -110,28 +113,16 @@ class DistributionMetric(CounterMetric):
...
@@ -110,28 +113,16 @@ class DistributionMetric(CounterMetric):
TODO(paul): Try to export some heatmap-style stats?
TODO(paul): Try to export some heatmap-style stats?
"""
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
name
,
*
args
,
**
kwargs
):
super
(
DistributionMetric
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
counts
=
CounterMetric
(
name
+
"
:count
"
,
**
kwargs
)
self
.
totals
=
CounterMetric
(
name
+
"
:total
"
,
**
kwargs
)
self
.
totals
=
{}
# Scalar metrics are never empty
if
self
.
is_scalar
():
self
.
totals
[()]
=
0
def
inc_by
(
self
,
inc
,
*
values
):
def
inc_by
(
self
,
inc
,
*
values
):
self
.
inc
(
*
values
)
self
.
counts
.
inc
(
*
values
)
self
.
totals
.
inc_by
(
inc
,
*
values
)
if
values
not
in
self
.
totals
:
self
.
totals
[
values
]
=
inc
else
:
self
.
totals
[
values
]
+=
inc
def
render_item
(
self
,
k
):
def
render
(
self
):
keystr
=
self
.
_render_key
(
k
)
return
self
.
counts
.
render
()
+
self
.
totals
.
render
()
return
[
"
%s:count%s %d
"
%
(
self
.
name
,
keystr
,
self
.
counts
[
k
]),
"
%s:total%s %d
"
%
(
self
.
name
,
keystr
,
self
.
totals
[
k
])]
class
CacheMetric
(
object
):
class
CacheMetric
(
object
):
...
...
This diff is collapsed.
Click to expand it.
tests/metrics/test_metric.py
+
2
−
3
View file @
c1cdd795
...
@@ -35,8 +35,7 @@ class CounterMetricTestCase(unittest.TestCase):
...
@@ -35,8 +35,7 @@ class CounterMetricTestCase(unittest.TestCase):
'
scalar 1
'
,
'
scalar 1
'
,
])
])
counter
.
inc
()
counter
.
inc_by
(
2
)
counter
.
inc
()
self
.
assertEquals
(
counter
.
render
(),
[
self
.
assertEquals
(
counter
.
render
(),
[
'
scalar 3
'
'
scalar 3
'
...
@@ -125,8 +124,8 @@ class DistributionMetricTestCase(unittest.TestCase):
...
@@ -125,8 +124,8 @@ class DistributionMetricTestCase(unittest.TestCase):
self
.
assertEquals
(
metric
.
render
(),
[
self
.
assertEquals
(
metric
.
render
(),
[
'
queries:count{verb=
"
INSERT
"
} 1
'
,
'
queries:count{verb=
"
INSERT
"
} 1
'
,
'
queries:total{verb=
"
INSERT
"
} 800
'
,
'
queries:count{verb=
"
SELECT
"
} 2
'
,
'
queries:count{verb=
"
SELECT
"
} 2
'
,
'
queries:total{verb=
"
INSERT
"
} 800
'
,
'
queries:total{verb=
"
SELECT
"
} 500
'
,
'
queries:total{verb=
"
SELECT
"
} 500
'
,
])
])
...
...
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