Skip to content

Commit 6b9076b

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

File tree

16 files changed

+40
-1
lines changed

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

0 commit comments

Comments
 (0)