test_image_block_builder.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import uuid
  2. from datetime import timedelta
  3. from unittest.mock import patch
  4. import pytest
  5. from sentry.integrations.slack.message_builder.image_block_builder import ImageBlockBuilder
  6. from sentry.issues.grouptype import (
  7. PerformanceHTTPOverheadGroupType,
  8. PerformanceP95EndpointRegressionGroupType,
  9. ProfileFunctionRegressionType,
  10. )
  11. from sentry.models.group import Group
  12. from sentry.testutils.cases import (
  13. AcceptanceTestCase,
  14. MetricsEnhancedPerformanceTestCase,
  15. ProfilesSnubaTestCase,
  16. )
  17. from sentry.testutils.helpers.datetime import before_now
  18. from sentry.testutils.helpers.features import with_feature
  19. from tests.sentry.issues.test_utils import OccurrenceTestMixin
  20. pytestmark = pytest.mark.sentry_metrics
  21. class TestSlackImageBlockBuilder(
  22. AcceptanceTestCase,
  23. MetricsEnhancedPerformanceTestCase,
  24. ProfilesSnubaTestCase,
  25. OccurrenceTestMixin,
  26. ):
  27. def setUp(self):
  28. super().setUp()
  29. self.features = {
  30. "organizations:performance-use-metrics": True,
  31. "organizations:slack-endpoint-regression-image": True,
  32. "organizations:slack-function-regression-image": True,
  33. }
  34. @with_feature("organizations:slack-endpoint-regression-image")
  35. def test_image_block_for_endpoint_regression(self):
  36. for i in range(10):
  37. event_id = uuid.uuid4().hex
  38. _ = self.process_occurrence(
  39. project_id=self.project.id,
  40. event_id=event_id,
  41. type=PerformanceP95EndpointRegressionGroupType.type_id,
  42. event_data={
  43. "fingerprint": ["group-1"],
  44. "timestamp": before_now(minutes=i + 10).isoformat(),
  45. "transaction": "/books/",
  46. },
  47. evidence_data={
  48. "breakpoint": before_now(minutes=i + 10).timestamp(),
  49. },
  50. )
  51. self.store_transaction_metric(
  52. metric="transaction.duration",
  53. tags={"transaction": "/books/"},
  54. value=1,
  55. timestamp=before_now(minutes=i + 10),
  56. project=self.project.id,
  57. )
  58. group = Group.objects.first()
  59. group.update(type=PerformanceP95EndpointRegressionGroupType.type_id)
  60. with self.feature(self.features):
  61. image_block = ImageBlockBuilder(group=group).build_image_block()
  62. assert image_block and "type" in image_block and image_block["type"] == "image"
  63. assert "_media/" in image_block["image_url"]
  64. @with_feature("organizations:slack-function-regression-image")
  65. def test_image_block_for_function_regression(self):
  66. hour_ago = (before_now(minutes=10) - timedelta(hours=1)).replace(
  67. minute=0, second=0, microsecond=0
  68. )
  69. for i in range(10):
  70. event_id = uuid.uuid4().hex
  71. _ = self.process_occurrence(
  72. project_id=self.project.id,
  73. event_id=event_id,
  74. type=ProfileFunctionRegressionType.type_id,
  75. event_data={
  76. "fingerprint": ["group-1"],
  77. "timestamp": before_now(minutes=i + 10).isoformat(),
  78. "function": "foo",
  79. },
  80. evidence_data={
  81. "breakpoint": before_now(minutes=i + 10).timestamp(),
  82. "fingerprint": self.function_fingerprint({"package": "foo", "function": "foo"}),
  83. "aggregate_range_1": 51588984.199999996,
  84. "aggregate_range_2": 839118611.8535699,
  85. },
  86. )
  87. self.store_functions(
  88. [
  89. {
  90. "self_times_ns": [100 for _ in range(100)],
  91. "package": "foo",
  92. "function": "foo",
  93. # only in app functions should
  94. # appear in the results
  95. "in_app": True,
  96. },
  97. ],
  98. project=self.project,
  99. timestamp=hour_ago,
  100. )
  101. group = Group.objects.first()
  102. with self.feature(self.features):
  103. image_block = ImageBlockBuilder(group=group).build_image_block()
  104. assert image_block and "type" in image_block and image_block["type"] == "image"
  105. assert "_media/" in image_block["image_url"]
  106. @patch("sentry_sdk.capture_exception")
  107. @with_feature("organizations:slack-function-regression-image")
  108. def test_image_not_generated_for_unsupported_issues(self, mock_capture_exception):
  109. group = self.create_group()
  110. group.update(type=PerformanceHTTPOverheadGroupType.type_id)
  111. image_block = ImageBlockBuilder(group=group).build_image_block()
  112. assert image_block is None
  113. assert mock_capture_exception.call_count == 0