Addressing feedback.

- When an error occurs, still check if the periodic supportability metric should be emitted
- Added a check to ensure the extension is ready in the error handler to match _process_success
This commit is contained in:
Josh Bonczkowski 2026-03-13 13:58:20 -04:00
parent 2cfacd611b
commit 275252009c
2 changed files with 23 additions and 6 deletions

View file

@ -595,6 +595,11 @@ class NewRelicLogger(CustomLogger):
def _record_error_metric(self):
"""Record error metric to New Relic."""
try:
if not self.enabled:
return
self._check_and_emit_periodic_metric()
import newrelic.agent
app = newrelic.agent.application()

View file

@ -415,26 +415,38 @@ class TestProcessSuccess:
class TestRecordErrorMetric:
def setup_method(self):
self.logger = make_logger()
def test_calls_record_custom_metric(self):
logger = make_logger()
mock_app = MagicMock()
mock_app.enabled = True
with patch("newrelic.agent.application", return_value=mock_app):
logger._record_error_metric()
with patch.object(self.logger, "_check_and_emit_periodic_metric"):
with patch("newrelic.agent.application", return_value=mock_app):
self.logger._record_error_metric()
mock_app.record_custom_metric.assert_called_once_with("LLM/LiteLLM/Error", 1)
def test_skips_when_app_disabled(self):
logger = make_logger()
mock_app = MagicMock()
mock_app.enabled = False
with patch("newrelic.agent.application", return_value=mock_app):
logger._record_error_metric()
with patch.object(self.logger, "_check_and_emit_periodic_metric"):
with patch("newrelic.agent.application", return_value=mock_app):
self.logger._record_error_metric()
mock_app.record_custom_metric.assert_not_called()
def test_calls_check_and_emit_periodic_metric(self):
with patch.object(
self.logger, "_check_and_emit_periodic_metric"
) as mock_periodic:
with patch("newrelic.agent.application", return_value=MagicMock()):
self.logger._record_error_metric()
mock_periodic.assert_called_once()
# ---------------------------------------------------------------------------
# 9. _emit_supportability_metric