Merge "Port swift.common.utils.StatsdClient to Python 3"
This commit is contained in:
@@ -1169,11 +1169,13 @@ class StatsdClient(object):
|
|||||||
parts.append('@%s' % (sample_rate,))
|
parts.append('@%s' % (sample_rate,))
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
if six.PY3:
|
||||||
|
parts = [part.encode('utf-8') for part in parts]
|
||||||
# Ideally, we'd cache a sending socket in self, but that
|
# Ideally, we'd cache a sending socket in self, but that
|
||||||
# results in a socket getting shared by multiple green threads.
|
# results in a socket getting shared by multiple green threads.
|
||||||
with closing(self._open_socket()) as sock:
|
with closing(self._open_socket()) as sock:
|
||||||
try:
|
try:
|
||||||
return sock.sendto('|'.join(parts), self._target)
|
return sock.sendto(b'|'.join(parts), self._target)
|
||||||
except IOError as err:
|
except IOError as err:
|
||||||
if self.logger:
|
if self.logger:
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
@@ -1230,7 +1232,7 @@ def timing_stats(**dec_kwargs):
|
|||||||
swift's wsgi server controllers, based on response code.
|
swift's wsgi server controllers, based on response code.
|
||||||
"""
|
"""
|
||||||
def decorating_func(func):
|
def decorating_func(func):
|
||||||
method = func.func_name
|
method = func.__name__
|
||||||
|
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def _timing_stats(ctrl, *args, **kwargs):
|
def _timing_stats(ctrl, *args, **kwargs):
|
||||||
|
@@ -34,6 +34,7 @@ import sys
|
|||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
import six
|
||||||
from six import BytesIO, StringIO
|
from six import BytesIO, StringIO
|
||||||
from six.moves.queue import Queue, Empty
|
from six.moves.queue import Queue, Empty
|
||||||
from six.moves import range
|
from six.moves import range
|
||||||
@@ -3659,7 +3660,7 @@ class TestStatsdLogging(unittest.TestCase):
|
|||||||
self.assertEqual(len(mock_socket.sent), 1)
|
self.assertEqual(len(mock_socket.sent), 1)
|
||||||
|
|
||||||
payload = mock_socket.sent[0][0]
|
payload = mock_socket.sent[0][0]
|
||||||
self.assertTrue(payload.endswith("|@0.5"))
|
self.assertTrue(payload.endswith(b"|@0.5"))
|
||||||
|
|
||||||
def test_sample_rates_with_sample_rate_factor(self):
|
def test_sample_rates_with_sample_rate_factor(self):
|
||||||
logger = utils.get_logger({
|
logger = utils.get_logger({
|
||||||
@@ -3685,8 +3686,10 @@ class TestStatsdLogging(unittest.TestCase):
|
|||||||
self.assertEqual(len(mock_socket.sent), 1)
|
self.assertEqual(len(mock_socket.sent), 1)
|
||||||
|
|
||||||
payload = mock_socket.sent[0][0]
|
payload = mock_socket.sent[0][0]
|
||||||
self.assertTrue(payload.endswith("|@%s" % effective_sample_rate),
|
suffix = "|@%s" % effective_sample_rate
|
||||||
payload)
|
if six.PY3:
|
||||||
|
suffix = suffix.encode('utf-8')
|
||||||
|
self.assertTrue(payload.endswith(suffix), payload)
|
||||||
|
|
||||||
effective_sample_rate = 0.587 * 0.91
|
effective_sample_rate = 0.587 * 0.91
|
||||||
statsd_client.random = lambda: effective_sample_rate - 0.001
|
statsd_client.random = lambda: effective_sample_rate - 0.001
|
||||||
@@ -3694,8 +3697,10 @@ class TestStatsdLogging(unittest.TestCase):
|
|||||||
self.assertEqual(len(mock_socket.sent), 2)
|
self.assertEqual(len(mock_socket.sent), 2)
|
||||||
|
|
||||||
payload = mock_socket.sent[1][0]
|
payload = mock_socket.sent[1][0]
|
||||||
self.assertTrue(payload.endswith("|@%s" % effective_sample_rate),
|
suffix = "|@%s" % effective_sample_rate
|
||||||
payload)
|
if six.PY3:
|
||||||
|
suffix = suffix.encode('utf-8')
|
||||||
|
self.assertTrue(payload.endswith(suffix), payload)
|
||||||
|
|
||||||
def test_timing_stats(self):
|
def test_timing_stats(self):
|
||||||
class MockController(object):
|
class MockController(object):
|
||||||
@@ -3992,7 +3997,7 @@ class TestStatsdLoggingDelegation(unittest.TestCase):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
payload = self.sock.recv(4096)
|
payload = self.sock.recv(4096)
|
||||||
if payload and 'STOP' in payload:
|
if payload and b'STOP' in payload:
|
||||||
return 42
|
return 42
|
||||||
self.queue.put(payload)
|
self.queue.put(payload)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -4015,10 +4020,14 @@ class TestStatsdLoggingDelegation(unittest.TestCase):
|
|||||||
|
|
||||||
def assertStat(self, expected, sender_fn, *args, **kwargs):
|
def assertStat(self, expected, sender_fn, *args, **kwargs):
|
||||||
got = self._send_and_get(sender_fn, *args, **kwargs)
|
got = self._send_and_get(sender_fn, *args, **kwargs)
|
||||||
|
if six.PY3:
|
||||||
|
got = got.decode('utf-8')
|
||||||
return self.assertEqual(expected, got)
|
return self.assertEqual(expected, got)
|
||||||
|
|
||||||
def assertStatMatches(self, expected_regexp, sender_fn, *args, **kwargs):
|
def assertStatMatches(self, expected_regexp, sender_fn, *args, **kwargs):
|
||||||
got = self._send_and_get(sender_fn, *args, **kwargs)
|
got = self._send_and_get(sender_fn, *args, **kwargs)
|
||||||
|
if six.PY3:
|
||||||
|
got = got.decode('utf-8')
|
||||||
return self.assertTrue(re.search(expected_regexp, got),
|
return self.assertTrue(re.search(expected_regexp, got),
|
||||||
[got, expected_regexp])
|
[got, expected_regexp])
|
||||||
|
|
||||||
@@ -4187,7 +4196,7 @@ class TestStatsdLoggingDelegation(unittest.TestCase):
|
|||||||
utils.get_valid_utf8_str(valid_utf8_str))
|
utils.get_valid_utf8_str(valid_utf8_str))
|
||||||
self.assertEqual(valid_utf8_str,
|
self.assertEqual(valid_utf8_str,
|
||||||
utils.get_valid_utf8_str(unicode_sample))
|
utils.get_valid_utf8_str(unicode_sample))
|
||||||
self.assertEqual('\xef\xbf\xbd\xef\xbf\xbd\xec\xbc\x9d\xef\xbf\xbd',
|
self.assertEqual(b'\xef\xbf\xbd\xef\xbf\xbd\xec\xbc\x9d\xef\xbf\xbd',
|
||||||
utils.get_valid_utf8_str(invalid_utf8_str))
|
utils.get_valid_utf8_str(invalid_utf8_str))
|
||||||
|
|
||||||
@reset_logger_state
|
@reset_logger_state
|
||||||
|
Reference in New Issue
Block a user