[英]Tracking topic size and consumer lag with kafka 0.8.2.0
自 kafka 0.8.2.0 以來,跟蹤消費者滯后和主題大小似乎變得非常困難
您如何在 kafka 中跟蹤偏移量(主題大小)和滯后? 當您的生產者插入一條消息時,您是否在某處增加一個計數器,並在您的消費者確認一條消息時增加另一個計數器?
我正在使用airbnb 的 kafka-statsd-metrics2 - 但由於某種原因,所有關於主題大小的指標總是0
,這可能是他們的錯誤報告,但你怎么做?
我們的消費者和生產者是使用kafka-python用 python 編寫的,他們聲明他們不支持 ConsumerCoordinator 偏移 API,所以我整理了一個解決方案,查詢 zookeeper 並將這些指標發送到 statsd 實例(看起來很尷尬),但我'我仍然缺少主題大小指標。
我們正在使用 collectd 來收集系統指標,我沒有使用 JMX 的經驗,並且在 collectd 中配置它似乎很復雜,我已經嘗試了幾次,所以我找到了一些不這樣做的方法。
如果您有任何意見,我很樂意聽到,即使是:“這屬於 x stackexchange-site”
如果我理解正確,您可以使用HighwaterMarkOffset
中的FetchResponse
。 通過這種方式,您將知道分區末尾的偏移量是多少,並且能夠將其與當前確認的偏移量或此FetchResponse
最后一條消息的偏移量進行比較。
詳情在這里
您是否嘗試過使用https://github.com/quantifind/KafkaOffsetMonitor來監控消費者滯后。 它適用於 0.8.2.0
這是代碼片段,確保在活動控制器中運行它。 BOOTSTRAP_SERVERS 是活動控制器 IP。
client = KafkaAdminClient(bootstrap_servers=BOOTSTRAP_SERVERS, request_timeout_ms=300)
list_groups_request = client.list_consumer_groups()
for group in list_groups_request:
if group[1] == 'consumer':
list_mebers_in_groups = client.describe_consumer_groups([group[0]])
(error_code, group_id, state, protocol_type, protocol, members) = list_mebers_in_groups[0]
if len(members) !=0:
for member in members:
(member_id, client_id, client_host, member_metadata, member_assignment) = member
member_topics_assignment = []
for (topic, partitions) in MemberAssignment.decode(member_assignment).assignment:
member_topics_assignment.append(topic)
for topic in member_topics_assignment:
consumer = KafkaConsumer(
bootstrap_servers=BOOTSTRAP_SERVERS,
group_id=group[0],
enable_auto_commit=False
)
consumer.topics()
for p in consumer.partitions_for_topic(topic):
tp = TopicPartition(topic, p)
consumer.assign([tp])
committed = consumer.committed(tp)
consumer.seek_to_end(tp)
last_offset = consumer.position(tp)
if last_offset != None and committed != None:
lag = last_offset - committed
print "group: {} topic:{} partition: {} lag: {}".format(group[0], topic, p, lag)
有人能說 MemberAssignment 是從哪個模塊導入的嗎?
對於 MemberAssignment.decode(member_assignment).assignment 中的(主題,分區):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.