test_image_block_builder.py 4.2 KB

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