How to get ip address?
According to Get the user IP address and Symfony HttpFoundation you can get visitor IP by 

first add use Symfony\Component\HttpFoundation; then 

 \Drupal::request()-&gt;getClientIp()