def _test():
        with BackgroundTask(application(), name='T1') as txn:
            txn._priority = first_priority
            try:
                raise ValueError('OOPS')
            except ValueError:
                txn.notice_error()

        with BackgroundTask(application(), name='T2') as txn:
            txn._priority = second_priority
            try:
                raise ValueError('OOPS')
            except ValueError:
                txn.notice_error()

        # Stats engine
        stats_engine = core_application_stats_engine()

        error_events = list(stats_engine.error_events)
        assert len(error_events) == 1

        # highest priority should win
        assert stats_engine.error_events.pq[0][0] == 1

        if first_transaction_saved:
            assert error_events[0][0]['transactionName'].endswith('/T1')
        else:
            assert error_events[0][0]['transactionName'].endswith('/T2')
def test_error_outside_transaction_excluded_user_param():

    try:
        raise OutsideNoParamsError("Error outside transaction")
    except OutsideNoParamsError:
        application_instance = application()
        application_instance.notice_error(attributes={"test_key": "test_value"})
def test_multiple_error_events_outside_transaction():
    app = application()
    for i in range(2):
        try:
            raise ErrorEventOutsideTransactionError(ERR_MESSAGE + str(i))
        except ErrorEventOutsideTransactionError:
            record_exception(*sys.exc_info(), application=app)
def test_application_exception_custom_params():
    try:
        raise RuntimeError('one')
    except RuntimeError:
        application_instance = application()
        record_exception(params={'key': 'value'},
                application=application_instance)
Ejemplo n.º 5
0
def test_application_notice_error_params_not_a_dict():
    try:
        raise RuntimeError()
    except RuntimeError:
        notice_error(sys.exc_info(),
                     attributes=[1, 2, 3],
                     application=application())
def test_py2_application_exception_message_bytes_english():
    """Assert byte string of ascii characters decodes sensibly"""
    try:
        raise ValueError(BYTES_ENGLISH)
    except ValueError:
        app = application()
        record_exception(application=app)
Ejemplo n.º 7
0
def test_application_exception_custom_params():
    try:
        raise RuntimeError("one")
    except RuntimeError:
        application_instance = application()
        notice_error(attributes={"key": "value"},
                     application=application_instance)
def test_application_exception_custom_params():
    try:
        raise RuntimeError('one')
    except RuntimeError:
        application_instance = application()
        notice_error(attributes={'key': 'value'},
                     application=application_instance)
def test_transaction_end_on_different_task():
    import asyncio

    txn = BackgroundTask(application(), name="Parent")

    async def parent():
        txn.__enter__()
        child_start = asyncio.Event()
        parent_end = asyncio.Event()
        child_task = asyncio.ensure_future(child(child_start, parent_end))
        await child_start.wait()
        parent_end.set()
        return child_task

    async def child(child_start, parent_end):
        child_start.set()
        await parent_end.wait()

        # Calling asyncio.sleep(0) allows the scheduled done callbacks to run
        # done callbacks are scheduled through call_soon
        await asyncio.sleep(0)
        txn.__exit__(None, None, None)

    async def test():
        task = await asyncio.ensure_future(parent())
        await task

    loop = asyncio.get_event_loop()
    loop.run_until_complete(test())
def test_error_outside_transaction_excluded_user_param():

    try:
        raise OutsideNoParamsError("Error outside transaction")
    except OutsideNoParamsError:
        application_instance = application()
        application_instance.record_exception(
            params={'test_key': 'test_value'})
def test_py2_application_exception_message_unicode():
    """Assert unicode message when using non-ascii characters is preserved,
    with sys default encoding"""
    try:
        raise ValueError(UNICODE_MESSAGE)
    except ValueError:
        app = application()
        record_exception(application=app)
def test_py2_application_exception_message_unicode_english():
    """Assert unicode message when using ascii compatible characters preserved,
    with sys default encoding"""
    try:
        raise ValueError(UNICODE_ENGLISH)
    except ValueError:
        app = application()
        record_exception(application=app)
def test_py3_application_exception_message_bytes_non_english_unicode():
    """Assert (native) unicode exception message is preserved when when
    non-ascii compatible characters present"""
    try:
        raise ValueError(UNICODE_MESSAGE)
    except ValueError:
        app = application()
        record_exception(application=app)
def test_py3_application_exception_message_unicode_english():
    """Assert (native) unicode exception message is preserved, when characters
    are ascii-compatible"""
    try:
        raise ValueError(UNICODE_ENGLISH)
    except ValueError:
        app = application()
        record_exception(application=app)
Ejemplo n.º 15
0
def test_transport_perform_request_requests():
    app = application()
    with BackgroundTask(app, 'perform_request_requests') as transaction:
        transport = Transport([HOST], connection_class=RequestsHttpConnection)
        transport.perform_request('POST', METHOD, params=PARAMS, body=DATA)

    expected = (ES_SETTINGS['host'], ES_SETTINGS['port'], None)
    assert transaction._nr_datastore_instance_info == expected
Ejemplo n.º 16
0
def test_transport_get_connection():
    app = application()
    with BackgroundTask(app, 'transport_perform_request') as transaction:
        transport = Transport([HOST])
        transport.get_connection()

    expected = (ES_SETTINGS['host'], ES_SETTINGS['port'], None)
    assert transaction._nr_datastore_instance_info == expected
async def sentinel_in_cache_txn_exited(asyncio, bg):
    event = asyncio.Event()

    with BackgroundTask(application(), 'fg') as txn:
        _ = txn.root_span
        task = asyncio.ensure_future(bg(event))

    await event.wait()
    return task
async def trace_in_cache_txn_active(asyncio, bg):
    event = asyncio.Event()

    with BackgroundTask(application(), 'fg'):
        with FunctionTrace('fg') as _:
            task = asyncio.ensure_future(bg(event))
        await event.wait()

    return task
def test_py2_application_exception_message_unicode_utf8_encoding():
    """Assert unicode error message is preserved with sys non-default utf-8
    encoding
    """
    try:
        raise ValueError(UNICODE_MESSAGE)
    except ValueError:
        app = application()
        record_exception(application=app)
def test_py2_application_exception_message_bytes_non_english():
    """Assert known situation where (explicitly) utf-8 encoded byte string gets
    mangled when default sys encoding is ascii. THIS TEST ASSERTS THAT THE
    MESSAGE IS WRONG. We do not expect it to work now, or in the future.
    """
    try:
        raise ValueError(BYTES_UTF8_ENCODED)
    except ValueError:
        app = application()
        record_exception(application=app)
    def _test():
        # Stats engine
        stats_engine = core_application_stats_engine()

        with BackgroundTask(application(), name='T1') as txn:
            txn._priority = first_priority

        with BackgroundTask(application(), name='T2') as txn:
            txn._priority = second_priority

        transaction_events = list(stats_engine.transaction_events)
        assert len(transaction_events) == 1

        # highest priority should win
        assert stats_engine.transaction_events.pq[0][0] == 1

        if first_transaction_saved:
            assert transaction_events[0][0]['name'].endswith('/T1')
        else:
            assert transaction_events[0][0]['name'].endswith('/T2')
def test_py3_application_exception_message_bytes_non_english():
    """It really makes a mess of things when you cast from bytes to a
    string in python 3 (that is using str(), not using encode/decode methods).
    This is because all characters in bytes are literals, no implicit
    decoding happens, like it does in python 2. You just shouldn't use bytes
    for exception messages in Python 3. THIS TEST ASSERTS THAT THE
    MESSAGE IS WRONG. We do not expect it to work now, or in the future.
    """
    try:
        raise ValueError(BYTES_UTF8_ENCODED)
    except ValueError:
        app = application()
        record_exception(application=app)
Ejemplo n.º 23
0
def test_notice_error_strip_message_disabled_outside_transaction():
    settings = application_settings()
    assert not settings.strip_exception_messages.enabled

    try:
        assert not error_is_saved(ErrorOne)
        raise ErrorOne(ErrorOne.message)
    except ErrorOne:
        application_instance = application()
        application_instance.notice_error()

    my_error = core_application_stats_engine_error(_error_one_name)
    assert my_error.message == ErrorOne.message
Ejemplo n.º 24
0
def test_notice_error_strip_message_enabled_outside_transaction():
    settings = application_settings()
    assert settings.strip_exception_messages.enabled

    try:
        assert not error_is_saved(ErrorTwo)
        raise ErrorTwo(ErrorTwo.message)
    except ErrorTwo:
        application_instance = application()
        application_instance.notice_error()

    my_error = core_application_stats_engine_error(_error_two_name)
    assert my_error.message == STRIP_EXCEPTION_MESSAGE
def test_py2_application_exception_message_bytes_utf8_encoding_non_english():
    """Assert utf-8 encoded byte produces correct exception message when sys
    encoding is also utf-8.
    """
    try:

        # Bytes literal with non-ascii compatible characters only allowed in
        # python 2

        raise ValueError('I💜🐍')
    except ValueError:
        app = application()
        record_exception(application=app)
    def _test():
        with BackgroundTask(application(), name='T1') as txn:
            txn._priority = first_priority
            txn.record_custom_event('foobar', {'foo': 'bar'})

        with BackgroundTask(application(), name='T2') as txn:
            txn._priority = second_priority
            txn.record_custom_event('barbaz', {'foo': 'bar'})

        # Stats engine
        stats_engine = core_application_stats_engine()

        custom_events = list(stats_engine.custom_events)
        assert len(custom_events) == 1

        # highest priority should win
        assert stats_engine.custom_events.pq[0][0] == 1

        if first_transaction_saved:
            assert custom_events[0][0]['type'] == 'foobar'
        else:
            assert custom_events[0][0]['type'] == 'barbaz'
def test_py2_application_exception_message_bytes_implicit_encoding_non_english():
    """Assert known situation where (implicitly) utf-8 encoded byte string gets
    mangled when default sys encoding is ascii. THIS TEST ASSERTS THAT THE
    MESSAGE IS WRONG. We do not expect it to work now, or in the future.
    """
    try:

        # Bytes literal with non-ascii compatible characters only allowed in
        # python 2

        raise ValueError('I💜🐍')
    except ValueError:
        app = application()
        record_exception(application=app)
Ejemplo n.º 28
0
def test_notice_error_strip_message_in_allowlist_outside_transaction():
    settings = application_settings()
    assert settings.strip_exception_messages.enabled
    assert _error_three_name in settings.strip_exception_messages.allowlist

    try:
        assert not error_is_saved(ErrorThree)
        raise ErrorThree(ErrorThree.message)
    except ErrorThree:
        application_instance = application()
        application_instance.notice_error()

    my_error = core_application_stats_engine_error(_error_three_name)
    assert my_error.message == ErrorThree.message
Ejemplo n.º 29
0
def test_notice_error_strip_message_not_in_allowlist_outside_transaction():
    settings = application_settings()
    assert settings.strip_exception_messages.enabled
    assert _error_four_name not in settings.strip_exception_messages.allowlist

    try:
        assert not error_is_saved(ErrorFour)
        raise ErrorFour(ErrorFour.message)
    except ErrorFour:
        application_instance = application()
        application_instance.notice_error()

    my_error = core_application_stats_engine_error(_error_four_name)
    assert my_error.message == STRIP_EXCEPTION_MESSAGE
Ejemplo n.º 30
0
def test_transport_perform_request_urllib3():
    app = application()
    with BackgroundTask(app, 'perform_request_urllib3') as transaction:
        transport = Transport([HOST], connection_class=Urllib3HttpConnection)
        if VERSION >= (7, 16, 0):
            transport.perform_request('POST',
                                      METHOD,
                                      headers=HEADERS,
                                      params=PARAMS,
                                      body=DATA)
        else:
            transport.perform_request('POST', METHOD, params=PARAMS, body=DATA)
    expected = (ES_SETTINGS['host'], ES_SETTINGS['port'], None)
    assert transaction._nr_datastore_instance_info == expected