Skip to content

Commit 6b9076b

Browse files
authored
Use __slots__ for metrics (#2583)
1 parent c52f25a commit 6b9076b

16 files changed

+40
-1
lines changed

kafka/metrics/compound_stat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def stats(self):
2121

2222

2323
class NamedMeasurable(object):
24+
__slots__ = ('_name', '_stat')
25+
2426
def __init__(self, metric_name, measurable_stat):
2527
self._name = metric_name
2628
self._stat = measurable_stat

kafka/metrics/kafka_metric.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
class KafkaMetric(object):
7+
__slots__ = ('_metric_name', '_measurable', '_config')
8+
79
# NOTE java constructor takes a lock instance
810
def __init__(self, metric_name, measurable, config):
911
if not metric_name:
@@ -33,4 +35,4 @@ def config(self, config):
3335
def value(self, time_ms=None):
3436
if time_ms is None:
3537
time_ms = time.time() * 1000
36-
return self.measurable.measure(self.config, time_ms)
38+
return self._measurable.measure(self._config, time_ms)

kafka/metrics/metric_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
class MetricConfig(object):
77
"""Configuration values for metrics"""
8+
__slots__ = ('quota', '_samples', 'event_window', 'time_window_ms', 'tags')
9+
810
def __init__(self, quota=None, samples=2, event_window=sys.maxsize,
911
time_window_ms=30 * 1000, tags=None):
1012
"""

kafka/metrics/metric_name.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class MetricName(object):
3838
# as messages are sent we record the sizes
3939
sensor.record(message_size)
4040
"""
41+
__slots__ = ('_name', '_group', '_description', '_tags', '_hash')
4142

4243
def __init__(self, name, group, description=None, tags=None):
4344
"""

kafka/metrics/quota.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
class Quota(object):
55
"""An upper or lower bound for metrics"""
6+
__slots__ = ('_bound', '_upper')
7+
68
def __init__(self, bound, is_upper):
79
self._bound = bound
810
self._upper = is_upper

kafka/metrics/stats/avg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Avg(AbstractSampledStat):
77
"""
88
An AbstractSampledStat that maintains a simple average over its samples.
99
"""
10+
__slots__ = ('_initial_value', '_samples', '_current')
11+
1012
def __init__(self):
1113
super(Avg, self).__init__(0.0)
1214

kafka/metrics/stats/count.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Count(AbstractSampledStat):
77
"""
88
An AbstractSampledStat that maintains a simple count of what it has seen.
99
"""
10+
__slots__ = ('_initial_value', '_samples', '_current')
11+
1012
def __init__(self):
1113
super(Count, self).__init__(0.0)
1214

kafka/metrics/stats/histogram.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
class Histogram(object):
7+
__slots__ = ('_hist', '_count', '_bin_scheme')
8+
79
def __init__(self, bin_scheme):
810
self._hist = [0.0] * bin_scheme.bins
911
self._count = 0.0
@@ -40,6 +42,8 @@ def __str__(self):
4042
return '{%s}' % ','.join(values)
4143

4244
class ConstantBinScheme(object):
45+
__slots__ = ('_min', '_max', '_bins', '_bucket_width')
46+
4347
def __init__(self, bins, min_val, max_val):
4448
if bins < 2:
4549
raise ValueError('Must have at least 2 bins.')
@@ -69,6 +73,8 @@ def to_bin(self, x):
6973
return int(((x - self._min) / self._bucket_width) + 1)
7074

7175
class LinearBinScheme(object):
76+
__slots__ = ('_bins', '_max', '_scale')
77+
7278
def __init__(self, num_bins, max_val):
7379
self._bins = num_bins
7480
self._max = max_val

kafka/metrics/stats/max_stat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
class Max(AbstractSampledStat):
77
"""An AbstractSampledStat that gives the max over its samples."""
8+
__slots__ = ('_initial_value', '_samples', '_current')
9+
810
def __init__(self):
911
super(Max, self).__init__(float('-inf'))
1012

kafka/metrics/stats/min_stat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class Min(AbstractSampledStat):
99
"""An AbstractSampledStat that gives the min over its samples."""
10+
__slots__ = ('_initial_value', '_samples', '_current')
11+
1012
def __init__(self):
1113
super(Min, self).__init__(float(sys.maxsize))
1214

kafka/metrics/stats/percentile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
class Percentile(object):
5+
__slots__ = ('_metric_name', '_percentile')
6+
57
def __init__(self, metric_name, percentile):
68
self._metric_name = metric_name
79
self._percentile = float(percentile)

kafka/metrics/stats/percentiles.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class BucketSizing(object):
1313

1414
class Percentiles(AbstractSampledStat, AbstractCompoundStat):
1515
"""A compound stat that reports one or more percentiles"""
16+
__slots__ = ('_initial_value', '_samples', '_current',
17+
'_percentiles', '_buckets', '_bin_scheme')
18+
1619
def __init__(self, size_in_bytes, bucketing, max_val, min_val=0.0,
1720
percentiles=None):
1821
super(Percentiles, self).__init__(0.0)

kafka/metrics/stats/rate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Rate(AbstractMeasurableStat):
3737
occurrences (e.g. the count of values measured over the time interval)
3838
or other such values.
3939
"""
40+
__slots__ = ('_stat', '_unit')
41+
4042
def __init__(self, time_unit=TimeUnit.SECONDS, sampled_stat=None):
4143
self._stat = sampled_stat or SampledTotal()
4244
self._unit = time_unit
@@ -105,6 +107,7 @@ def convert(self, time_ms):
105107

106108

107109
class SampledTotal(AbstractSampledStat):
110+
__slots__ = ('_initial_value', '_samples', '_current')
108111
def __init__(self, initial_value=None):
109112
if initial_value is not None:
110113
raise ValueError('initial_value cannot be set on SampledTotal')

kafka/metrics/stats/sampled_stat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class AbstractSampledStat(AbstractMeasurableStat):
2222
Subclasses of this class define different statistics measured
2323
using this basic pattern.
2424
"""
25+
__slots__ = ('_initial_value', '_samples', '_current')
26+
2527
def __init__(self, initial_value):
2628
self._initial_value = initial_value
2729
self._samples = []

kafka/metrics/stats/sensor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class Sensor(object):
1515
the `record(double)` api and would maintain a set
1616
of metrics about request sizes such as the average or max.
1717
"""
18+
__slots__ = ('_lock', '_registry', '_name', '_parents', '_metrics',
19+
'_stats', '_config', '_inactive_sensor_expiration_time_ms',
20+
'_last_record_time')
21+
1822
def __init__(self, registry, name, parents, config,
1923
inactive_sensor_expiration_time_seconds):
2024
if not name:

kafka/metrics/stats/total.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
class Total(AbstractMeasurableStat):
77
"""An un-windowed cumulative total maintained over all time."""
8+
__slots__ = ('_total')
9+
810
def __init__(self, value=0.0):
911
self._total = value
1012

0 commit comments

Comments
 (0)