@@ -56,12 +56,13 @@ class Fetcher(six.Iterator):
5656 'max_partition_fetch_bytes' : 1048576 ,
5757 'max_poll_records' : sys .maxsize ,
5858 'check_crcs' : True ,
59+ 'metrics' : None ,
5960 'metric_group_prefix' : 'consumer' ,
6061 'retry_backoff_ms' : 100 ,
6162 'enable_incremental_fetch_sessions' : True ,
6263 }
6364
64- def __init__ (self , client , subscriptions , metrics , ** configs ):
65+ def __init__ (self , client , subscriptions , ** configs ):
6566 """Initialize a Kafka Message Fetcher.
6667
6768 Keyword Arguments:
@@ -111,7 +112,10 @@ def __init__(self, client, subscriptions, metrics, **configs):
111112 self ._next_partition_records = None # Holds a single PartitionRecords until fully consumed
112113 self ._iterator = None
113114 self ._fetch_futures = collections .deque ()
114- self ._sensors = FetchManagerMetrics (metrics , self .config ['metric_group_prefix' ])
115+ if self .config ['metrics' ]:
116+ self ._sensors = FetchManagerMetrics (self .config ['metrics' ], self .config ['metric_group_prefix' ])
117+ else :
118+ self ._sensors = None
115119 self ._isolation_level = READ_UNCOMMITTED
116120 self ._session_handlers = {}
117121 self ._nodes_with_pending_fetch_requests = set ()
@@ -391,7 +395,7 @@ def _append(self, drained, part, max_records, update_offsets):
391395 # when each message is yielded). There may be edge cases where we re-fetch records
392396 # that we'll end up skipping, but for now we'll live with that.
393397 highwater = self ._subscriptions .assignment [tp ].highwater
394- if highwater is not None :
398+ if highwater is not None and self . _sensors :
395399 self ._sensors .records_fetch_lag .record (highwater - part .next_fetch_offset )
396400 if update_offsets or not part_records :
397401 # TODO: save leader_epoch
@@ -705,7 +709,10 @@ def _handle_fetch_response(self, node_id, fetch_offsets, send_time, response):
705709 partitions = set ([TopicPartition (topic , partition_data [0 ])
706710 for topic , partitions in response .topics
707711 for partition_data in partitions ])
708- metric_aggregator = FetchResponseMetricAggregator (self ._sensors , partitions )
712+ if self ._sensors :
713+ metric_aggregator = FetchResponseMetricAggregator (self ._sensors , partitions )
714+ else :
715+ metric_aggregator = None
709716
710717 for topic , partitions in response .topics :
711718 for partition_data in partitions :
@@ -719,7 +726,8 @@ def _handle_fetch_response(self, node_id, fetch_offsets, send_time, response):
719726 )
720727 self ._completed_fetches .append (completed_fetch )
721728
722- self ._sensors .fetch_latency .record ((time .time () - send_time ) * 1000 )
729+ if self ._sensors :
730+ self ._sensors .fetch_latency .record ((time .time () - send_time ) * 1000 )
723731 self ._nodes_with_pending_fetch_requests .remove (node_id )
724732
725733 def _handle_fetch_error (self , node_id , exception ):
@@ -816,7 +824,7 @@ def _parse_fetched_data(self, completed_fetch):
816824 raise error_type ('Unexpected error while fetching data' )
817825
818826 finally :
819- if parsed_records is None :
827+ if parsed_records is None and completed_fetch . metric_aggregator :
820828 completed_fetch .metric_aggregator .record (tp , 0 , 0 )
821829
822830 if error_type is not Errors .NoError :
@@ -873,7 +881,8 @@ def __bool__(self):
873881 def drain (self ):
874882 if self .record_iterator is not None :
875883 self .record_iterator = None
876- self .metric_aggregator .record (self .topic_partition , self .bytes_read , self .records_read )
884+ if self .metric_aggregator :
885+ self .metric_aggregator .record (self .topic_partition , self .bytes_read , self .records_read )
877886 self .on_drain (self )
878887
879888 def take (self , n = None ):
0 commit comments