Example #1
0
def test_ensure_installed_twice(caplog):
    ensure_installed()
    ensure_installed()

    assert caplog.record_tuples == 2 * [(
        "scout_apm.instruments.elasticsearch",
        logging.DEBUG,
        "Instrumenting elasticsearch.",
    )]
Example #2
0
def test_ensure_installed_twice(caplog):
    ensure_installed()
    ensure_installed()

    assert caplog.record_tuples == 2 * [(
        "scout_apm.instruments.elasticsearch",
        logging.INFO,
        "Ensuring elasticsearch instrumentation is installed.",
    )]
def elasticsearch_client():
    # e.g. export ELASTICSEARCH_URL="http://localhost:9200/"
    ensure_installed()
    if "ELASTICSEARCH_URL" not in os.environ:
        raise pytest.skip("Elasticsearch isn't available")
    client = elasticsearch.Elasticsearch(os.environ["ELASTICSEARCH_URL"])
    try:
        yield client
    finally:
        client.close()
Example #4
0
def test_ensure_installed_fail_no_client(caplog):
    mock_no_client = mock.patch(
        "scout_apm.instruments.elasticsearch.Elasticsearch", new=None)
    with mock_no_client:
        ensure_installed()

    assert caplog.record_tuples == [
        (
            "scout_apm.instruments.elasticsearch",
            logging.INFO,
            "Ensuring elasticsearch instrumentation is installed.",
        ),
        (
            "scout_apm.instruments.elasticsearch",
            logging.INFO,
            "Unable to import elasticsearch.Elasticsearch",
        ),
    ]
Example #5
0
def test_ensure_installed_fail_no_client(caplog):
    mock_no_client = mock.patch(
        "scout_apm.instruments.elasticsearch.Elasticsearch", new=None)
    with mock_no_client:
        ensure_installed()

    assert caplog.record_tuples == [
        (
            "scout_apm.instruments.elasticsearch",
            logging.DEBUG,
            "Instrumenting elasticsearch.",
        ),
        (
            "scout_apm.instruments.elasticsearch",
            logging.DEBUG,
            "Couldn't import elasticsearch.Elasticsearch - probably not installed.",
        ),
    ]
def test_ensure_installed_fail_no_client_methods(caplog):
    mock_not_patched = mock.patch(
        "scout_apm.instruments.elasticsearch.have_patched_client", new=False)
    # Change the wrap methods to a boolean to cause an exception
    # and fail to wrap any of the client methods.
    mock_wrap_client_index_method = mock.patch(
        "scout_apm.instruments.elasticsearch.wrap_client_index_method",
        new=False)
    mock_wrap_client_method = mock.patch(
        "scout_apm.instruments.elasticsearch.wrap_client_method", new=False)
    with mock_not_patched, mock_wrap_client_index_method, mock_wrap_client_method:
        ensure_installed()

    logger, level, message = caplog.record_tuples[-1]
    assert logger == "scout_apm.instruments.elasticsearch"
    assert level == logging.WARNING
    assert message.startswith(
        "Failed to instrument any elasticsearch.Elasticsearch methods.")
Example #7
0
def test_ensure_installed_fail_no_client_bulk(caplog):
    mock_not_patched = mock.patch(
        "scout_apm.instruments.elasticsearch.have_patched_client", new=False)
    mock_no_bulk = delete_attributes(elasticsearch.Elasticsearch, "bulk")
    with mock_not_patched, mock_no_bulk:
        ensure_installed()

    assert len(caplog.record_tuples) == 2
    assert caplog.record_tuples[0] == (
        "scout_apm.instruments.elasticsearch",
        logging.DEBUG,
        "Instrumenting elasticsearch.",
    )
    logger, level, message = caplog.record_tuples[1]
    assert logger == "scout_apm.instruments.elasticsearch"
    assert level == logging.WARNING
    assert message.startswith(
        "Failed to instrument elasticsearch.Elasticsearch.bulk: AttributeError"
    )
Example #8
0
def test_ensure_installed_fail_no_client_bulk(caplog):
    mock_not_patched = mock.patch(
        "scout_apm.instruments.elasticsearch.have_patched_client", new=False)
    mock_client = mock.patch(
        "scout_apm.instruments.elasticsearch.Elasticsearch")
    with mock_not_patched, mock_client as mocked_client:
        del mocked_client.bulk

        ensure_installed()

    assert len(caplog.record_tuples) == 2
    assert caplog.record_tuples[0] == (
        "scout_apm.instruments.elasticsearch",
        logging.INFO,
        "Ensuring elasticsearch instrumentation is installed.",
    )
    logger, level, message = caplog.record_tuples[1]
    assert logger == "scout_apm.instruments.elasticsearch"
    assert level == logging.WARNING
    assert message.startswith(
        "Unable to instrument elasticsearch.Elasticsearch.bulk: AttributeError"
    )
Example #9
0
def test_ensure_installed_fail_no_transport_perform_request(caplog):
    mock_not_patched = mock.patch(
        "scout_apm.instruments.elasticsearch.have_patched_transport",
        new=False)
    mock_transport = mock.patch(
        "scout_apm.instruments.elasticsearch.Transport")
    with mock_not_patched, mock_transport as mocked_transport:
        del mocked_transport.perform_request

        ensure_installed()

    assert len(caplog.record_tuples) == 2
    assert caplog.record_tuples[0] == (
        "scout_apm.instruments.elasticsearch",
        logging.DEBUG,
        "Instrumenting elasticsearch.",
    )
    logger, level, message = caplog.record_tuples[1]
    assert logger == "scout_apm.instruments.elasticsearch"
    assert level == logging.WARNING
    assert message.startswith(
        "Failed to instrument elasticsearch.Transport.perform_request: AttributeError"
    )