test_metrics_extraction.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import uuid
  2. import confluent_kafka as kafka
  3. import pytest
  4. from sentry.sentry_metrics.indexer.strings import SHARED_STRINGS
  5. from sentry.testutils.cases import TransactionTestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. from sentry.testutils.helpers.features import Feature
  8. from sentry.testutils.helpers.options import override_options
  9. from sentry.testutils.relay import RelayStoreHelper
  10. from sentry.testutils.skips import requires_kafka
  11. from sentry.utils import json
  12. pytestmark = [requires_kafka]
  13. class MetricsExtractionTest(RelayStoreHelper, TransactionTestCase):
  14. @pytest.mark.skip("breaks in Relay for unknown reasons")
  15. @override_options({"relay.transaction-names-client-based": 1.0})
  16. def test_all_transaction_metrics_emitted(self):
  17. with Feature(
  18. {
  19. "organizations:transaction-metrics-extraction": True,
  20. }
  21. ):
  22. event_data = {
  23. "type": "transaction",
  24. "transaction": "foo",
  25. "transaction_info": {"source": "url"}, # 'transaction' tag not extracted
  26. "timestamp": iso_format(before_now(seconds=1)),
  27. "start_timestamp": iso_format(before_now(seconds=2)),
  28. "contexts": {
  29. "trace": {
  30. "trace_id": 32 * "b",
  31. "span_id": 16 * "c",
  32. "type": "trace",
  33. }
  34. },
  35. "user": {"id": 123},
  36. "measurements": {
  37. "fp": {"value": 2258.060000000114},
  38. "fcp": {"value": 2258.060000000114},
  39. "lcp": {"value": 2807.335},
  40. "inp": {"value": 51.318},
  41. "fid": {"value": 3.4900000027846545},
  42. "cls": {"value": 0.0382},
  43. "frames_total": {"value": 100},
  44. "frames_slow": {"value": 10},
  45. "frames_frozen": {"value": 5},
  46. "stall_count": {"value": 2},
  47. "stall_total_time": {"value": 12},
  48. "stall_longest_time": {"value": 7},
  49. "app_start_warm": {"value": 0.001},
  50. "app_start_cold": {"value": 0.001},
  51. "ttfb": {"value": 5},
  52. "ttfb.requesttime": {"value": 6},
  53. },
  54. "spans": [
  55. {
  56. "op": op,
  57. "trace_id": 32 * "b",
  58. "span_id": 16 * "1",
  59. "start_timestamp": iso_format(before_now(seconds=2)),
  60. "timestamp": iso_format(before_now(seconds=1)),
  61. }
  62. for op in ("db", "http", "resource", "browser", "ui")
  63. ],
  64. }
  65. settings = {
  66. "bootstrap.servers": "127.0.0.1:9092", # TODO: read from django settings here
  67. "group.id": "test-consumer-%s" % uuid.uuid4().hex,
  68. "enable.auto.commit": True,
  69. "auto.offset.reset": "earliest",
  70. }
  71. consumer = kafka.Consumer(settings)
  72. consumer.assign([kafka.TopicPartition("ingest-metrics", 0)])
  73. self.post_and_retrieve_event(event_data)
  74. strings_emitted = set()
  75. for _ in range(1000):
  76. message = consumer.poll(timeout=1.0)
  77. if message is None:
  78. break
  79. message = json.loads(message.value())
  80. if message["project_id"] == self.project.id:
  81. strings_emitted.add(message["name"])
  82. for key, value in message["tags"].items():
  83. strings_emitted.add(key)
  84. strings_emitted.add(value)
  85. consumer.close()
  86. #: These strings should be common strings, but we cannot add them
  87. #: to the indexer because they already exist in the release health
  88. #: indexer db.
  89. known_non_common_strings = {
  90. "other",
  91. "platform",
  92. "d:transactions/measurements.inp@millisecond",
  93. }
  94. # Make sure that all the standard strings are part of the list of common strings:
  95. non_common_strings = strings_emitted - SHARED_STRINGS.keys()
  96. assert non_common_strings == known_non_common_strings
  97. def test_histogram_outliers(self):
  98. with Feature(
  99. {
  100. "organizations:transaction-metrics-extraction": True,
  101. }
  102. ):
  103. event_data = {
  104. "type": "transaction",
  105. "transaction": "foo",
  106. "transaction_info": {"source": "url"}, # 'transaction' tag not extracted
  107. "timestamp": iso_format(before_now(seconds=1)),
  108. "start_timestamp": iso_format(before_now(seconds=2)),
  109. "platform": "javascript",
  110. "contexts": {
  111. "trace": {
  112. "op": "pageload",
  113. "trace_id": 32 * "b",
  114. "span_id": 16 * "c",
  115. "type": "trace",
  116. }
  117. },
  118. "user": {"id": 123},
  119. "measurements": {
  120. "fcp": {"value": 999999999.0},
  121. "lcp": {"value": 0.0},
  122. },
  123. }
  124. settings = {
  125. "bootstrap.servers": "127.0.0.1:9092", # TODO: read from django settings here
  126. "group.id": "test-consumer-%s" % uuid.uuid4().hex,
  127. "enable.auto.commit": True,
  128. "auto.offset.reset": "earliest",
  129. }
  130. consumer = kafka.Consumer(settings)
  131. consumer.assign([kafka.TopicPartition("ingest-performance-metrics", 0)])
  132. self.post_and_retrieve_event(event_data)
  133. histogram_outlier_tags = {}
  134. for _ in range(1000):
  135. message = consumer.poll(timeout=1.0)
  136. if message is None:
  137. break
  138. bucket = json.loads(message.value())
  139. try:
  140. histogram_outlier_tags[bucket["name"]] = bucket["tags"]["histogram_outlier"]
  141. except KeyError:
  142. pass
  143. consumer.close()
  144. assert histogram_outlier_tags == {
  145. "d:transactions/duration@millisecond": "inlier",
  146. "d:transactions/measurements.fcp@millisecond": "outlier",
  147. "d:transactions/measurements.lcp@millisecond": "inlier",
  148. }