9
9
gzip_encode , snappy_encode ,
10
10
lz4_encode , lz4_encode_old_kafka )
11
11
from .. import errors as Errors
12
+ from ..metrics .stats import Rate
12
13
from ..protocol .types import Int32 , Int64
13
14
from ..protocol .message import MessageSet , Message
14
15
@@ -135,7 +136,7 @@ def buffer(self):
135
136
136
137
class SimpleBufferPool (object ):
137
138
"""A simple pool of BytesIO objects with a weak memory ceiling."""
138
- def __init__ (self , memory , poolable_size ):
139
+ def __init__ (self , memory , poolable_size , metrics = None , metric_group_prefix = 'producer-metrics' ):
139
140
"""Create a new buffer pool.
140
141
141
142
Arguments:
@@ -150,10 +151,13 @@ def __init__(self, memory, poolable_size):
150
151
self ._free = collections .deque ([io .BytesIO () for _ in range (buffers )])
151
152
152
153
self ._waiters = collections .deque ()
153
- #self.metrics = metrics;
154
- #self.waitTime = this.metrics.sensor("bufferpool-wait-time");
155
- #MetricName metricName = metrics.metricName("bufferpool-wait-ratio", metricGrpName, "The fraction of time an appender waits for space allocation.");
156
- #this.waitTime.add(metricName, new Rate(TimeUnit.NANOSECONDS));
154
+ self .wait_time = None
155
+ if metrics :
156
+ self .wait_time = metrics .sensor ('bufferpool-wait-time' )
157
+ self .wait_time .add (metrics .metric_name (
158
+ 'bufferpool-wait-ratio' , metric_group_prefix ,
159
+ 'The fraction of time an appender waits for space allocation.' ),
160
+ Rate ())
157
161
158
162
def allocate (self , size , max_time_to_block_ms ):
159
163
"""
@@ -187,7 +191,8 @@ def allocate(self, size, max_time_to_block_ms):
187
191
start_wait = time .time ()
188
192
more_memory .wait (max_time_to_block_ms / 1000.0 )
189
193
end_wait = time .time ()
190
- #this.waitTime.record(endWait - startWait, time.milliseconds());
194
+ if self .wait_time :
195
+ self .wait_time .record (end_wait - start_wait )
191
196
192
197
if self ._free :
193
198
buf = self ._free .popleft ()
0 commit comments