Ejemplo n.º 1
0
def test_basehttp_style_middleware(target_application, route):
    route_metrics = [("Function/_test_bg_tasks:run_%s_bg_task" % route, 1)]
    old_metrics = [
        ("Function/_test_bg_tasks:%s_bg_task" % route, 1),
        ("Function/_test_bg_tasks:run_%s_bg_task" % route, 1),
    ]

    def _test():
        app = target_application["basehttp"]
        response = app.get("/" + route)
        assert response.status == 200

    if starlette_version >= (0, 20, 1):
        if sys.version_info[:2] > (3, 7):
            _test = validate_transaction_metrics(
                "_test_bg_tasks:run_%s_bg_task" % route,
                index=-2,
                scoped_metrics=route_metrics)(_test)
            _test = validate_transaction_metrics("_test_bg_tasks:%s_bg_task" %
                                                 route,
                                                 background_task=True)(_test)
            _test = validate_transaction_count(2)(_test)
        else:  # Python <= 3.7 requires this specific configuration with starlette 0.20.1
            _test = validate_transaction_metrics(
                "_test_bg_tasks:run_%s_bg_task" % route,
                scoped_metrics=route_metrics)(_test)
            _test = validate_transaction_count(1)(_test)
    else:
        _test = validate_transaction_metrics("_test_bg_tasks:run_%s_bg_task" %
                                             route,
                                             scoped_metrics=old_metrics)(_test)
        _test = validate_transaction_count(1)(_test)

    _test()
def test_middleware(nr_enabled, aiohttp_app, middleware, metric):
    @asyncio.coroutine
    def fetch():
        resp = yield from aiohttp_app.client.request('GET', '/coro')
        assert resp.status == 200
        text = yield from resp.text()
        assert "Hello Aiohttp!" in text
        return resp

    def _test():
        aiohttp_app.loop.run_until_complete(fetch())

    if nr_enabled:
        scoped_metrics = [
            ('Function/_target_application:index', 1),
            (metric, 1),
        ]

        rollup_metrics = [
            ('Function/_target_application:index', 1),
            (metric, 1),
            ('Python/Framework/aiohttp/%s' % aiohttp.__version__, 1),
        ]

        _test = validate_transaction_metrics(
            '_target_application:index',
            scoped_metrics=scoped_metrics,
            rollup_metrics=rollup_metrics)(_test)
    else:
        settings = global_settings()

        _test = override_generic_settings(settings, {'enabled': False})(_test)

    _test()
def test_errors_in_error_handlers(
        nr_enabled, app, url, metric_name, metrics, errors):
    settings = global_settings()

    @override_generic_settings(settings, {'enabled': nr_enabled})
    def _test():
        # Because of a bug in Sanic versions <0.8.0, the response.status value
        # is inconsistent. Rather than assert the status value, we rely on the
        # transaction errors validator to confirm the application acted as we'd
        # expect it to.
        app.fetch('get', url)

    if nr_enabled:
        _test = validate_transaction_errors(errors=errors)(_test)
        _test = validate_transaction_metrics(metric_name,
                scoped_metrics=metrics,
                rollup_metrics=metrics)(_test)
    else:
        _test = function_not_called('newrelic.core.stats_engine',
            'StatsEngine.record_transaction')(_test)

    _test()
Ejemplo n.º 4
0
def test_incomplete_coroutine(nr_transaction):
    @function_trace(name="coro")
    def coro():
        for _ in range(5):
            yield

    def _test():
        c = coro()

        for _ in c:
            break

        if is_pypy:
            # pypy is not guaranteed to delete the coroutine when it goes out
            # of scope. This code "helps" pypy along. The test above is really
            # just to verify that incomplete coroutines will "eventually" be
            # cleaned up. In pypy, unfortunately that means it may not be
            # reported all the time. A customer would be expected to call gc
            # directly; however, they already have to handle this case since
            # incomplete generators are well documented as having problems with
            # pypy's gc.

            # See:
            # http://doc.pypy.org/en/latest/cpython_differences.html#differences-related-to-garbage-collection-strategies
            # https://bitbucket.org/pypy/pypy/issues/736
            del c
            import gc

            gc.collect()

    if nr_transaction:
        _test = validate_transaction_metrics(
            "test_incomplete_coroutine",
            background_task=True,
            scoped_metrics=[("Function/coro", 1)],
            rollup_metrics=[("Function/coro", 1)],
        )(background_task(name="test_incomplete_coroutine")(_test))

    _test()
Ejemplo n.º 5
0
def test_unsupported_method(app, nr_enabled, ignore_status_codes):
    def _test():
        response = app.fetch('/simple',
                             method='TEAPOT',
                             body=b'',
                             allow_nonstandard_methods=True)
        assert response.code == 405

    if nr_enabled:
        _test = override_ignore_status_codes(ignore_status_codes)(_test)
        _test = validate_transaction_metrics(
            '_target_application:SimpleHandler')(_test)

        if ignore_status_codes:
            _test = validate_transaction_errors(errors=[])(_test)
        else:
            _test = validate_transaction_errors(
                errors=['tornado.web:HTTPError'])(_test)
    else:
        settings = global_settings()
        _test = override_generic_settings(settings, {'enabled': False})(_test)

    _test()