test_metrics_extraction.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import uuid
  2. import confluent_kafka as kafka
  3. from sentry.sentry_metrics.indexer.strings import SHARED_STRINGS
  4. from sentry.tasks.relay import compute_projectkey_config
  5. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. from sentry.testutils.helpers.features import Feature
  8. from sentry.utils import json
  9. class MetricsExtractionTest(RelayStoreHelper, TransactionTestCase):
  10. def test_all_transaction_metrics_emitted(self):
  11. with Feature(
  12. {
  13. "organizations:transaction-metrics-extraction": True,
  14. "organizations:performance-ops-breakdown": True,
  15. }
  16. ):
  17. event_data = {
  18. "type": "transaction",
  19. "transaction": "foo",
  20. "timestamp": iso_format(before_now(seconds=1)),
  21. "start_timestamp": iso_format(before_now(seconds=2)),
  22. "contexts": {
  23. "trace": {
  24. "trace_id": 32 * "b",
  25. "span_id": 16 * "c",
  26. "type": "trace",
  27. }
  28. },
  29. "user": {"id": 123},
  30. "measurements": {
  31. "fp": {"value": 2258.060000000114},
  32. "fcp": {"value": 2258.060000000114},
  33. "lcp": {"value": 2807.335},
  34. "inp": {"value": 51.318},
  35. "fid": {"value": 3.4900000027846545},
  36. "cls": {"value": 0.0382},
  37. "frames_total": {"value": 100},
  38. "frames_slow": {"value": 10},
  39. "frames_frozen": {"value": 5},
  40. "stall_count": {"value": 2},
  41. "stall_total_time": {"value": 12},
  42. "stall_longest_time": {"value": 7},
  43. "app_start_warm": {"value": 0.001},
  44. "app_start_cold": {"value": 0.001},
  45. "ttfb": {"value": 5},
  46. "ttfb.requesttime": {"value": 6},
  47. },
  48. "spans": [
  49. {
  50. "op": op,
  51. "trace_id": 32 * "b",
  52. "span_id": 16 * "1",
  53. "start_timestamp": iso_format(before_now(seconds=2)),
  54. "timestamp": iso_format(before_now(seconds=1)),
  55. }
  56. for op in ("db", "http", "resource", "browser", "ui")
  57. ],
  58. }
  59. settings = {
  60. "bootstrap.servers": "127.0.0.1:9092", # TODO: read from django settings here
  61. "group.id": "test-consumer-%s" % uuid.uuid4().hex,
  62. "enable.auto.commit": True,
  63. "auto.offset.reset": "earliest",
  64. }
  65. consumer = kafka.Consumer(settings)
  66. consumer.assign([kafka.TopicPartition("ingest-metrics", 0)])
  67. self.post_and_retrieve_event(event_data)
  68. metrics_emitted = set()
  69. strings_emitted = set()
  70. for _ in range(1000):
  71. message = consumer.poll(timeout=1.0)
  72. if message is None:
  73. break
  74. message = json.loads(message.value())
  75. if message["project_id"] == self.project.id:
  76. metrics_emitted.add(message["name"])
  77. strings_emitted.add(message["name"])
  78. for key, value in message["tags"].items():
  79. strings_emitted.add(key)
  80. strings_emitted.add(value)
  81. consumer.close()
  82. # Make sure that all expected metrics were extracted:
  83. project_config = compute_projectkey_config(self.projectkey)
  84. extraction_config = project_config["config"]["transactionMetrics"]
  85. metrics_expected = set(extraction_config["extractMetrics"])
  86. assert sorted(metrics_emitted) == sorted(metrics_expected)
  87. #: These strings should be common strings, but we cannot add them
  88. #: to the indexer because they already exist in the release health
  89. #: indexer db.
  90. known_non_common_strings = {
  91. "other",
  92. "platform",
  93. "d:transactions/measurements.inp@millisecond",
  94. }
  95. # Make sure that all the standard strings are part of the list of common strings:
  96. non_common_strings = strings_emitted - SHARED_STRINGS.keys()
  97. assert non_common_strings == known_non_common_strings