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)
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_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()
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)
Esempio n. 5
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()))
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_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_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_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")
Esempio n. 10
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
Esempio n. 11
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()
Esempio n. 12
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()
Esempio n. 13
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()
def test_apptuit_sanitizer_of_reporter(mock_post):
    """
    Test that apptuit_sanitizer of reporter works
    """
    mock_post.return_value.status_code = 200
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        u"region-loc$-本語": u"us-east-1-本語",
        "service.type/name": "web-server"
    }
    registry = MetricsRegistry()
    reporter = ApptuitReporter(
        sanitize_mode="apptuit",
        registry=registry,
        api_endpoint="http://localhost",
        reporting_interval=1,
        token=token,
        tags=tags,
    )
    assert_equals(reporter.client.sanitizer, sanitize_name_apptuit)
    unicode_counter = registry.counter(u'abc.日本語')
    unicode_counter.inc(1)
    dps = reporter._collect_data_points(reporter.registry)
    payload = reporter.client._create_payload_from_datapoints(dps)
    assert_equals(payload[0]['metric'], u'abc.日本語.count')
    assert_equals(
        payload[0]['tags'], {
            "host": "localhost",
            u"region-loc_-本語": u"us-east-1-本語",
            "service.type/name": "web-server"
        })
    assert_equals(payload[0]['value'], 1)
    registry.clear()
    cput = registry.counter('7&&cpu-time/seconds{"total-%": "100"}')
    cput.inc(1)
    dps = reporter._collect_data_points(reporter.registry)
    payload = reporter.client._create_payload_from_datapoints(dps)
    assert_equals(len(payload), 1)
    assert_equals(payload[0]['metric'], "7_cpu-time/seconds.count")
    assert_equals(
        payload[0]['tags'], {
            'host': 'localhost',
            u'region-loc_-本語': u'us-east-1-本語',
            'service.type/name': 'web-server',
            'total-_': '100'
        })
    assert_equals(payload[0]['value'], 1)
    reporter.report_now()
    dps = reporter._collect_data_points(reporter._meta_metrics_registry)
    payload = reporter.client._create_payload_from_datapoints(dps)
    assert_equals(len(payload), 18)
    payload = sorted(payload, key=lambda x: x['metric'])
    assert_equals(payload[0]['metric'], "apptuit.reporter.send.failed.count")
    assert_equals(payload[1]['metric'],
                  "apptuit.reporter.send.successful.count")
    assert_equals(payload[11]['metric'], "apptuit.reporter.send.time.count")
    assert_equals(payload[17]['metric'], "apptuit.reporter.send.total.count")
Esempio n. 15
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.")
Esempio n. 17
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()
Esempio n. 18
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()
Esempio n. 19
0
    def test_delta_counter(self):
        reg = MetricsRegistry()
        counter = delta.delta_counter(reg, "foo")
        assert (isinstance(counter, delta.DeltaCounter))

        # test duplicate (should return previously registered counter)
        duplicate_counter = delta.delta_counter(reg, "foo")
        assert (counter == duplicate_counter)
        assert (delta.is_delta_counter(delta.DeltaCounter.DELTA_PREFIX + "foo",
                                       reg))

        different_counter = delta.delta_counter(reg, "foobar")
        assert (counter != different_counter)
Esempio n. 20
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"])
Esempio n. 21
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
Esempio n. 22
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"])
Esempio n. 23
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()
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()
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")
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_prometheus_sanitizer_of_reporter_disabled(mock_post):
    """
    Test that prometheus_sanitizer of reporter is disabled
    """
    mock_post.return_value.status_code = 200
    token = "asdashdsauh_8aeraerf"
    tags = {
        "host": "localhost",
        u"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)
    unicode_counter = registry.counter(u'abc.日本語')
    unicode_counter.inc(1)
    dps = reporter._collect_data_points(reporter.registry)
    payload = reporter.client._create_payload_from_datapoints(dps)
    assert_equals(payload[0]['metric'], u'abc.日本語.count')
    assert_equals(payload[0]['tags'], tags)
    assert_equals(payload[0]['value'], 1)
    registry.clear()
    cput = registry.counter("cpu.time")
    cput.inc(1)
    dps = reporter._collect_data_points(reporter.registry)
    payload = reporter.client._create_payload_from_datapoints(dps)
    assert_equals(len(payload), 1)
    assert_equals(payload[0]['metric'], "cpu.time.count")
    assert_equals(payload[0]['value'], 1)
    reporter.report_now()
    dps = reporter._collect_data_points(reporter._meta_metrics_registry)
    payload = reporter.client._create_payload_from_datapoints(dps)
    payload = sorted(payload, key=lambda x: x['metric'])
    assert_equals(len(dps), 18)
    assert_equals(payload[0]['metric'], "apptuit.reporter.send.failed.count")
    assert_equals(payload[1]['metric'],
                  "apptuit.reporter.send.successful.count")
    assert_equals(payload[11]['metric'], "apptuit.reporter.send.time.count")
    assert_equals(payload[17]['metric'], "apptuit.reporter.send.total.count")
Esempio n. 28
0
    def wavefront_wrapper(*args, **kwargs):
        print("Func has been decorated.")

        # Initialize registry
        global reg
        reg = MetricsRegistry()

        # Get wavefront secrets
        context, payload = args[0], args[1]
        server = context["secrets"].get("wavefront_server_url", "")
        auth_token = context["secrets"].get("wavefront_auth_token", "")

        # Initialize the wavefront direct reporter
        wf_direct_reporter = WavefrontDirectReporter(server=server,
                                                     token=auth_token,
                                                     registry=reg,
                                                     prefix="")

        call_dispatch_function(wf_direct_reporter, *args, **kwargs)
def test_process_metrics_of_reporter_not_active(mock_post):
    """
    Test that process metrics of reporter is not 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)
    reporter.report_now()
    assert_is_none(reporter.process_metrics)
def test_partially_successful_send(mock_post):
    """
        Test that we handle partially successful sends
    """
    mock_post.return_value.status_code = 400
    mock_post.side_effect = ApptuitSendException("failed to send some points",
                                                 400,
                                                 success=98,
                                                 failed=2,
                                                 errors=[])
    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 = 100
    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()
    with assert_raises(ApptuitSendException):
        reporter.report_now()
    successful_points_sent = reporter._meta_metrics_registry. \
        counter(NUMBER_OF_SUCCESSFUL_POINTS).get_count()
    failed_points_count = reporter._meta_metrics_registry. \
        counter(NUMBER_OF_FAILED_POINTS).get_count()
    assert_equals(successful_points_sent, 98)
    assert_equals(failed_points_count, 2)