def test_send_remote_failover_async(self, should_try, http_send):
        should_try.return_value = True

        client = Client(
            servers=['http://example.com'],
            app_name='app_name',
            secret_token='secret',
            async_mode=True,
        )
        logger = mock.Mock()
        client.error_logger.error = logger

        # test error
        encoded_data = client.encode({'message': 'oh no'})
        http_send.side_effect = TransportException('oopsie', encoded_data)
        client.send_remote('http://example.com/api/store', data=encoded_data)
        client.close()
        assert client.state.status == client.state.ERROR
        assert len(logger.call_args_list) == 2
        assert 'oopsie' in logger.call_args_list[0][0][0]
        assert 'oh no' in logger.call_args_list[1][0][1]

        # test recovery
        http_send.side_effect = None
        client.send_remote('http://example.com/api/store', 'foo')
        client.close()
        assert client.state.status == client.state.ONLINE
def test_send_remote_failover_sync_non_transport_exception_error(should_try, http_send, caplog):
    should_try.return_value = True

    client = Client(
        server_url="http://example.com",
        service_name="app_name",
        secret_token="secret",
        transport_class="elasticapm.transport.http.Transport",
        metrics_interval="0ms",
        metrics_sets=[],
    )
    # test error
    http_send.side_effect = ValueError("oopsie")
    with caplog.at_level("ERROR", "elasticapm.transport"):
        client.capture_message("foo", handled=False)
    client._transport.flush()
    record = caplog.records[0]
    assert client._transport.state.did_fail()
    assert "oopsie" in record.message

    # test recovery
    http_send.side_effect = None
    client.capture_message("foo", handled=False)
    client.close()
    assert not client._transport.state.did_fail()
    client.close()
def test_config_by_environment():
    with mock.patch.dict("os.environ", {"ELASTIC_APM_SERVICE_NAME": "envapp", "ELASTIC_APM_SECRET_TOKEN": "envtoken"}):
        client = Client(metrics_interval="0ms")
        assert client.config.service_name == "envapp"
        assert client.config.secret_token == "envtoken"
        assert client.config.disable_send is False
    with mock.patch.dict("os.environ", {"ELASTIC_APM_DISABLE_SEND": "true"}):
        client = Client(metrics_interval="0ms")
        assert client.config.disable_send is True
    client.close()
Beispiel #4
0
def sending_elasticapm_client(request, validating_httpserver):
    validating_httpserver.serve_content(
        code=202, content='', headers={'Location': 'http://example.com/foo'})
    client_config = getattr(request, 'param', {})
    client_config.setdefault('server_url', validating_httpserver.url)
    client_config.setdefault('service_name', 'myapp')
    client_config.setdefault('secret_token', 'test_key')
    client_config.setdefault('transport_class',
                             'elasticapm.transport.http.Transport')
    client = Client(**client_config)
    client.httpserver = validating_httpserver
    yield client
    client.close()
 def test_client_shutdown_async(self, mock_traces_collect, mock_send):
     client = Client(
         servers=['http://example.com'],
         app_name='app_name',
         secret_token='secret',
         async_mode=True,
     )
     client.send(auth_header='foo', **{
         'foo': 'bar',
     })
     client.close()
     self.assertEqual(mock_traces_collect.call_count, 1)
     self.assertEqual(mock_send.call_count, 1)
Beispiel #6
0
def sending_elasticapm_client(request, validating_httpserver):
    validating_httpserver.serve_content(code=202, content="", headers={"Location": "http://example.com/foo"})
    client_config = getattr(request, "param", {})
    client_config.setdefault("server_url", validating_httpserver.url)
    client_config.setdefault("service_name", "myapp")
    client_config.setdefault("secret_token", "test_key")
    client_config.setdefault("transport_class", "elasticapm.transport.http.Transport")
    client_config.setdefault("span_frames_min_duration", -1)
    client_config.setdefault("include_paths", ("*/tests/*",))
    client_config.setdefault("metrics_interval", "0ms")
    client = Client(**client_config)
    client.httpserver = validating_httpserver
    yield client
    client.close()
Beispiel #7
0
def test_config_by_environment():
    with mock.patch.dict('os.environ', {
        'ELASTIC_APM_SERVICE_NAME': 'app',
        'ELASTIC_APM_SECRET_TOKEN': 'token',
    }):
        client = Client()
        assert client.config.service_name == 'app'
        assert client.config.secret_token == 'token'
        assert client.config.disable_send is False
    with mock.patch.dict('os.environ', {
        'ELASTIC_APM_DISABLE_SEND': 'true',
    }):
        client = Client()
        assert client.config.disable_send is True
    client.close()
Beispiel #8
0
def test_config_non_string_types():
    """
    tests if we can handle non string types as configuration, e.g.
    Value types from django-configuration
    """
    class MyValue(object):
        def __init__(self, content):
            self.content = content

        def __str__(self):
            return str(self.content)

        def __repr__(self):
            return repr(self.content)

    client = Client(server_url="localhost",
                    service_name=MyValue("bar"),
                    secret_token=MyValue("bay"))
    assert isinstance(client.config.secret_token, compat.string_types)
    assert isinstance(client.config.service_name, compat.string_types)
    client.close()
Beispiel #9
0
def test_send_remote_failover_sync_stdlib(should_try, http_send):
    should_try.return_value = True

    client = Client(
        server_url='http://example.com',
        service_name='app_name',
        secret_token='secret',
        transport_class='elasticapm.transport.http.Transport',
    )
    logger = mock.Mock()
    client.error_logger.error = logger

    # test error
    http_send.side_effect = ValueError('oopsie')
    client.send('http://example.com/api/store', **{'message': 'oh no'})
    assert client.state.status == client.state.ERROR
    assert len(logger.call_args_list) == 1
    assert 'oopsie' in logger.call_args_list[0][0][1]

    # test recovery
    http_send.side_effect = None
    client.send('http://example.com/api/store', **{'message': 'oh no'})
    assert client.state.status == client.state.ONLINE
    client.close()
Beispiel #10
0
def sending_elasticapm_client(request, validating_httpserver):
    validating_httpserver.serve_content(
        code=202, content="", headers={"Location": "http://example.com/foo"})
    client_config = getattr(request, "param", {})
    client_config.setdefault("server_url", validating_httpserver.url)
    client_config.setdefault("service_name", "myapp")
    client_config.setdefault("secret_token", "test_key")
    client_config.setdefault("transport_class",
                             "elasticapm.transport.http.Transport")
    client_config.setdefault("span_frames_min_duration", -1)
    client_config.setdefault("span_compression_exact_match_max_duration",
                             "0ms")
    client_config.setdefault("span_compression_same_kind_max_duration", "0ms")
    client_config.setdefault("include_paths", ("*/tests/*", ))
    client_config.setdefault("metrics_interval", "0ms")
    client_config.setdefault("central_config", "false")
    client_config.setdefault("server_version", (8, 0, 0))
    client = Client(**client_config)
    client.httpserver = validating_httpserver
    yield client
    client.close()
    # clear any execution context that might linger around
    execution_context.set_transaction(None)
    execution_context.set_span(None)
Beispiel #11
0
def test_send_remote_failover_sync_stdlib(should_try, http_send):
    should_try.return_value = True

    client = Client(
        server_url="http://example.com",
        service_name="app_name",
        secret_token="secret",
        transport_class="elasticapm.transport.http.Transport",
    )
    logger = mock.Mock()
    client.error_logger.error = logger

    # test error
    http_send.side_effect = ValueError("oopsie")
    client.send("http://example.com/api/store", **{"message": "oh no"})
    assert client.state.status == client.state.ERROR
    assert len(logger.call_args_list) == 1
    assert "oopsie" in logger.call_args_list[0][0][1]

    # test recovery
    http_send.side_effect = None
    client.send("http://example.com/api/store", **{"message": "oh no"})
    assert client.state.status == client.state.ONLINE
    client.close()