我写了一个Kafka消费者,我在其中捕获类变量中的一些指标,下面是消费者的代码。
class Consumer(Thread):
def __init__(self, kafka_topic, kafka_brokers, group_id):
Thread.__init__(self)
self.consumer = KafkaConsumer(kafka_topic, bootstrap_servers=kafka_brokers, group_id=group_id)
self.metrics = Metrics()
def process_message(self, message):
msg_data = json.loads(message.value)
meeting_id = str(uuid.uuid1())
metric_response = MetricResponse(meeting_id)
#perform some task and update metric response object
self.metrics.add_metric_response(metric_response)
print('\nFinished......')
def get_metrics(self):
return self.metrics
def run(self):
print('Consumer started...')
with ThreadPoolExecutor(max_workers=10) as executor:
for message in self.consumer:
executor.map(self.process_message, (message,))
现在我已经写了一个烧瓶api来检索这个度量变量,我也在开始消费者,但是我得到了空的度量对象。 以下是api代码
app = Flask(__name__)
consumer = Consumer('my-topic', ['localhost:9092'], 'my-group')
consumer.start()
@app.route('/metrics')
def get_metrics():
metrics = consumer.get_metrics()
metrics_response = metrics.toJSON()
return Response(metrics_response, 200, mimetype='application/json')
if __name__ == '__main__':
app.run(debug=True)
我需要帮助才能从消费者类访问指标对象。我是Python的新手