mask last octet from IP address for metrics

This commit is contained in:
Mark Hammond 2011-04-18 12:09:29 +10:00
Родитель 98be22b291
Коммит 6ae1f97bcf
1 изменённых файлов: 13 добавлений и 3 удалений

Просмотреть файл

@ -62,6 +62,7 @@ class TimedMetricsCollector(object):
# more sophisticated or more options...
import logging
log = logging.getLogger('linkdrop-metrics')
errlog = logging.getLogger('linkdrop')
class F1MetricsConsumer(MetricsConsumer):
@ -82,9 +83,18 @@ class F1MetricsCollector(MetricsCollector):
remote_address = distinct_ob.environ['HTTP_X_FORWARDED_FOR']
except KeyError:
remote_address = distinct_ob.environ.get("REMOTE_ADDR")
return {
'remote_address': remote_address,
}
# discard the last octet of the IP address. Will almost certainly
# need work for IPv6 :)
result = {}
try:
remote_address = ".".join(remote_address.split(".", 3)[:-1]) + ".0"
except (ValueError, IndexError), exc:
errlog.error("failed to anonymize remote address %r: %s",
remote_address, exc)
else:
result['remote_address'] = remote_address
return result
# the object used by the code.
metrics = F1MetricsCollector(F1MetricsConsumer())