def test_send_negative(mock_post):
    """
        Test negative responce from Apptuit backend
    """
    mock_post.return_value.status_code = 503
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               prefix="apr.",
                               tags=tags)
    cput = registry.histogram("cpu")
    count = 0
    while True:
        cput.add(random.randint(1, 100))
        count = count + 1
        if count > 10000:
            break
    with assert_raises(ApptuitSendException):
        reporter.report_now()
예제 #2
0
    def __init__(self,
                 privkey: keys.PrivateKey,
                 context: BasePeerContext,
                 max_peers: int = DEFAULT_MAX_PEERS,
                 event_bus: EndpointAPI = None,
                 metrics_registry: MetricsRegistry = None,
                 ) -> None:
        self.logger = get_logger(self.__module__ + '.' + self.__class__.__name__)

        self.privkey = privkey
        self.max_peers = max_peers
        self.context = context

        self.connected_nodes: Dict[SessionAPI, BasePeer] = {}

        self._subscribers: List[PeerSubscriber] = []
        self._event_bus = event_bus

        if metrics_registry is None:
            # Initialize with a MetricsRegistry from pyformance as p2p can not depend on Trinity
            # This is so that we don't need to pass a MetricsRegistry in tests and mocked pools.
            metrics_registry = MetricsRegistry()
        self._active_peer_counter = metrics_registry.counter('trinity.p2p/peers.counter')
        self._peer_reporter_registry = self.get_peer_reporter_registry(metrics_registry)

        # Restricts the number of concurrent connection attempts can be made
        self._connection_attempt_lock = asyncio.BoundedSemaphore(MAX_CONCURRENT_CONNECTION_ATTEMPTS)

        # Ensure we can only have a single concurrent handshake in flight per remote
        self._handshake_locks = ResourceLock()

        self.peer_backends = self.setup_peer_backends()
        self.connection_tracker = self.setup_connection_tracker()
class TestOpenTSDBReporter(TimedTestCase):
    def setUp(self):
        super(TestOpenTSDBReporter, self).setUp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def tearDown(self):
        super(TestOpenTSDBReporter, self).tearDown()

    def test_report_now(self):
        r = OpenTSDBReporter(application_name="app",
                             write_key="key",
                             registry=self.registry,
                             reporting_interval=1,
                             clock=self.clock,
                             prefix="prefix.",
                             url="http://opentsdb.com/api/put")
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2**i)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        output = r._collect_metrics(registry=self.registry)
        self.assertEqual(len(output), 31)
        for data in output:
            assert data['metric'].startswith("prefix.")

    def test_send_request(self):
        r = OpenTSDBReporter(application_name="app",
                             write_key="key",
                             registry=self.registry,
                             reporting_interval=1,
                             clock=self.clock,
                             prefix="prefix.",
                             url="http://opentsdb.com/api/put")
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2**i)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        with mock.patch("pyformance.reporters.opentsdb_reporter.urllib.urlopen"
                        ) as patch:
            r.report_now()
            patch.assert_called()
def test_collect_data_points():
    """
        Test data is being collected correctly
    """
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               prefix="apr.",
                               tags=tags)
    counter_test = registry.counter('counter {"tk1":"tv1","tk2":"tv2"}')
    counter_test.inc(2)
    dps = reporter._collect_data_points(reporter.registry)
    assert_equals(len(dps), 1)
    assert_equals(dps[0].value, 2)
    assert_equals(dps[0].metric, "apr.counter.count")
    assert_equals(
        dps[0].tags, {
            'host': 'localhost',
            'region': 'us-east-1',
            'service': 'web-server',
            'tk1': 'tv1',
            'tk2': 'tv2'
        })
def test_meta_metrics_of_reporter(mock_post):
    """
    Test that meta metrics of reporter work
    """
    mock_post.return_value.status_code = 200
    token = "asdashdsauh_8aeraerf"
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               tags=None)
    cput = registry.counter("cpu.time")
    cput.inc(1)
    dps = reporter._collect_data_points(reporter.registry)
    assert_equals(len(dps), 1)
    assert_equals(dps[0].metric, "cpu.time.count")
    assert_equals(dps[0].value, 1)
    reporter.report_now()
    dps = reporter._collect_data_points(reporter._meta_metrics_registry)
    dps = sorted(dps, key=lambda x: x.metric)
    assert_equals(len(dps), 18)
    assert_equals(dps[0].metric, "apptuit.reporter.send.failed.count")
    assert_equals(dps[1].metric, "apptuit.reporter.send.successful.count")
    assert_equals(dps[11].metric, "apptuit.reporter.send.time.count")
    assert_equals(dps[17].metric, "apptuit.reporter.send.total.count")
def test_globaltags_override():
    """
        Test that if the global tags and metric tags contain same tag key,
        the metric tags override global tags
    """
    host = socket.gethostname()
    token = "asdashdsauh_8aeraerf"
    tags = {"region": "us-east-1"}
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               tags=tags)
    counter1 = registry.counter('counter1 {"region":"us-west-2","id": 1}')
    counter2 = registry.counter(
        'counter2 {"region":"us-west-3","id": 2, "new_tag": "foo"}')
    counter3 = registry.counter('counter3')
    counter1.inc(2)
    counter2.inc()
    counter3.inc()
    dps = reporter._collect_data_points(reporter.registry)
    dps = sorted(dps, key=lambda x: x.metric)
    assert_equals(dps[0].tags, {"region": "us-west-2", "id": 1, "host": host})
    assert_equals(dps[1].tags, {
        "region": "us-west-3",
        "id": 2,
        "new_tag": "foo",
        "host": host
    })
    assert_equals(dps[2].tags, {"region": "us-east-1", "host": host})
    assert_equals(reporter.tags, {"region": "us-east-1", "host": host})
def test_globaltags_none():
    """
        Test that metric tags work when global tags are not present
    """
    host = socket.gethostname()
    token = "asdashdsauh_8aeraerf"
    tags = {"region": "us-east-1"}
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               tags=None)
    counter1 = registry.counter('counter1 {"region":"us-west-2","id": 1}')
    counter2 = registry.counter(
        'counter2 {"region":"us-west-3","id": 2, "new_tag": "foo"}')
    counter1.inc(2)
    counter2.inc()
    dps = reporter._collect_data_points(reporter.registry)
    dps = sorted(dps, key=lambda x: x.metric)
    assert_equals(len(dps), 2)
    assert_equals(dps[0].tags, {"region": "us-west-2", "id": 1, "host": host})
    assert_equals(dps[1].tags, {
        "region": "us-west-3",
        "id": 2,
        "new_tag": "foo",
        "host": host
    })
    assert_equals(reporter.tags, {"host": host})
def test_batch_send(mock_post):
    """
        Test that when we create more than BATCH_SIZE number of points
        we are able to send all of them
    """
    mock_post.return_value.status_code = 204
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               prefix="apr.",
                               tags=tags)
    points_to_be_created = BATCH_SIZE * 2 + 10
    counters = [
        registry.counter("counter%d" % i) for i in range(points_to_be_created)
    ]
    for i in range(points_to_be_created):
        counters[i].inc()
    reporter.report_now()
    total_points_sent = reporter._meta_metrics_registry.counter(
        NUMBER_OF_TOTAL_POINTS).get_count()
    assert_equals(total_points_sent, points_to_be_created)
예제 #9
0
def test_reporter_tags_with_global_env_tags():
    """
        Test that reporter tags take priority
        TODO:
        We have 8 possible combinations -
            1. global env tags: true, reporter tags: true, metric tags: true
            2. global env tags: true, reporter tags: true, metric tags: false
            3. global env tags: true, reporter tags: false, metric tags: true
            4. global env tags: true, reporter tags: false, metric tags: false
            5. global env tags: false, reporter tags: true, metric tags: true
            6. global env tags: false, reporter tags: true, metric tags: false
            7. global env tags: false, reporter tags: false, metric tags: true
            8. global env tags: false, reporter tags: false, metric tags: false
    """

    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token",
                                           APPTUIT_PY_TAGS: 'host: environ, ip: 1.1.1.1'})
    mock_environ.start()
    registry = MetricsRegistry()
    reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"})
    counter = registry.counter("counter")
    counter.inc(1)
    payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry))
    assert_equals(len(payload), 1)
    assert_equals(payload[0]["tags"], {'host': 'reporter', 'ip': '2.2.2.2'})
    reporter = ApptuitReporter(registry=registry)
    counter = registry.counter("counter")
    counter.inc(1)
    payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry))
    assert_equals(len(payload), 1)
    assert_equals(payload[0]["tags"], {"host": "environ", "ip": "1.1.1.1"})
    mock_environ.stop()
예제 #10
0
def test_no_environ_tags():
    """
        Test tags work even if no global tags present as env variable
    """

    timestamp = int(time.time())
    test_val = math.pi
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token"})
    mock_environ.start()
    client = Apptuit()
    dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1},
                    timestamp=timestamp, value=test_val)
    dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val)
    payload = client._create_payload_from_datapoints([dp1, dp2])
    assert_equals(len(payload), 2)
    assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1})
    assert_equals(payload[1]["tags"], {"test": 2})

    registry = MetricsRegistry()
    reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"})
    counter = registry.counter("counter")
    counter.inc(1)
    payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry))
    assert_equals(len(payload), 1)
    assert_equals(payload[0]["tags"], {'host': 'reporter', 'ip': '2.2.2.2'})
    mock_environ.stop()
def test_reporter_thread_active(mock_post):
    """
        Test that reporter thread is active even if we are not able to send data
    """
    mock_post.return_value.status_code = 503
    mock_post.side_effect = HTTPError()
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               prefix="apr.",
                               tags=tags)
    reporter.start()
    cput = registry.histogram("cpu")
    cput.add(random.randint(1, 100))
    time.sleep(3)
    assert_greater_equal(mock_post.call_count, 2)
예제 #12
0
class TestCloudWatchReporter(TimedTestCase):
    def setUp(self):
        super(TestCloudWatchReporter, self).setUp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def tearDown(self):
        super(TestCloudWatchReporter, self).tearDown()

    def test_report_now(self):
        r = CloudWatchReporter(application_name="app", tags={"host":"localhost"},
                               registry=self.registry, reporting_interval=1, clock=self.clock,
                               aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                               region=REGION)

        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        output = r._collect_metrics(registry=self.registry)
        self.assertEqual(len(output), 31)
        for data in output:
            assert data['metric'].startswith("prefix.")

    def test_send_request(self):
        r = CloudWatchReporter(application_name="app", tags={"host":"localhost"},
                               registry=self.registry, reporting_interval=1, clock=self.clock,
                               aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                               region="ap-south-1")
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        r.report_now()
        # with mock.patch("pyformance.reporters.cloudwatch_reporter.botocore.client.CloudWatch.put_metric_data") as patch:
        with mock.patch("pyformance.reporters.cloudwatch_report.CloudWatchReporter._collect_metrics") as patch:
            r.report_now()
            patch.assert_called()
예제 #13
0
class RegistryTestCase(TimedTestCase):
    def setUp(self):
        super(RegistryTestCase, self).setUp()
        self.registry = MetricsRegistry(TimedTestCase.clock)

    def tearDown(self):
        super(RegistryTestCase, self).tearDown()

    def test__add(self):
        self.registry.add("foo", Meter(TimedTestCase.clock))
예제 #14
0
class RegistryTestCase(TimedTestCase):


    def setUp(self):
        super(RegistryTestCase, self).setUp()
        self.registry = MetricsRegistry(TimedTestCase.clock)

    def tearDown(self):
        super(RegistryTestCase, self).tearDown()

    def test__add(self):
        self.registry.add('foo', Meter(TimedTestCase.clock))
예제 #15
0
    def test_time_calls_with_registry(self):
        registry = MetricsRegistry()

        @time_calls(registry=registry, tags={"tag1": "val1"})
        def timed_func():
            pass

        timed_func()

        stats = registry.get_metrics(key="timed_func_calls", tags={"tag1": "val1"})
        print(registry.get_metrics(key="timed_func_calls", tags={"tag1": "val1"}))
        self.assertEqual(stats["count"], 1)
        self.assertTrue(stats["mean_rate"])
예제 #16
0
    def test_collect_metrics(self):
        # should return a list of point lines
        reg = MetricsRegistry()
        reg.counter("foo_counter_1")
        reg.counter("foo_counter_2")

        wf_reporter = get_base_reporter(reg)
        lines = wf_reporter._collect_metrics(reg)
        assert (len(lines) == 2)  # equals no. of registered counters

        delta.delta_counter(reg, "foo_delta_1")
        delta.delta_counter(reg, "foo_delta_2")
        lines = wf_reporter._collect_metrics(reg)
        assert (len(lines) == 4)  # added two more counters
예제 #17
0
class TestOpenTSDBReporter(TimedTestCase):
    def setUp(self):
        super(TestOpenTSDBReporter, self).setUp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def tearDown(self):
        super(TestOpenTSDBReporter, self).tearDown()

    def test_report_now(self):
        r = OpenTSDBReporter(application_name="app", write_key="key", registry=self.registry, reporting_interval=1,
                             clock=self.clock, prefix="prefix.", url="http://opentsdb.com/api/put")
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        output = r._collect_metrics(registry=self.registry)
        self.assertEqual(len(output), 31)
        for data in output:
            assert data['metric'].startswith("prefix.")

    def test_send_request(self):
        r = OpenTSDBReporter(application_name="app", write_key="key", registry=self.registry, reporting_interval=1,
                             clock=self.clock, prefix="prefix.", url="http://opentsdb.com/api/put")
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        with mock.patch("pyformance.reporters.opentsdb_reporter.urllib.urlopen") as patch:
            r.report_now()
            patch.assert_called()
예제 #18
0
    def test_time_calls_with_registry(self):
        registry = MetricsRegistry()

        @time_calls(registry=registry, tags={"tag1": "val1"})
        def timed_func():
            pass

        timed_func()

        metric_name = "RegistryTestCase.test_time_calls_with_registry.<locals>.timed_func_calls"

        stats = registry.get_metrics(key=metric_name, tags={"tag1": "val1"})
        print(registry.get_metrics(key=metric_name, tags={"tag1": "val1"}))
        self.assertEqual(stats["count"], 1)
        self.assertTrue(stats["mean_rate"])
예제 #19
0
def test_tags_of_metric_take_priority():
    """
        Test that metric tags take priority
    """
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token",
                                           APPTUIT_PY_TAGS: 'host: environ, ip: 1.1.1.1'})
    mock_environ.start()
    registry = MetricsRegistry()
    reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"})
    counter = registry.counter('counter {"host": "metric", "ip": "3.3.3.3"}')
    counter.inc(1)

    payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry))
    assert_equals(len(payload), 1)
    assert_equals(payload[0]["tags"], {"host": "metric", "ip": "3.3.3.3"})
    mock_environ.stop()
예제 #20
0
class TestCsvReporter(TimedTestCase):

    def setUp(self):
        super(TestCsvReporter, self).setUp()
        self.path = tempfile.mktemp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def tearDown(self):
        super(TestCsvReporter, self).tearDown()
        if os.path.exists(self.path):
            shutil.rmtree(self.path)

    def test_report_now(self):
        g1 = self.registry.gauge("gauge1")
        g1.set_value(123)
        r = CsvReporter(
            registry=self.registry, reporting_interval=1, clock=self.clock,
            path=self.path)
        r.report_now()
        output_filename = os.path.join(self.path, "gauge1.csv")

        output = open(output_filename).read()
        self.assertEqual(output.splitlines(), [
          'timestamp\tvalue', '1970-01-01 00:00:00\t123'
        ])
예제 #21
0
    def __init__(self,
                 host,
                 port,
                 global_key,
                 worker_name,
                 metrics_name,
                 logger,
                 enabled=True):
        self._logger = logger
        try:
            self.enabled = enabled
            if self.enabled:
                self.hostname = socket.gethostname().split('.')[0]

                self.registry = MetricsRegistry()
                self.global_key = global_key + '.' + self.hostname + '.' + worker_name + '.'

                self.reporter = CarbonReporter(registry=self.registry,
                                               reporting_interval=1,
                                               prefix=self.global_key,
                                               server=host,
                                               port=port)
                self.reporter.start()

            self.after_init(host, port, global_key, worker_name, metrics_name,
                            enabled)
        except Exception as e:
            self._logger.error('GraphiteSender exception: {}'.format(e))
            self._logger.error('GraphiteSender traceback: {}'.format(
                traceback.format_exc()))
예제 #22
0
class TestCsvReporter(TimedTestCase):
    def setUp(self):
        super(TestCsvReporter, self).setUp()
        self.clock = ManualClock()
        self.path = tempfile.mktemp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def tearDown(self):
        super(TestCsvReporter, self).tearDown()
        if os.path.exists(self.path):
            shutil.rmtree(self.path)

    def test_report_now(self):
        g1 = self.registry.gauge("gauge1")
        g1.set_value(123)

        with CsvReporter(registry=self.registry,
                         reporting_interval=1,
                         clock=self.clock,
                         path=self.path) as r:
            r.report_now()

        output_filename = os.path.join(self.path, "gauge1.csv")

        output = open(output_filename).read()
        self.assertEqual(output.splitlines(),
                         ['timestamp\tvalue', '1970-01-01 00:00:00\t123'])
예제 #23
0
 def test_delta_reset(self):
     reg = MetricsRegistry()
     counter = delta.delta_counter(reg, "foo_delta_1")
     counter.inc(10)
     wf_reporter = get_base_reporter(reg)
     wf_reporter._collect_metrics(reg)
     assert (counter.get_count() == 0)  # delta decremented by count
def test_process_metrics_of_reporter_is_active(mock_post):
    """
    Test that process metrics of reporter is active
    """
    mock_post.return_value.status_code = 200
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               tags=tags,
                               collect_process_metrics=True)
    reporter.report_now()
    for i in reporter.process_metrics.resource_metric_names:
        assert_in(i, registry._counters)
    for i in reporter.process_metrics.thread_metrics_names:
        assert_in(i, registry._gauges)
    for i in reporter.process_metrics.gc_metric_names:
        assert_in(i, registry._counters)
def test_zero_tags(mock_post):
    """
        Test that using reporter without tags does not raise error
        (we add host tag)
    """
    mock_post.return_value.status_code = 204
    token = "asdashdsauh_8aeraerf"
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               token=token,
                               prefix="apr.")
    counter_test = registry.counter('counter')
    counter_test.inc(2)
    reporter.report_now()
예제 #26
0
class TestSysLogReporter(TimedTestCase):

    def setUp(self):
        super(TestSysLogReporter, self).setUp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None
        self.clock.now = 0

    def tearDown(self):
        super(TestSysLogReporter, self).tearDown()
        self.clock.now = 0

    def test_report_now(self):
        # connect to a local rsyslog server
        r = SysLogReporter(registry=self.registry, reporting_interval=1, clock=self.clock)
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        gcb = self.registry.gauge("gcb", lambda: 123)
        gsimple = self.registry.gauge("gsimple").set_value(42)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        with mock.patch("pyformance.reporters.syslog_reporter.logging.Logger.info") as patch:
            r.report_now()
            expected = '{"c1.count": 1, "counter-2.count": -2, "gcb.value": 123, "gsimple.value": 42, "hist.75_percentile": 160.0, "hist.95_percentile": 512, "hist.999_percentile": 512, "hist.99_percentile": 512, "hist.avg": 102.3, "hist.count": 10.0, "hist.max": 512, "hist.min": 1, "hist.std_dev": 164.94851048466944, "m1.15m_rate": 0, "m1.1m_rate": 0, "m1.5m_rate": 0, "m1.count": 1.0, "m1.mean_rate": 1.0, "t1.15m_rate": 0, "t1.1m_rate": 0, "t1.50_percentile": 1, "t1.5m_rate": 0, "t1.75_percentile": 1, "t1.95_percentile": 1, "t1.999_percentile": 1, "t1.99_percentile": 1, "t1.avg": 1.0, "t1.count": 1.0, "t1.max": 1, "t1.mean_rate": 1.0, "t1.min": 1, "t1.std_dev": 0.0, "t1.sum": 1.0, "timestamp": 1}'
 
            patch.assert_called_with(expected)
예제 #27
0
class TestSysLogReporter(TimedTestCase):

    def setUp(self):
        super(TestSysLogReporter, self).setUp()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None
        self.clock.now = 0

    def tearDown(self):
        super(TestSysLogReporter, self).tearDown()
        self.clock.now = 0

    def test_report_now(self):
        #connect to a logal rsyslog server
        r = SysLogReporter(registry=self.registry, reporting_interval=1, clock=self.clock)
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        gcb = self.registry.gauge("gcb", lambda: 123)
        gsimple = self.registry.gauge("gsimple").set_value(42)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        with mock.patch("pyformance.reporters.syslog_reporter.logging.Logger.info") as patch:
            r.report_now()
            expected = '{"c1.count": 1, "counter-2.count": -2, "gcb.value": 123, "gsimple.value": 42, "hist.75_percentile": 160.0, "hist.95_percentile": 512, "hist.999_percentile": 512, "hist.99_percentile": 512, "hist.avg": 102.3, "hist.count": 10.0, "hist.max": 512, "hist.min": 1, "hist.std_dev": 164.94851048466947, "m1.15m_rate": 0, "m1.1m_rate": 0, "m1.5m_rate": 0, "m1.count": 1.0, "m1.mean_rate": 1.0, "t1.15m_rate": 0, "t1.1m_rate": 0, "t1.50_percentile": 1, "t1.5m_rate": 0, "t1.75_percentile": 1, "t1.95_percentile": 1, "t1.999_percentile": 1, "t1.99_percentile": 1, "t1.avg": 1.0, "t1.count": 1.0, "t1.max": 1, "t1.mean_rate": 1.0, "t1.min": 1, "t1.std_dev": 0.0, "t1.sum": 1.0, "timestamp": 1}'
 
            patch.assert_called_with(expected)
def test_zero_tags_with_host_disabled(mock_post):
    """
        Test that using reporter without tags raises error
    """
    mock_post.return_value.status_code = 204
    token = "asdashdsauh_8aeraerf"
    registry = MetricsRegistry()
    with patch.dict(os.environ, {DISABLE_HOST_TAG: "True"}):
        reporter = ApptuitReporter(sanitize_mode=None,
                                   registry=registry,
                                   api_endpoint="http://localhost",
                                   reporting_interval=1,
                                   token=token,
                                   prefix="apr.")
        counter_test = registry.counter('counter')
        counter_test.inc(2)
        with assert_raises(ValueError):
            reporter.report_now()
def test_none_prefix():
    """
        Test for None prefix
    """
    token = "asdashdsauh_8aeraerf"
    tags = {"region": "us-east-1"}
    registry = MetricsRegistry()
    reporter = ApptuitReporter(sanitize_mode=None,
                               registry=registry,
                               api_endpoint="http://localhost",
                               reporting_interval=1,
                               prefix=None,
                               token=token,
                               tags=tags)
    counter1 = registry.counter('counter1')
    counter1.inc()
    dps = reporter._collect_data_points(reporter.registry)
    assert_equals(dps[0].metric, "counter1.count")
예제 #30
0
    def wavefront_wrapper(*args, **kwargs):
        log.debug("WaveFrontWrapper: starting")
        server = os.environ.get('WAVEFRONT_URL')
        if not server:
            raise ValueError("Environment variable WAVEFRONT_URL is not set.")
        auth_token = os.environ.get('WAVEFRONT_API_TOKEN')
        if not auth_token:
            raise ValueError(
                "Environment variable WAVEFRONT_API_TOKEN is not set.")
        is_report_standard_metrics = True
        if os.environ.get('REPORT_STANDARD_METRICS') in ['False', 'false']:
            is_report_standard_metrics = False
        # AWS lambda execution enviroment requires handler to consume two arguments
        # 1. Event - input of json format
        # 2. Context - The execution context object
        context = args[1]
        # Expected formats for Lambda ARN are:
        # https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#arn-syntax-lambda
        invoked_function_arn = context.invoked_function_arn
        split_arn = invoked_function_arn.split(':')
        point_tags = {
            'LambdaArn': invoked_function_arn,
            'FunctionName': context.function_name,
            'ExecutedVersion': context.function_version,
            'Region': split_arn[3],
            'accountId': split_arn[4]
        }
        if split_arn[5] == 'function':
            point_tags['Resource'] = split_arn[6]
            if len(split_arn) == 8:
                point_tags[
                    'Resource'] = point_tags['Resource'] + ":" + split_arn[7]
        elif split_arn[5] == 'event-source-mappings':
            point_tags['EventSourceMappings'] = split_arn[6]

        log.debug("WaveFrontWrapper: starting registry")
        # Initialize registry for each lambda invocation
        global reg
        reg = MetricsRegistry()
        if reg is None:
            log.error("WaveFrontWrapper: failed to start registry")

        # Initialize the wavefront direct reporter
        wf_direct_reporter = WavefrontDirectReporter(
            server=server,
            token=auth_token,
            registry=reg,
            source=point_tags['FunctionName'],
            tags=point_tags,
            prefix="")

        if is_report_standard_metrics:
            return call_lambda_with_standard_metrics(wf_direct_reporter, *args,
                                                     **kwargs)
        else:
            return call_lambda_without_standard_metrics(
                wf_direct_reporter, *args, **kwargs)
def test_no_token():
    """
            Test that no token raises error
    """
    registry = MetricsRegistry()
    with assert_raises(ValueError) as ex:
        ApptuitReporter(sanitize_mode=None,
                        registry=registry,
                        reporting_interval=1,
                        prefix="apr.")
예제 #32
0
class TestCarbonReporter(TimedTestCase):
    def setUp(self):
        super(TestCarbonReporter, self).setUp()
        self.output = StringIO()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def connect(self, *args):
        # part of fake socket interface
        pass

    def sendall(self, data):
        # part of fake socket interface
        self.output.write(data)

    def close(self):
        # part of fake socket interface
        pass

    def tearDown(self):
        super(TestCarbonReporter, self).tearDown()

    def test_report_now(self):
        r = CarbonReporter(registry=self.registry,
                           reporting_interval=1,
                           clock=self.clock,
                           socket_factory=lambda: self)
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2**i)
        gcb = self.registry.gauge("gcb", lambda: 123)
        gsimple = self.registry.gauge("gsimple").set_value(42)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        r.report_now()
        self.assertEqual(self.output.getvalue().splitlines(), [
            'counter-2.count -2 1', 'gsimple.value 42 1', 'gcb.value 123 1',
            't1.1m_rate 0 1', 't1.999_percentile 1 1', 't1.15m_rate 0 1',
            't1.99_percentile 1 1', 't1.mean_rate 1.0 1',
            't1.95_percentile 1 1', 't1.min 1 1', 't1.5m_rate 0 1',
            't1.count 1.0 1', 't1.75_percentile 1 1', 't1.std_dev 0.0 1',
            't1.max 1 1', 't1.avg 1.0 1', 'hist.count 10.0 1',
            'hist.999_percentile 512 1', 'hist.99_percentile 512 1',
            'hist.min 1 1', 'hist.95_percentile 512 1',
            'hist.75_percentile 160.0 1', 'hist.std_dev 164.948510485 1',
            'hist.max 512 1', 'hist.avg 102.3 1', 'm1.1m_rate 0 1',
            'm1.15m_rate 0 1', 'm1.5m_rate 0 1', 'm1.mean_rate 1.0 1',
            'c1.count 1 1'
        ])
예제 #33
0
def test_deprecated_token_variable():
    """
    Test that reporter and client work with the deprecated token env variable
    """
    warnings.filterwarnings('error')
    with patch.dict(os.environ, {DEPRECATED_APPTUIT_PY_TOKEN: "test_token"}):
        registry = MetricsRegistry()
        with assert_raises(DeprecationWarning):
            ApptuitReporter(registry=registry,
                            tags={'host': 'reporter'})
    warnings.resetwarnings()
예제 #34
0
def report_metrics(host, server, token):
    reg = MetricsRegistry()

    wf_proxy_reporter = WavefrontProxyReporter(
        host=host,
        port=2878,
        registry=reg,
        source="wavefront-pyformance-example",
        tags={
            "key1": "val1",
            "key2": "val2"
        },
        prefix="python.proxy.")
    wf_direct_reporter = WavefrontDirectReporter(
        server=server,
        token=token,
        registry=reg,
        source="wavefront-pyformance-exmaple",
        tags={
            "key1": "val1",
            "key2": "val2"
        },
        prefix="python.direct.")

    # counter
    c1 = reg.counter("foo_count")
    c1.inc()

    # delta counter
    d1 = delta.delta_counter(reg, "foo_delta_count")
    d1.inc()
    d1.inc()

    # gauge
    g1 = reg.gauge("foo_gauge")
    g1.set_value(2)

    # meter
    m1 = reg.meter("foo_meter")
    m1.mark()

    # timer
    t1 = reg.timer("foo_timer")
    timer_ctx = t1.time()
    time.sleep(3)
    timer_ctx.stop()

    # histogram
    h1 = reg.histogram("foo_histogram")
    h1.add(1.0)
    h1.add(1.5)

    wf_proxy_reporter.report_now()
    wf_proxy_reporter.stop()
    wf_direct_reporter.report_now()
예제 #35
0
class TestCarbonReporter(TimedTestCase):

    def setUp(self):
        super(TestCarbonReporter, self).setUp()
        self.output = StringIO()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def connect(self, *args):
        # part of fake socket interface
        pass

    def sendall(self, data):
        # part of fake socket interface
        self.output.write(data)

    def close(self):
        # part of fake socket interface
        pass

    def tearDown(self):
        super(TestCarbonReporter, self).tearDown()

    def test_report_now(self):
        r = CarbonReporter(
            registry=self.registry, reporting_interval=1, clock=self.clock,
            socket_factory=lambda: self)
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        gcb = self.registry.gauge("gcb", lambda: 123)
        gsimple = self.registry.gauge("gsimple").set_value(42)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        r.report_now()
        self.assertEqual(self.output.getvalue().splitlines(), [
            'counter-2.count -2 1',
            'gsimple.value 42 1',
            'gcb.value 123 1',
            't1.1m_rate 0 1',
            't1.999_percentile 1 1',
            't1.15m_rate 0 1',
            't1.99_percentile 1 1',
            't1.mean_rate 1.0 1',
            't1.95_percentile 1 1',
            't1.min 1 1',
            't1.5m_rate 0 1',
            't1.count 1.0 1',
            't1.75_percentile 1 1',
            't1.std_dev 0.0 1',
            't1.max 1 1',
            't1.avg 1.0 1',
            'hist.count 10.0 1',
            'hist.999_percentile 512 1',
            'hist.99_percentile 512 1',
            'hist.min 1 1',
            'hist.95_percentile 512 1',
            'hist.75_percentile 160.0 1',
            'hist.std_dev 164.948510485 1',
            'hist.max 512 1',
            'hist.avg 102.3 1',
            'm1.1m_rate 0 1',
            'm1.15m_rate 0 1',
            'm1.5m_rate 0 1',
            'm1.mean_rate 1.0 1',
            'c1.count 1 1'
        ])
예제 #36
0
 def setUp(self):
     super(TestOpenTSDBReporter, self).setUp()
     self.registry = MetricsRegistry(clock=self.clock)
     self.maxDiff = None
예제 #37
0
 def setUp(self):
     super(TestCarbonReporter, self).setUp()
     self.output = BytesIO()
     self.registry = MetricsRegistry(clock=self.clock)
     self.maxDiff = None
예제 #38
0
 def setUp(self):
     super(TestSysLogReporter, self).setUp()
     self.registry = MetricsRegistry(clock=self.clock)
     self.maxDiff = None
     self.clock.now = 0
예제 #39
0
class TestCarbonReporter(TimedTestCase):
    def setUp(self):
        super(TestCarbonReporter, self).setUp()
        self.output = BytesIO()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None

    def connect(self, *args):
        # part of fake socket interface
        pass

    def sendall(self, data):
        # part of fake socket interface
        self.output.write(data)

    def close(self):
        # part of fake socket interface
        pass

    def tearDown(self):
        super(TestCarbonReporter, self).tearDown()

    def capture_test_metrics(self):
        self.clock.now = 1
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        gcb = self.registry.gauge("gcb", lambda: 123)
        gsimple = self.registry.gauge("gsimple").set_value(42)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)

    def test_report_now_plain(self):
        r = CarbonReporter(
            registry=self.registry,
            reporting_interval=1,
            clock=self.clock,
            socket_factory=lambda: self,
        )
        self.capture_test_metrics()
        r.report_now()
        test_data = sorted(self.output.getvalue().decode().splitlines())
        expected_data = sorted(
            [
                "counter-2.count -2 2",
                "c1.count 1 2",
                "gsimple.value 42 2",
                "gcb.value 123 2",
                "t1.1m_rate 0 2",
                "t1.999_percentile 1 2",
                "t1.15m_rate 0 2",
                "t1.99_percentile 1 2",
                "t1.mean_rate 1.0 2",
                "t1.95_percentile 1 2",
                "t1.min 1 2",
                "t1.50_percentile 1 2",
                "t1.5m_rate 0 2",
                "t1.count 1.0 2",
                "t1.75_percentile 1 2",
                "t1.std_dev 0.0 2",
                "t1.max 1 2",
                "t1.sum 1.0 2",
                "t1.avg 1.0 2",
                "hist.count 10.0 2",
                "hist.999_percentile 512 2",
                "hist.99_percentile 512 2",
                "hist.min 1 2",
                "hist.95_percentile 512 2",
                "hist.75_percentile 160.0 2",
                "hist.std_dev 164.94851048466944 2"
                if PY3
                else "hist.std_dev 164.948510485 2",
                "hist.max 512 2",
                "hist.avg 102.3 2",
                "m1.count 1.0 2",
                "m1.1m_rate 0 2",
                "m1.15m_rate 0 2",
                "m1.5m_rate 0 2",
                "m1.mean_rate 1.0 2",
            ]
        )
        self.assertEqual(test_data, expected_data)

    def test_report_now_pickle(self):
        r = CarbonReporter(
            registry=self.registry,
            reporting_interval=1,
            clock=self.clock,
            socket_factory=lambda: self,
            pickle_protocol=True,
        )
        self.capture_test_metrics()
        r.report_now()
        test_data = sorted(pickle.loads(self.output.getvalue()[4:]))
        expected_data = sorted(
            [
                ("counter-2.count", (2, -2.0)),
                ("c1.count", (2, 1)),
                ("gsimple.value", (2, 42.0)),
                ("gcb.value", (2, 123.0)),
                ("t1.1m_rate", (2, 0.0)),
                ("t1.999_percentile", (2, 1)),
                ("t1.15m_rate", (2, 0.0)),
                ("t1.99_percentile", (2, 1)),
                ("t1.mean_rate", (2, 1)),
                ("t1.95_percentile", (2, 1)),
                ("t1.min", (2, 1)),
                ("t1.50_percentile", (2, 1)),
                ("t1.5m_rate", (2, 0.0)),
                ("t1.count", (2, 1)),
                ("t1.75_percentile", (2, 1)),
                ("t1.std_dev", (2, 0.0)),
                ("t1.max", (2, 1)),
                ("t1.sum", (2, 1)),
                ("t1.avg", (2, 1)),
                ("hist.count", (2, 10.0)),
                ("hist.999_percentile", (2, 512.0)),
                ("hist.99_percentile", (2, 512.0)),
                ("hist.min", (2, 1)),
                ("hist.95_percentile", (2, 512.0)),
                ("hist.75_percentile", (2, 160.0)),
                ("hist.std_dev", (2, 164.94851048466944)),
                ("hist.max", (2, 512.0)),
                ("hist.avg", (2, 102.3)),
                ("m1.count", (2, 1)),
                ("m1.1m_rate", (2, 0.0)),
                ("m1.15m_rate", (2, 0.0)),
                ("m1.5m_rate", (2, 0.0)),
                ("m1.mean_rate", (2, 1)),
            ]
        )
        self.assertEqual(test_data, expected_data)
예제 #40
0
 def setUp(self):
     super(RegistryTestCase, self).setUp()
     self.registry = MetricsRegistry(TimedTestCase.clock)
class TestConsoleReporter(TimedTestCase):

    def setUp(self):
        super(TestConsoleReporter, self).setUp()
        self.output = StringIO()
        self.registry = MetricsRegistry(clock=self.clock)
        self.maxDiff = None
        self.clock.now = 0

    def tearDown(self):
        super(TestConsoleReporter, self).tearDown()
        self.clock.now = 0

    def test_report_now(self):
        r = ConsoleReporter(
            registry=self.registry, reporting_interval=1, stream=self.output, clock=self.clock)
        h1 = self.registry.histogram("hist")
        for i in range(10):
            h1.add(2 ** i)
        gcb = self.registry.gauge("gcb", lambda: 123)
        gsimple = self.registry.gauge("gsimple").set_value(42)
        t1 = self.registry.timer("t1")
        m1 = self.registry.meter("m1")
        m1.mark()
        with t1.time():
            c1 = self.registry.counter("c1")
            c2 = self.registry.counter("counter-2")
            c1.inc()
            c2.dec()
            c2.dec()
            self.clock.add(1)
        r.report_now()
        self.assertEqual(self.output.getvalue().splitlines(), [
                         '== 1970-01-01 00:00:01 ===================================',
                         'counter-2:', '               count = -2',
                         'gsimple:', '               value = 42',
                         'gcb:', '               value = 123',
                         't1:', '             1m_rate = 0',
                         '      999_percentile = 1',
                         '            15m_rate = 0',
                         '       99_percentile = 1',
                         '           mean_rate = 1.0',
                         '       95_percentile = 1',
                         '                 min = 1',
                         '             5m_rate = 0',
                         '               count = 1.0',
                         '       75_percentile = 1',
                         '             std_dev = 0.0',
                         '                 max = 1',
                         '                 avg = 1.0',
                         'hist:', '               count = 10.0',
                         '      999_percentile = 512',
                         '       99_percentile = 512',
                         '                 min = 1',
                         '       95_percentile = 512',
                         '       75_percentile = 160.0',
                         '             std_dev = 164.948510485',
                         '                 max = 512',
                         '                 avg = 102.3',
                         'm1:', '             1m_rate = 0',
                         '            15m_rate = 0',
                         '             5m_rate = 0',
                         '           mean_rate = 1.0',
                         'c1:', '               count = 1', ''])
 def setUp(self):
     super(TestConsoleReporter, self).setUp()
     self.output = StringIO()
     self.registry = MetricsRegistry(clock=self.clock)
     self.maxDiff = None
     self.clock.now = 0
예제 #43
0
 def setUp(self):
     super(TestCsvReporter, self).setUp()
     self.path = tempfile.mktemp()
     self.registry = MetricsRegistry(clock=self.clock)
     self.maxDiff = None