2014-02-28 10:19:02 -07:00
|
|
|
import unittest
|
|
|
|
import time
|
|
|
|
import threading
|
2014-05-01 16:20:25 -06:00
|
|
|
import os
|
2014-02-28 10:19:02 -07:00
|
|
|
|
2014-05-01 16:20:25 -06:00
|
|
|
from nose.plugins.skip import SkipTest
|
2014-04-30 16:21:39 -06:00
|
|
|
|
2014-05-02 15:06:54 -06:00
|
|
|
from monagent.common.aggregator import MetricsAggregator
|
2014-05-08 08:46:09 -06:00
|
|
|
from monagent.monstatsd import Server
|
2014-05-02 15:06:54 -06:00
|
|
|
from monagent.common.util import PidFile
|
|
|
|
from monagent.common.config import get_logging_config
|
|
|
|
from monagent.collector.jmxfetch import JMXFetch
|
|
|
|
|
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
STATSD_PORT = 8129
|
|
|
|
class DummyReporter(threading.Thread):
|
|
|
|
def __init__(self, metrics_aggregator):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.finished = threading.Event()
|
|
|
|
self.metrics_aggregator = metrics_aggregator
|
|
|
|
self.interval = 10
|
|
|
|
self.metrics = None
|
|
|
|
self.finished = False
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self.finished:
|
|
|
|
time.sleep(self.interval)
|
|
|
|
self.flush()
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
metrics = self.metrics_aggregator.flush()
|
|
|
|
if metrics:
|
|
|
|
self.metrics = metrics
|
|
|
|
|
|
|
|
class JMXTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
aggregator = MetricsAggregator("test_host")
|
|
|
|
self.server = Server(aggregator, "localhost", STATSD_PORT)
|
|
|
|
pid_file = PidFile('dogstatsd')
|
|
|
|
self.reporter = DummyReporter(aggregator)
|
|
|
|
|
|
|
|
self.t1 = threading.Thread(target=self.server.start)
|
|
|
|
self.t1.start()
|
|
|
|
|
|
|
|
confd_path = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", "jmx_yamls"))
|
|
|
|
JMXFetch.init(confd_path, {'dogstatsd_port':STATSD_PORT}, get_logging_config(), 15)
|
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.server.stop()
|
|
|
|
self.reporter.finished = True
|
|
|
|
JMXFetch.stop()
|
|
|
|
|
|
|
|
|
|
|
|
def testCustomJMXMetric(self):
|
2014-04-30 16:21:39 -06:00
|
|
|
raise SkipTest('Requires running JMX')
|
2014-02-28 10:19:02 -07:00
|
|
|
count = 0
|
|
|
|
while self.reporter.metrics is None:
|
|
|
|
time.sleep(1)
|
|
|
|
count += 1
|
|
|
|
if count > 20:
|
|
|
|
raise Exception("No metrics were received in 20 seconds")
|
|
|
|
|
|
|
|
metrics = self.reporter.metrics
|
|
|
|
|
|
|
|
self.assertTrue(type(metrics) == type([]))
|
|
|
|
self.assertTrue(len(metrics) > 0)
|
2014-05-02 16:41:50 -06:00
|
|
|
self.assertEquals(len([t for t in metrics if t['metric'] == "my.metric.buf" and "instance:jmx_instance1" in t['dimensions']]), 2, metrics)
|
|
|
|
self.assertTrue(len([t for t in metrics if 'type:ThreadPool' in t['dimensions'] and "instance:jmx_instance1" in t['dimensions'] and "jmx.catalina" in t['metric']]) > 8, metrics)
|
|
|
|
self.assertTrue(len([t for t in metrics if "jvm." in t['metric'] and "instance:jmx_instance1" in t['dimensions']]) == 7, metrics)
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|