diff --git a/pyworker/reporter.py b/pyworker/reporter.py index 8cff213..9217e80 100644 --- a/pyworker/reporter.py +++ b/pyworker/reporter.py @@ -13,15 +13,7 @@ def __init__(self, attribute_prefix='', logger=None): newrelic.agent.initialize() self._newrelic_app = newrelic.agent.register_application() - def report(self, review_id, articles_count, **attributes): - # extend attributes with review_id and articles_count - attributes.update({ - 'review_id': review_id, - 'articles_count': articles_count - }) - self.report_generic(**attributes) - - def report_generic(self, **attributes): + def report(self, **attributes): # format attributes attributes = self._format_attributes(attributes) # report to NewRelic diff --git a/pyworker/worker.py b/pyworker/worker.py index 5e4a13d..1791bde 100644 --- a/pyworker/worker.py +++ b/pyworker/worker.py @@ -77,7 +77,7 @@ def _latency(job_run_at): with self.reporter.recorder(job.job_name) as task: # Record custom attributes for the job transaction - self.reporter.report_generic( + self.reporter.report( job_id=job.job_id, job_name=job.job_name, job_queue=job.queue, @@ -88,7 +88,7 @@ def _latency(job_run_at): # Record extra fields if configured self.logger.debug('job extra fields: %s' % job.extra_fields) if job.extra_fields is not None: - self.reporter.report_generic(**job.extra_fields) + self.reporter.report(**job.extra_fields) yield task else: @@ -181,7 +181,7 @@ def handle_job(self, job): finally: # report error status if self.reporter: - self.reporter.report_generic(error=error, job_failure=failed) + self.reporter.report(error=error, job_failure=failed) if caught_exception: self.reporter.record_exception(caught_exception) time_diff = time.time() - start_time diff --git a/tests/test_reporter.py b/tests/test_reporter.py index 35de207..eb7f0bb 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -27,28 +27,13 @@ def test_reporter_init_initializes_newrelic(self, newrelic_agent): #********** .report tests **********# - @patch('pyworker.reporter.Reporter.report_generic') - def test_reporter_report_merges_attributes(self, mock_report_generic): - reporter = Reporter() - reporter.report(review_id=1, articles_count=2, - test_attribute1=3, test_attribute2=4) - - mock_report_generic.assert_called_once_with( - test_attribute1=3, - review_id=1, - test_attribute2=4, - articles_count=2 - ) - - #********** .report_generic tests **********# - @patch('pyworker.reporter.Reporter._report_newrelic') @patch('pyworker.reporter.Reporter._format_attributes') - def test_reporter_report_generic_formats_attributes_and_reports_to_newrelic( + def test_reporter_report_formats_attributes_and_reports_to_newrelic( self, mock_format_attributes, mock_report_newrelic): reporter = Reporter() mock_format_attributes.return_value = {'formatted_attribute': '1'} - reporter.report_generic(test_attribute=1) + reporter.report(test_attribute=1) mock_format_attributes.assert_called_once_with({'test_attribute': 1}) mock_report_newrelic.assert_called_once_with({'formatted_attribute': '1'}) diff --git a/tests/test_worker.py b/tests/test_worker.py index 3a32e33..6f21f72 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -108,7 +108,7 @@ def test_worker_run_when_job_found_handles_job(self, mock_get_job, mock_handle_j def assert_instrument_context_reports_custom_attributes(self, job, reporter): reporter.recorder.assert_called_once() - reporter.report_generic.assert_any_call( + reporter.report.assert_any_call( job_id=job.job_id, job_name=job.job_name, job_queue=job.queue, @@ -116,7 +116,7 @@ def assert_instrument_context_reports_custom_attributes(self, job, reporter): job_attempts=job.attempts ) if job.extra_fields is not None: - reporter.report_generic.assert_any_call(**job.extra_fields) + reporter.report.assert_any_call(**job.extra_fields) def test_worker_handle_job_when_job_is_none_does_nothing(self): self.worker.handle_job(None) # no error raised @@ -144,7 +144,7 @@ def test_worker_handle_job_when_job_is_unsupported_type_reports_error( self.assert_instrument_context_reports_custom_attributes(job, reporter) reporter.record_exception.assert_called_once() - reporter.report_generic.assert_any_call(error=True, job_failure=False) + reporter.report.assert_any_call(error=True, job_failure=False) @patch('pyworker.worker.get_current_time') def test_worker_handle_job_when_job_is_unsupported_type_reports_extra_fields( @@ -182,7 +182,7 @@ def test_worker_handle_job_when_no_errors_reports_success( self.worker.handle_job(job) reporter.record_exception.assert_not_called() - reporter.report_generic.assert_any_call(error=False, job_failure=False) + reporter.report.assert_any_call(error=False, job_failure=False) self.assert_instrument_context_reports_custom_attributes(job, reporter) def test_worker_handle_job_when_error_sets_error_and_unlocks_job(self): @@ -210,7 +210,7 @@ def test_worker_handle_job_when_error_reports_error(self, self.assert_instrument_context_reports_custom_attributes(job, reporter) reporter.record_exception.assert_called_once() - reporter.report_generic.assert_any_call(error=True, job_failure=False) + reporter.report.assert_any_call(error=True, job_failure=False) job.remove.assert_not_called() @patch('pyworker.worker.get_current_time') @@ -228,7 +228,7 @@ def test_worker_handle_job_when_permanent_error_reports_failure( self.assert_instrument_context_reports_custom_attributes(job, reporter) reporter.record_exception.assert_called_once() - reporter.report_generic.assert_any_call(error=True, job_failure=True) + reporter.report.assert_any_call(error=True, job_failure=True) job.remove.assert_not_called() def test_worker_handle_job_when_error_is_termination_error_bubbles_up(self):