Browse Source

ref: maxminddb module is always available, GEOIP_PATH_MMDB attr always present (#69902)

<!-- Describe your PR here. -->
anthony sottile 10 months ago
parent
commit
7c48603fa4
1 changed files with 8 additions and 13 deletions
  1. 8 13
      src/sentry/utils/geo.py

+ 8 - 13
src/sentry/utils/geo.py

@@ -7,7 +7,6 @@ from django.conf import settings
 from sentry_relay.processing import GeoIpLookup
 
 logger = logging.getLogger(__name__)
-geoip_path_mmdb = getattr(settings, "GEOIP_PATH_MMDB", None)
 
 
 # default is no-op
@@ -20,19 +19,15 @@ rust_geoip: None | GeoIpLookup = None
 
 def _init_geoip() -> None:
     global geo_by_addr
-    try:
-        import maxminddb
-    except ImportError:
-        logger.warning("maxminddb module not available.")
-        return
 
-    if geoip_path_mmdb is None:
-        return
+    import maxminddb
+
+    assert settings.GEOIP_PATH_MMDB is not None  # checked below
 
     try:
-        geo_db = maxminddb.open_database(geoip_path_mmdb, maxminddb.MODE_AUTO)
+        geo_db = maxminddb.open_database(settings.GEOIP_PATH_MMDB, maxminddb.MODE_AUTO)
     except Exception:
-        logger.warning("Error opening GeoIP database: %s", geoip_path_mmdb)
+        logger.warning("Error opening GeoIP database: %s", settings.GEOIP_PATH_MMDB)
         return
 
     def _geo_by_addr(ip: str) -> dict[str, Any] | None:
@@ -59,11 +54,11 @@ def _init_geoip_rust():
     from sentry_relay.processing import GeoIpLookup
 
     try:
-        rust_geoip = GeoIpLookup.from_path(geoip_path_mmdb)
+        rust_geoip = GeoIpLookup.from_path(settings.GEOIP_PATH_MMDB)
     except Exception:
-        logger.warning("Error opening GeoIP database in Rust: %s", geoip_path_mmdb)
+        logger.warning("Error opening GeoIP database in Rust: %s", settings.GEOIP_PATH_MMDB)
 
 
-if geoip_path_mmdb:
+if settings.GEOIP_PATH_MMDB:
     _init_geoip()
     _init_geoip_rust()