def test_collect_elapsed_time_happy_path():
    """Decorating a Task's run method with @collect_elapsed_time should store some metric via the MetricClient."""
    mock_client = MockGraphiteClient()
    with mock.patch('exporter.metrics.get_graphite_client', return_value=mock_client) as mock_get_client, \
         mock.patch('exporter.metrics.datetime', MockDateTime):

        kwargs = {
            'graphite_host': 'the-host',
            'graphite_port': 'the-port',
            'graphite_prefix': 'the-prefix',
            'other_option': 'who-cares',
            'organization': 'edX',
            'course': 'the-course'
        }
        with collect_elapsed_time(MockTask, **kwargs):
            result = MockTask.run('my.file', True, **kwargs)

        assert 'the-result' == result
        emission_timestamp = timestamp(MockDateTime.NOW)
        assert 0.1 <= mock_client.metrics[(
            'edX.the-course.MockTask.elapsed_time', emission_timestamp)] < 0.2
        mock_get_client.assert_called_once_with(
            graphite_host='the-host',
            graphite_port='the-port',
            graphite_prefix='the-prefix',
        )
def test_no_graphite_client_configured():
    """If the graphite configuration information is not present, a Task's run() method should still run."""
    mock_client = MockFailingGraphiteClient()
    with mock.patch('exporter.metrics.get_graphite_client', return_value=mock_client) as mock_get_client, \
         mock.patch('exporter.metrics.datetime', MockDateTime):

        kwargs = {
            'other_option': 'who-cares',
            'organization': 'edX',
            'course': 'the-course',
        }
        with collect_elapsed_time(MockTask, **kwargs):
            result = MockTask.run('my.file', True, **kwargs)

        assert 'the-result' == result
        assert {} == mock_client.metrics
        mock_get_client.assert_called_once_with()
def test_collect_elapsed_time_no_org():
    """When no organization exists in kwargs, the collect_elapsed_time decorator should return without
    sending anything to graphite."""
    mock_client = MockGraphiteClient()
    with mock.patch('exporter.metrics.get_graphite_client', return_value=mock_client) as mock_get_client, \
         mock.patch('exporter.metrics.datetime', MockDateTime):

        kwargs = {
            'graphite_host': 'the-host',
            'graphite_port': 'the-port',
            'graphite_prefix': 'the-prefix',
            'other_option': 'who-cares',
            'course': 'the-course'
        }
        with collect_elapsed_time(MockTask, **kwargs):
            result = MockTask.run('my.file', True, **kwargs)

        assert 'the-result' == result
        assert not mock_get_client.called
        assert {} == mock_client.metrics
예제 #4
0
def _run_task(task, **kwargs):
    log.info("Running task %s", task.__name__)
    filename = task.get_filename(**kwargs)
    try:
        with collect_elapsed_time(task, **kwargs), \
             logging_streams_on_failure(task.__name__) as (output_file, error_file):
            task.run(filename,
                     stderr_file=error_file,
                     stdout_file=output_file,
                     **kwargs)
            log.info('Saving task results to %s', filename)
            return filename
    except FatalTaskError:
        log.exception('Task %s failed fatally to write to %s', task.__name__,
                      filename)
        raise
    except Exception:  # pylint: disable=broad-except
        failed_filename = task.write_failed_file(**kwargs)
        log.exception('Task %s failed fatally, writing failure file %s',
                      task.__name__, failed_filename)
        return failed_filename
def test_graphite_send_exception():
    """When a GraphiteSendException occurs, @collect_elapsed_time should not cause the run() method to fail."""
    mock_client = MockFailingGraphiteClient()
    with mock.patch('exporter.metrics.get_graphite_client', return_value=mock_client) as mock_get_client, \
         mock.patch('exporter.metrics.datetime', MockDateTime):

        kwargs = {
            'graphite_host': 'the-host',
            'graphite_port': 'the-port',
            'graphite_prefix': 'the-prefix',
            'other_option': 'who-cares',
            'organization': 'edX',
            'course': 'the-course',
        }
        with collect_elapsed_time(MockTask, **kwargs):
            result = MockTask.run('my.file', True, **kwargs)

        assert 'the-result' == result
        assert {} == mock_client.metrics
        mock_get_client.assert_called_once_with(
            graphite_host='the-host',
            graphite_port='the-port',
            graphite_prefix='the-prefix',
        )