Source:
public void run() { try { Logging.putPrefix(MessageConsumerManager.LOG_PREFIX); consumer.subscribe(Arrays.asList(KafkaOffsetConstants.OFFSETS_TOPIC)); LOGGER.info("Connected to Kafka consumer offset topic"); Schema schema = new Schema(new Field("group", Schema.STRING), new Field(KafkaOffsetConstants.TOPIC, Schema.STRING), new Field("partition", Schema.INT32)); while (!closed.get()) { ConsumerRecords<byte[], byte[]> records = consumer.poll(KafkaOffsetConstants.POLL_INTERVAL); for (ConsumerRecord<byte[], byte[]> consumerRecord : records) { if (consumerRecord.value() != null && consumerRecord.key() != null) { ByteBuffer key = ByteBuffer.wrap(consumerRecord.key()); short version = key.getShort(); if (version < 2) { try { Struct struct = (Struct) schema.read(key); String group = struct.getString(KafkaOffsetConstants.GROUP); if ( !group.equals(SystemInfoUtils.getInstanceId())) { continue; } String topic = struct.getString(KafkaOffsetConstants.TOPIC); int partition = struct.getInt(KafkaOffsetConstants.PARTITION); SimpleConsumer con = getConsumer(); long realOffset = getLastOffset(con, struct.getString(KafkaOffsetConstants.TOPIC), partition, -1); long consumerOffset = readOffsetMessageValue( ByteBuffer.wrap(consumerRecord.value())); long lag = realOffset - consumerOffset; KafkaOffset mon = new KafkaOffset(group, topic, partition, realOffset, consumerOffset, lag); LOGGER.info("group : , topic: {}:{} , offsets : {}-{}-{}", group, topic, partition, consumerOffset, realOffset, lag); Map<String, KafkaOffset> map = consumerOffsetMap.get(topic); if (map == null) { map = new ConcurrentHashMap<>(); consumerOffsetMap.put(topic, map); } map.put(group + "%" + partition, mon); } catch (Exception e) { LOGGER.error("Exception while getting offset", e); } } } } } } catch (Exception e) { LOGGER.error("Exception while getting offset", e); } finally { consumer.close(); } }

Target:
public void run() { try { Logging.putPrefix(MessageConsumerManager.LOG_PREFIX); consumer.subscribe(Arrays.asList(KafkaOffsetConstants.OFFSETS_TOPIC)); LOGGER.info("Connected to Kafka consumer offset topic"); Schema schema = new Schema(new Field("group", Schema.STRING), new Field(KafkaOffsetConstants.TOPIC, Schema.STRING), new Field("partition", Schema.INT32)); while (!closed.get()) { ConsumerRecords<byte[], byte[]> records = consumer.poll(KafkaOffsetConstants.POLL_INTERVAL); for (ConsumerRecord<byte[], byte[]> consumerRecord : records) { if (consumerRecord.value() != null && consumerRecord.key() != null) { ByteBuffer key = ByteBuffer.wrap(consumerRecord.key()); short version = key.getShort(); if (version < 2) { try { Struct struct = (Struct) schema.read(key); String group = struct.getString(KafkaOffsetConstants.GROUP); if ( !group.equals(SystemInfoUtils.getInstanceId())) { continue; } String topic = struct.getString(KafkaOffsetConstants.TOPIC); int partition = struct.getInt(KafkaOffsetConstants.PARTITION); SimpleConsumer con = getConsumer(); long realOffset = getLastOffset(con, struct.getString(KafkaOffsetConstants.TOPIC), partition, -1); long consumerOffset = readOffsetMessageValue( ByteBuffer.wrap(consumerRecord.value())); long lag = realOffset - consumerOffset; KafkaOffset mon = new KafkaOffset(group, topic, partition, realOffset, consumerOffset, lag); LOGGER.debug("group : , topic: {}:{} , offsets : {}-{}-{}", group, topic, partition, consumerOffset, realOffset, lag); Map<String, KafkaOffset> map = consumerOffsetMap.get(topic); if (map == null) { map = new ConcurrentHashMap<>(); consumerOffsetMap.put(topic, map); } map.put(group + "%" + partition, mon); } catch (Exception e) { LOGGER.error("Exception while getting offset", e); } } } } } } catch (Exception e) { LOGGER.error("Exception while getting offset", e); } finally { consumer.close(); } }