|
@@ -1,5 +1,7 @@
|
|
|
from unittest import mock
|
|
|
|
|
|
+import pytest
|
|
|
+
|
|
|
from sentry.integrations.base import IntegrationDomain
|
|
|
from sentry.integrations.types import EventLifecycleOutcome
|
|
|
from sentry.integrations.utils.metrics import IntegrationEventLifecycleMetric
|
|
@@ -7,6 +9,10 @@ from sentry.testutils.cases import TestCase
|
|
|
from sentry.testutils.silo import no_silo_test
|
|
|
|
|
|
|
|
|
+class ExampleException(Exception):
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
@no_silo_test
|
|
|
class IntegrationEventLifecycleMetricTest(TestCase):
|
|
|
class TestLifecycleMetric(IntegrationEventLifecycleMetric):
|
|
@@ -73,21 +79,108 @@ class IntegrationEventLifecycleMetricTest(TestCase):
|
|
|
|
|
|
@mock.patch("sentry.integrations.utils.metrics.logger")
|
|
|
@mock.patch("sentry.integrations.utils.metrics.metrics")
|
|
|
- def test_recording_failure(self, mock_metrics, mock_logger):
|
|
|
- class TestException(Exception):
|
|
|
- pass
|
|
|
+ def test_recording_explicit_halt_with_exception(self, mock_metrics, mock_logger):
|
|
|
+ metric_obj = self.TestLifecycleMetric()
|
|
|
+ with metric_obj.capture() as lifecycle:
|
|
|
+ lifecycle.add_extra("extra", "value")
|
|
|
+ lifecycle.record_halt(ExampleException(""), extra={"even": "more"})
|
|
|
|
|
|
+ self._check_metrics_call_args(mock_metrics, "halted")
|
|
|
+ mock_logger.warning.assert_called_once_with(
|
|
|
+ "integrations.slo.halted",
|
|
|
+ extra={
|
|
|
+ "extra": "value",
|
|
|
+ "even": "more",
|
|
|
+ "integration_domain": "messaging",
|
|
|
+ "integration_name": "my_integration",
|
|
|
+ "interaction_type": "my_interaction",
|
|
|
+ },
|
|
|
+ exc_info=mock.ANY,
|
|
|
+ )
|
|
|
+
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.logger")
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.metrics")
|
|
|
+ def test_recording_explicit_halt_with_str(self, mock_metrics, mock_logger):
|
|
|
+ metric_obj = self.TestLifecycleMetric()
|
|
|
+ with metric_obj.capture() as lifecycle:
|
|
|
+ lifecycle.add_extra("extra", "value")
|
|
|
+ lifecycle.record_halt("Integration went boom", extra={"even": "more"})
|
|
|
+
|
|
|
+ self._check_metrics_call_args(mock_metrics, "halted")
|
|
|
+ mock_logger.warning.assert_called_once_with(
|
|
|
+ "integrations.slo.halted",
|
|
|
+ extra={
|
|
|
+ "outcome_reason": "Integration went boom",
|
|
|
+ "extra": "value",
|
|
|
+ "even": "more",
|
|
|
+ "integration_domain": "messaging",
|
|
|
+ "integration_name": "my_integration",
|
|
|
+ "interaction_type": "my_interaction",
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.logger")
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.metrics")
|
|
|
+ def test_recording_failure(self, mock_metrics, mock_logger):
|
|
|
metric_obj = self.TestLifecycleMetric()
|
|
|
- try:
|
|
|
+ with pytest.raises(ExampleException):
|
|
|
with metric_obj.capture() as lifecycle:
|
|
|
lifecycle.add_extra("extra", "value")
|
|
|
- raise TestException
|
|
|
- except TestException:
|
|
|
- pass
|
|
|
- else:
|
|
|
- self.fail()
|
|
|
+ raise ExampleException
|
|
|
+
|
|
|
+ self._check_metrics_call_args(mock_metrics, "failure")
|
|
|
+ mock_logger.error.assert_called_once_with(
|
|
|
+ "integrations.slo.failure",
|
|
|
+ extra={
|
|
|
+ "extra": "value",
|
|
|
+ "integration_domain": "messaging",
|
|
|
+ "integration_name": "my_integration",
|
|
|
+ "interaction_type": "my_interaction",
|
|
|
+ },
|
|
|
+ exc_info=mock.ANY,
|
|
|
+ )
|
|
|
+
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.logger")
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.metrics")
|
|
|
+ def test_recording_explicit_failure_with_exception(self, mock_metrics, mock_logger):
|
|
|
+ metric_obj = self.TestLifecycleMetric()
|
|
|
+ with metric_obj.capture() as lifecycle:
|
|
|
+ try:
|
|
|
+ lifecycle.add_extra("extra", "value")
|
|
|
+ raise ExampleException
|
|
|
+ except ExampleException as exc:
|
|
|
+ lifecycle.record_failure(exc, extra={"even": "more"})
|
|
|
+
|
|
|
+ self._check_metrics_call_args(mock_metrics, "failure")
|
|
|
+ mock_logger.error.assert_called_once_with(
|
|
|
+ "integrations.slo.failure",
|
|
|
+ extra={
|
|
|
+ "extra": "value",
|
|
|
+ "even": "more",
|
|
|
+ "integration_domain": "messaging",
|
|
|
+ "integration_name": "my_integration",
|
|
|
+ "interaction_type": "my_interaction",
|
|
|
+ },
|
|
|
+ exc_info=mock.ANY,
|
|
|
+ )
|
|
|
+
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.logger")
|
|
|
+ @mock.patch("sentry.integrations.utils.metrics.metrics")
|
|
|
+ def test_recording_explicit_failure_with_str(self, mock_metrics, mock_logger):
|
|
|
+ metric_obj = self.TestLifecycleMetric()
|
|
|
+ with metric_obj.capture() as lifecycle:
|
|
|
+ lifecycle.add_extra("extra", "value")
|
|
|
+ lifecycle.record_failure("Integration went boom", extra={"even": "more"})
|
|
|
|
|
|
self._check_metrics_call_args(mock_metrics, "failure")
|
|
|
mock_logger.error.assert_called_once_with(
|
|
|
- "integrations.slo.failure", extra={"extra": "value"}, exc_info=mock.ANY
|
|
|
+ "integrations.slo.failure",
|
|
|
+ extra={
|
|
|
+ "outcome_reason": "Integration went boom",
|
|
|
+ "extra": "value",
|
|
|
+ "even": "more",
|
|
|
+ "integration_domain": "messaging",
|
|
|
+ "integration_name": "my_integration",
|
|
|
+ "interaction_type": "my_interaction",
|
|
|
+ },
|
|
|
)
|