code: def statsd_middleware_factory(app, handler):
      
      @coroutine
      def middleware(request):
          """Send stats to statsd."""
          timer = Timer()
          timer.start()
  
          statsd = yield from app.ps.metrics.client()
          pipe = statsd.pipe()
          pipe.incr('request.method.%s' % request.method)
  
          try:
              response = yield from handler(request)
              pipe.incr('response.status.%s' % response.status)
              return response
  
          except HTTPException as exc:
              pipe.incr('response.status.%s' % exc.status_code)
              raise
  
          except Exception:
              pipe.incr('response.exception')
              raise
  
          finally:
              timer.stop()
              pipe.timing('response.time', timer.ms)
              pipe.disconnect()
  
      return middleware
docs: Send the application stats to statsd.