示例#1
0
def test_client_send(mock_redis_channel):
    to_send = "to_send"
    channel_default_args = {"arg1": "1", "arg2": 2}

    mock_redis_channel.DEFAULT_ARGS = channel_default_args
    mock_redis_channel.send = MagicMock()
    mock_channel_instance = mock_redis_channel.return_value

    client = kadabra.Kadabra()
    client.send(to_send)

    mock_channel_instance.send.assert_called_with(to_send)
示例#2
0
def test_client_ctor_defaults(mock_redis_channel):
    channel = "test"
    channel_default_args = {"arg1": "1", "arg2": 2}

    mock_redis_channel.return_value = channel
    mock_redis_channel.DEFAULT_ARGS = channel_default_args

    client = kadabra.Kadabra()

    mock_redis_channel.assert_called_with(**channel_default_args)
    assert client.default_dimensions == {}
    assert client.channel == channel
示例#3
0
def test_client_ctor_custom_channel_args(mock_redis_channel):
    channel = "test"
    channel_default_args = {"arg1": "1", "arg2": 2}
    channel_custom_args = {"arg2": 3}
    combined_args = channel_default_args.copy()
    combined_args.update(channel_custom_args)

    mock_redis_channel.return_value = channel
    mock_redis_channel.DEFAULT_ARGS = channel_default_args

    client = kadabra.Kadabra(\
            configuration={"CLIENT_CHANNEL_ARGS": combined_args})

    mock_redis_channel.assert_called_with(**combined_args)
    assert client.channel == channel
示例#4
0
    def init_app(self, app, config=None):
        """Configure the application to use Kadabra. Requests will have access
        to a :class:`~kadabra.client.MetricsCollector` via the ``metrics``
        attribute of Flask's :data:`~flask.g` object. You can record
        metrics anywhere in the context of a request like so::

            ...
            g.metrics.add_count("userSignup", 1)
            ...

        The metrics object will be closed and sent at the end of the
        request if any view that handles the request has been annotated with
        :data:`~flask_kadabra.record_metrics`."""
        app.kadabra = kadabra.Kadabra(config)
        self.app = app

        @app.before_request
        def initialize_metrics():
            ctx = stack.top
            if ctx is not None:
                ctx.kadabra_request_start_time = _get_now()
                g.metrics = current_app.kadabra.metrics()

        @app.after_request
        def transport_metrics(response):
            # Only send the metrics if the current view has "opted in".
            ctx = stack.top
            if ctx is not None and getattr(ctx, "enable_kadabra", False):
                end_time = _get_now()
                g.metrics.set_timer("RequestTime",
                        (end_time - ctx.kadabra_request_start_time),
                        kadabra.Units.MILLISECONDS)

                failure = 0
                client_error = 0
                if response.status_code >= 500:
                    failure = 1
                elif response.status_code >= 400:
                    client_error = 1

                g.metrics.add_count("Failure", failure)
                g.metrics.add_count("ClientError", client_error)

                closed = g.metrics.close()
                if not current_app.config.get("DISABLE_KADABRA"):
                    current_app.kadabra.send(closed)
            return response
示例#5
0
def test_client_metrics_no_default_dimensions(mock_redis_channel,
                                              mock_collector):
    timestamp_format = "timestamp_format"
    expected_collector = "test_collector"
    channel = "test"
    channel_default_args = {"arg1": "1", "arg2": 2}

    mock_redis_channel.return_value = channel
    mock_redis_channel.DEFAULT_ARGS = channel_default_args

    mock_collector.return_value = expected_collector

    client = kadabra.Kadabra(\
            configuration={"CLIENT_TIMESTAMP_FORMAT": timestamp_format})
    collector = client.metrics()

    mock_collector.assert_called_with(timestamp_format)
    assert collector == expected_collector
示例#6
0
    def __init__(self, get_response):
        self.get_response = get_response

        config = getattr(settings, 'KADABRA_CONFIG', {})
        self.kadabra = kadabra.Kadabra(config)
示例#7
0
def test_client_ctor_unrecognized_channel():
    with pytest.raises(Exception):
        kadabra.Kadabra(configuration={"CLIENT_CHANNEL_TYPE": "grenjiweroni"})