|
5 | 5 | from mock import call, patch |
6 | 6 |
|
7 | 7 | from edx_django_utils import monitoring |
| 8 | +from edx_django_utils.cache import RequestCache |
8 | 9 | from edx_django_utils.monitoring.middleware import MonitoringCustomMetricsMiddleware, MonitoringMemoryMiddleware |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestMonitoringCustomMetrics(TestCase): |
12 | 13 | """ |
13 | 14 | Test the monitoring_utils middleware and helpers |
14 | 15 | """ |
| 16 | + def setUp(self): |
| 17 | + super(TestMonitoringCustomMetrics, self).setUp() |
| 18 | + RequestCache.clear_all_namespaces() |
15 | 19 |
|
16 | 20 | @patch('newrelic.agent') |
17 | 21 | @override_settings(MIDDLEWARE=[ |
18 | 22 | 'edx_django_utils.cache.middleware.RequestCacheMiddleware', |
19 | 23 | 'edx_django_utils.monitoring.middleware.MonitoringCustomMetricsMiddleware', |
20 | 24 | ]) |
21 | | - def test_custom_metrics_with_new_relic(self, mock_newrelic_agent): |
| 25 | + def test_accumulate_and_increment(self, mock_newrelic_agent): |
22 | 26 | """ |
23 | 27 | Test normal usage of collecting custom metrics and reporting to New Relic |
24 | 28 | """ |
@@ -48,7 +52,39 @@ def test_custom_metrics_with_new_relic(self, mock_newrelic_agent): |
48 | 52 |
|
49 | 53 | # Assert call args to newrelic.agent.add_custom_parameter(). Due to |
50 | 54 | # the nature of python dicts, call order is undefined. |
51 | | - mock_newrelic_agent.add_custom_parameter.has_calls(nr_agent_calls_expected, any_order=True) |
| 55 | + mock_newrelic_agent.add_custom_parameter.assert_has_calls(nr_agent_calls_expected, any_order=True) |
| 56 | + |
| 57 | + @patch('newrelic.agent') |
| 58 | + @override_settings(MIDDLEWARE=[ |
| 59 | + 'edx_django_utils.cache.middleware.RequestCacheMiddleware', |
| 60 | + 'edx_django_utils.monitoring.middleware.MonitoringCustomMetricsMiddleware', |
| 61 | + ]) |
| 62 | + def test_accumulate_with_illegal_value(self, mock_newrelic_agent): |
| 63 | + """ |
| 64 | + Test monitoring accumulate with illegal value that can't be added. |
| 65 | + """ |
| 66 | + monitoring.accumulate('hello', None) |
| 67 | + monitoring.accumulate('hello', 10) |
| 68 | + |
| 69 | + # based on the metric data above, we expect the following calls to newrelic: |
| 70 | + nr_agent_calls_expected = [ |
| 71 | + call('hello', None), |
| 72 | + call('error_adding_accumulated_metric', 'name=hello, new_value=10, cached_value=None'), |
| 73 | + ] |
| 74 | + |
| 75 | + # fake a response to trigger metrics reporting |
| 76 | + MonitoringCustomMetricsMiddleware().process_response( |
| 77 | + 'fake request', |
| 78 | + 'fake response', |
| 79 | + ) |
| 80 | + |
| 81 | + # Assert call counts to newrelic.agent.add_custom_parameter() |
| 82 | + expected_call_count = len(nr_agent_calls_expected) |
| 83 | + measured_call_count = mock_newrelic_agent.add_custom_parameter.call_count |
| 84 | + self.assertEqual(expected_call_count, measured_call_count) |
| 85 | + |
| 86 | + # Assert call args to newrelic.agent.add_custom_parameter(). |
| 87 | + mock_newrelic_agent.add_custom_parameter.assert_has_calls(nr_agent_calls_expected, any_order=True) |
52 | 88 |
|
53 | 89 | @override_settings(MIDDLEWARE=[ |
54 | 90 | 'edx_django_utils.cache.middleware.RequestCacheMiddleware', |
|
0 commit comments