diff --git a/litellm/integrations/newrelic/newrelic.py b/litellm/integrations/newrelic/newrelic.py index e0e61e9cacd..93fe09d6010 100644 --- a/litellm/integrations/newrelic/newrelic.py +++ b/litellm/integrations/newrelic/newrelic.py @@ -186,12 +186,13 @@ class NewRelicLogger(CustomLogger): # Record metric with value of 1 (will be aggregated by New Relic) app = newrelic.agent.application() + # Always update the timestamp so the 27-hour back-off applies + # regardless of whether the app is ready, preventing lock contention + # on every request when the agent is slow to register or never starts. + _last_metric_emission_time = time.time() + if app and app.enabled: app.record_custom_metric(metric_name, 1) - - # Update last emission time - _last_metric_emission_time = time.time() - verbose_logger.info( f"Emitted New Relic supportability metric: {metric_name}" ) diff --git a/tests/test_litellm/integrations/newrelic/test_newrelic.py b/tests/test_litellm/integrations/newrelic/test_newrelic.py index 166fec98b93..4bac745c7f1 100644 --- a/tests/test_litellm/integrations/newrelic/test_newrelic.py +++ b/tests/test_litellm/integrations/newrelic/test_newrelic.py @@ -607,12 +607,15 @@ class TestEmitSupportabilityMetric: with patch("newrelic.agent.application", return_value=mock_app): self.logger._emit_supportability_metric() mock_app.record_custom_metric.assert_not_called() - assert nr_module._last_metric_emission_time == 0.0 + # Timestamp is still updated to back off lock contention during registration. + assert nr_module._last_metric_emission_time != 0.0 def test_skips_when_no_app(self): with patch("newrelic.agent.application", return_value=None): self.logger._emit_supportability_metric() - assert nr_module._last_metric_emission_time == 0.0 + # Timestamp is updated even when app is None to back off lock contention + # if the agent never starts or is slow to initialise. + assert nr_module._last_metric_emission_time != 0.0 # ---------------------------------------------------------------------------