I have a script that is extracting data from an internal service and storing the count of data types in memory like this:
metrics = {
"new_accounts": 152,
"acquisitions": 2005,
...
}
That is being done in memory and that whole process is kicked off by the subscribe method in this script:
if __name__ == "__main__":
loop = asyncio.get_event_loop()
for signal in [signal.SIGHUP, signal.SIGTERM, signal.SIGINT]:
loop.add_signal_handler(
signal, lambda s=signal: asyncio.create_task(close_subscriptions(s, loop)))
for subscription in SUBSCRIPTION_TYPES:
loop.create_task(subscribe(subscription))
loop.run_forever()
I want to expose that metrics data through an endpoint in flask for monitoring purposes. In a thread, can I start the flask app in this script like this:
if __name__ == "__main__":
...
for subscription in SUBSCRIPTION_TYPES:
loop.create_task(subscribe(subscription))
...
loop.run_forever()
Does this make sense? I already have the flask app running locally but it doesn't have access to this metrics data. Is this the way I need to go about this?
解决方案
Try to put Your metric to Queue:
Flask endpoint can read all data from Queue, and expose last value as endpoint data
Cheers, Fenrir