Skip to content

Commit

Permalink
Handle AddressNotFoundError in nodemapper.py (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-F-Helm authored May 15, 2024
1 parent e4c9eaf commit fd41033
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions dockerfiles/nodemapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import requests
import geoip2.database
from geoip2.errors import AddressNotFoundError
from flask import Flask, make_response


Expand All @@ -24,8 +25,11 @@

def get_geoip(ip):
"""Takes an IP address and determines GeoIP data"""
with geoip2.database.Reader("./geoip.mmdb") as reader:
return reader.city(ip)
try:
with geoip2.database.Reader("./geoip.mmdb") as reader:
return reader.city(ip)
except AddressNotFoundError:
return None


@app.route("/metrics")
Expand All @@ -35,6 +39,9 @@ def nodes():
peer_list = requests.get(f'http://{NODE_HOST}:{NODE_PORT}/get_peer_list').json()
def add_peer(host, status):
geo = get_geoip(host)
if geo is None:
return

geostr = 'geoip{{latitude="{lat}", longitude="{lon}", country_code="{country_code}", country_name="{country_name}", status="{status}"}} 1'
if geostr not in peers:
if 'en' in geo.continent.names:
Expand All @@ -54,4 +61,4 @@ def add_peer(host, status):
data = '\n'.join(peers)
response = make_response(data, 200)
response.mimetype = "text/plain"
return response
return response

0 comments on commit fd41033

Please sign in to comment.