Make a subunit2sql.target extension
Just having this installed should make subunit2sql shove any counters.json it finds through to statsd.
This commit is contained in:
parent
e7d63bfd10
commit
f920b45956
@ -10,16 +10,24 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
import statsd
|
try:
|
||||||
|
import statsd
|
||||||
|
except ImportError:
|
||||||
|
statsd = None
|
||||||
|
import testtools
|
||||||
|
|
||||||
OPTS_GROUP = cfg.OptGroup(name='counters2statsd', title='Counters2Statsd')
|
OPTS_GROUP = cfg.OptGroup(name='counters2statsd', title='Counters2Statsd')
|
||||||
|
|
||||||
OPTS = [
|
OPTS = [
|
||||||
cfg.StrOpt('host', help='Statsd host to connect to', default=None),
|
cfg.StrOpt('host', help='Statsd host to connect to', default='localhost'),
|
||||||
cfg.IntOpt('port', help='Port on statsd host to connect to', default=None),
|
cfg.IntOpt('port', help='Port on statsd host to connect to', default=8125),
|
||||||
cfg.StrOpt('prefix', help='Prefix to add to stats', default=None),
|
cfg.StrOpt('prefix', help='Prefix to add to stats', default=None),
|
||||||
|
cfg.BoolOpt('enabled', help='Set to false to disable this plugin',
|
||||||
|
default=True)
|
||||||
]
|
]
|
||||||
|
|
||||||
_statsd_client = None
|
_statsd_client = None
|
||||||
@ -28,32 +36,70 @@ _statsd_client = None
|
|||||||
def get_statsd_client():
|
def get_statsd_client():
|
||||||
global _statsd_client
|
global _statsd_client
|
||||||
if _statsd_client is None:
|
if _statsd_client is None:
|
||||||
cfg.CONF.register_group(OPTS_GROUP)
|
|
||||||
cfg.CONF.register_opts(OPTS, group=OPTS_GROUP)
|
|
||||||
_statsd_client = statsd.StatsClient(cfg.CONF.counters2statsd.host,
|
_statsd_client = statsd.StatsClient(cfg.CONF.counters2statsd.host,
|
||||||
cfg.CONF.counters2statsd.port,
|
cfg.CONF.counters2statsd.port,
|
||||||
cfg.CONF.counters2statsd.prefix)
|
cfg.CONF.counters2statsd.prefix)
|
||||||
return _statsd_client
|
return _statsd_client
|
||||||
|
|
||||||
|
|
||||||
def add_test_run_attachments(attachments, test_run_id, session):
|
class AttachmentResult(testtools.StreamResult):
|
||||||
for attachment in attachments:
|
"""Keeps track of top level results with StreamToDict drops.
|
||||||
try:
|
|
||||||
counters = json.loads(attachment)
|
We use a SpooledTemporaryFile to keep it performant with smaller files
|
||||||
except ValueError:
|
but to ensure we don't use up tons of RAM. Anything over 1MB will be
|
||||||
continue
|
spooled out to disk.
|
||||||
if not isinstance(counters, dict):
|
"""
|
||||||
continue
|
@classmethod
|
||||||
if '__counters_meta__' not in counters:
|
def enabled(cls):
|
||||||
continue
|
cfg.CONF.register_group(OPTS_GROUP)
|
||||||
|
cfg.CONF.register_opts(OPTS, group=OPTS_GROUP)
|
||||||
|
cfg.CONF.register_cli_opts(OPTS, group=OPTS_GROUP)
|
||||||
|
return bool(statsd)
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(AttachmentResult, self).__init__()
|
||||||
|
self.attachments = {}
|
||||||
|
|
||||||
|
def status(self, test_id=None, test_status=None, test_tags=None,
|
||||||
|
runnable=True, file_name=None, file_bytes=None, eof=False,
|
||||||
|
mime_type=None, route_code=None, timestamp=None):
|
||||||
|
if not cfg.CONF.counters2statsd.enabled:
|
||||||
|
return
|
||||||
|
if test_id is not None:
|
||||||
|
return
|
||||||
|
if not file_name:
|
||||||
|
return
|
||||||
|
if file_name not in self.attachments:
|
||||||
|
self.attachments[file_name] = tempfile.SpooledTemporaryFile(
|
||||||
|
max_size=2 ** 30)
|
||||||
|
self.attachments[file_name].write(file_bytes)
|
||||||
|
if eof:
|
||||||
|
self.attachments[file_name].seek(0)
|
||||||
|
|
||||||
|
def stopTestRun(self):
|
||||||
|
if not cfg.CONF.counters2statsd.enabled:
|
||||||
|
return
|
||||||
client = get_statsd_client()
|
client = get_statsd_client()
|
||||||
for groupname, values in counters.items():
|
for file_name, attachment in self.attachments.items():
|
||||||
if not isinstance(values, dict):
|
if file_name != 'counters.json':
|
||||||
continue
|
continue
|
||||||
for k, v in values.items():
|
try:
|
||||||
k = '{}.{}'.format(groupname, k)
|
|
||||||
try:
|
try:
|
||||||
v = int(v)
|
attachment.seek(0)
|
||||||
except ValueError:
|
counters = json.loads(attachment.read().decode('utf-8'))
|
||||||
|
except AttributeError:
|
||||||
|
counters = json.loads(attachment)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if not isinstance(counters, dict):
|
||||||
|
continue
|
||||||
|
for groupname, values in counters.items():
|
||||||
|
if not isinstance(values, dict):
|
||||||
continue
|
continue
|
||||||
client.incr(k, v)
|
for k, v in values.items():
|
||||||
|
k = '{}.{}'.format(groupname, k)
|
||||||
|
try:
|
||||||
|
v = int(v)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
client.incr(k, v)
|
||||||
|
@ -34,8 +34,10 @@ class TestOpenStackQaTols(base.TestCase):
|
|||||||
mock_client.incr = mock.MagicMock('statds_incr')
|
mock_client.incr = mock.MagicMock('statds_incr')
|
||||||
statsd_mock.return_value = mock_client
|
statsd_mock.return_value = mock_client
|
||||||
fake_counters = {'mysql': {'Queries': 10}}
|
fake_counters = {'mysql': {'Queries': 10}}
|
||||||
fake_counters['__counters_meta__'] = {}
|
fake_counters = json.dumps(fake_counters).encode('utf-8')
|
||||||
fake_counters = json.dumps(fake_counters)
|
self.assertTrue(counters2statsd.AttachmentResult.enabled())
|
||||||
counters2statsd.add_test_run_attachments([fake_counters], 'foo', None)
|
result = counters2statsd.AttachmentResult()
|
||||||
statsd_mock.assert_called_with(None, None, None)
|
result.status(file_name='counters.json', file_bytes=fake_counters)
|
||||||
|
result.stopTestRun()
|
||||||
|
statsd_mock.assert_called_with('localhost', 8125, None)
|
||||||
mock_client.incr.assert_called_with('mysql.Queries', 10)
|
mock_client.incr.assert_called_with('mysql.Queries', 10)
|
||||||
|
@ -6,3 +6,4 @@ pbr>=1.6
|
|||||||
PyMySQL>=0.6.2 # MIT License
|
PyMySQL>=0.6.2 # MIT License
|
||||||
statsd>=1.0.0,<3.0
|
statsd>=1.0.0,<3.0
|
||||||
oslo.config>=1.4.0.0a3
|
oslo.config>=1.4.0.0a3
|
||||||
|
testtools>=1.4.0
|
||||||
|
@ -22,6 +22,8 @@ classifier =
|
|||||||
[entry_points]
|
[entry_points]
|
||||||
console_scripts =
|
console_scripts =
|
||||||
os-qa-counters = openstack_qa_tools.collect:main
|
os-qa-counters = openstack_qa_tools.collect:main
|
||||||
|
subunit2sql.target =
|
||||||
|
openstack_qa_statsd = openstack_qa_tools.counters2statsd:AttachmentResult
|
||||||
|
|
||||||
[files]
|
[files]
|
||||||
packages =
|
packages =
|
||||||
|
@ -12,5 +12,4 @@ oslosphinx>=2.5.0 # Apache-2.0
|
|||||||
oslotest>=1.10.0 # Apache-2.0
|
oslotest>=1.10.0 # Apache-2.0
|
||||||
testrepository>=0.0.18
|
testrepository>=0.0.18
|
||||||
testscenarios>=0.4
|
testscenarios>=0.4
|
||||||
testtools>=1.4.0
|
|
||||||
mock>=1.2
|
mock>=1.2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user