test_metrics_extraction.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "fid": {"value": 3.4900000027846545},
  35. "cls": {"value": 0.0382},
  36. "frames_total": {"value": 100},
  37. "frames_slow": {"value": 10},
  38. "frames_frozen": {"value": 5},
  39. "stall_count": {"value": 2},
  40. "stall_total_time": {"value": 12},
  41. "stall_longest_time": {"value": 7},
  42. "app_start_warm": {"value": 0.001},
  43. "app_start_cold": {"value": 0.001},
  44. "ttfb": {"value": 5},
  45. "ttfb.requesttime": {"value": 6},
  46. },
  47. "spans": [
  48. {
  49. "op": op,
  50. "trace_id": 32 * "b",
  51. "span_id": 16 * "1",
  52. "start_timestamp": iso_format(before_now(seconds=2)),
  53. "timestamp": iso_format(before_now(seconds=1)),
  54. }
  55. for op in ("db", "http", "resource", "browser", "ui")
  56. ],
  57. }
  58. settings = {
  59. "bootstrap.servers": "127.0.0.1:9092", # TODO: read from django settings here
  60. "group.id": "test-consumer-%s" % uuid.uuid4().hex,
  61. "enable.auto.commit": True,
  62. "auto.offset.reset": "earliest",
  63. }
  64. consumer = kafka.Consumer(settings)
  65. consumer.assign([kafka.TopicPartition("ingest-metrics", 0)])
  66. self.post_and_retrieve_event(event_data)
  67. metrics_emitted = set()
  68. strings_emitted = set()
  69. for _ in range(1000):
  70. message = consumer.poll(timeout=1.0)
  71. if message is None:
  72. break
  73. message = json.loads(message.value())
  74. if message["project_id"] == self.project.id:
  75. metrics_emitted.add(message["name"])
  76. strings_emitted.add(message["name"])
  77. for key, value in message["tags"].items():
  78. strings_emitted.add(key)
  79. strings_emitted.add(value)
  80. consumer.close()
  81. # Make sure that all expected metrics were extracted:
  82. project_config = compute_projectkey_config(self.projectkey)
  83. extraction_config = project_config["config"]["transactionMetrics"]
  84. metrics_expected = set(extraction_config["extractMetrics"])
  85. assert sorted(metrics_emitted) == sorted(metrics_expected)
  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 = {"other", "platform"}
  90. # Make sure that all the standard strings are part of the list of common strings:
  91. non_common_strings = strings_emitted - SHARED_STRINGS.keys()
  92. assert non_common_strings == known_non_common_strings