From 92bfaa630eeee04c8b368df812380f09bcfa455b Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 4 Jul 2024 11:27:42 +0200 Subject: [PATCH] Fix statsd client on urls that aren't ascii encodable Fixes https://sentry.galaxyproject.org/share/issue/e2c535d2d4904d438a0a1524c991bba4/: ``` UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-16: ordinal not in range(128) File "galaxy/web/framework/middleware/error.py", line 167, in __call__ app_iter = self.application(environ, sr_checker) File "galaxy/web/framework/middleware/statsd.py", line 34, in __call__ self.galaxy_stasd_client.timing(page, dt) File "galaxy/web/statsd_client.py", line 35, in timing self.statsd_client.timing(infix + path, time) File "statsd/client/base.py", line 33, in timing self._send_stat(stat, '%0.6f|ms' % delta, rate) File "statsd/client/base.py", line 61, in _send_stat self._after(self._prepare(stat, value, rate)) File "statsd/client/base.py", line 76, in _after self._send(data) File "statsd/client/udp.py", line 42, in _send self._sock.sendto(data.encode('ascii'), self._addr) ``` --- lib/galaxy/web/framework/middleware/statsd.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/galaxy/web/framework/middleware/statsd.py b/lib/galaxy/web/framework/middleware/statsd.py index 1b017f71565..d817569f3d8 100644 --- a/lib/galaxy/web/framework/middleware/statsd.py +++ b/lib/galaxy/web/framework/middleware/statsd.py @@ -28,8 +28,13 @@ class StatsdMiddleware: start_time = time.time() req = self.application(environ, start_response) dt = int((time.time() - start_time) * 1000) - page = environ.get("controller_action_key", None) or environ.get("PATH_INFO", "NOPATH").strip("/").replace( - "/", "." + page = ( + environ.get("controller_action_key", None) + or environ.get("PATH_INFO", "NOPATH") + .strip("/") + .replace("/", ".") + .encode("ascii", errors="replace") + .decode() ) self.galaxy_stasd_client.timing(page, dt) try: