Skip to content

Commit

Permalink
Remove report and replace with report_generic
Browse files Browse the repository at this point in the history
  • Loading branch information
hammady committed Dec 20, 2023
1 parent aeb85e8 commit d5e3a12
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 35 deletions.
10 changes: 1 addition & 9 deletions pyworker/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pyworker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
19 changes: 2 additions & 17 deletions tests/test_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down
12 changes: 6 additions & 6 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ 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,
job_latency=self.mocked_latency,
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand All @@ -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):
Expand Down

0 comments on commit d5e3a12

Please sign in to comment.