Multi Workers Sanic App And pyformance Lib for Metrics Reporting

428 Views Asked by At

I am trying to add metrics to sanic based application using pyformance metrics library.

import random
import time

from sanic import Sanic
from sanic.response import json

from pyformance import MetricsRegistry
from pyformance.reporters.carbon_reporter import CarbonReporter

registry = MetricsRegistry()
__reporter__ = CarbonReporter(registry=registry,
                          reporting_interval=10,
                          prefix='sanic',
                          server='localhost',
                          port=2003)
__reporter__.start()
app = Sanic()

@app.route("/api/v1/foo", methods=["POST"])
   def foo(request):

      timer = registry.timer(".foo")
         with timer.time():
         time.sleep(random.randint(1, 2))
         return json({"status": True})

if __name__ == "__main__":
     app.run(host='0.0.0.0', port=8080, workers=1, access_log=False, 
     debug=False)

When running 1 worker everything looks fine, but when configuring more workers no metrics being sent to carbon.

Any help is appreciated and also different approaches sending metrics to graphite from multi-workers sanic application.

1

There are 1 best solutions below

0
On

So multiple workers would basically subprocess the workers, might be something with IPC back to main process.

Try running multiple workers according to this and see if the same behaviour persists? How to configure ExecStart for Gunicorn without WSGI?

Here gunicorn manages the processes.