test_mri.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from typing import cast
  2. import pytest
  3. from sentry.snuba.metrics.naming_layer.mri import format_mri_field, format_mri_field_value
  4. from sentry.testutils.cases import TestCase
  5. pytestmark = pytest.mark.sentry_metrics
  6. class TestMRIUtils(TestCase):
  7. def test_format_mri_field(self):
  8. assert format_mri_field("avg(c:custom/foo@none)") == "avg(foo)"
  9. assert format_mri_field("max(s:spans/user@none)") == "max(user)"
  10. assert format_mri_field("sum(d:spans/exclusive_time@millisecond)") == "sum(exclusive_time)"
  11. assert (
  12. format_mri_field("sum(c:custom/http.client.active_requests@{request})")
  13. == "sum(http.client.active_requests)"
  14. )
  15. assert format_mri_field("sum(c:custom/foo...bar@{request})") == "sum(foo...bar)"
  16. assert format_mri_field("invalid_mri_field") == "invalid_mri_field"
  17. assert format_mri_field(cast(str, None)) is None
  18. def test_format_mri_field_value(self):
  19. assert format_mri_field_value("avg(c:custom/foo@none)", "100.0") == "100.0"
  20. assert format_mri_field_value("count(s:spans/user@none)", "100") == "100"
  21. assert format_mri_field_value("sum(d:spans/exclusive_time@millisecond)", "1000") == "1 s"
  22. assert format_mri_field_value("invalid_mri_field", "100") == "100"
  23. assert format_mri_field_value(cast(str, None), "100") == "100"
  24. def test_span_metric_mri_field(self):
  25. config = {
  26. "spanAttribute": "browser.name",
  27. "aggregates": ["count_unique"],
  28. "unit": "none",
  29. "tags": [],
  30. "conditions": [
  31. {"value": "browser.name:Chrome or browser.name:Firefox"},
  32. ],
  33. }
  34. project = self.create_project(organization=self.organization, name="my new project")
  35. config_object = self.create_span_attribute_extraction_config(
  36. dictionary=config, user_id=self.user.id, project=project
  37. )
  38. condition_id = config_object.conditions.get().id
  39. assert (
  40. format_mri_field(f"count_unique(c:custom/span_attribute_{condition_id}@none)")
  41. == 'count_unique(browser.name) filtered by "browser.name:Chrome or browser.name:Firefox"'
  42. )
  43. def test_span_metric_mri_field_empty_condition(self):
  44. config = {
  45. "spanAttribute": "browser.name",
  46. "aggregates": ["count_unique"],
  47. "unit": "none",
  48. "tags": [],
  49. "conditions": [
  50. {"value": ""},
  51. ],
  52. }
  53. project = self.create_project(organization=self.organization, name="my new project")
  54. config_object = self.create_span_attribute_extraction_config(
  55. dictionary=config, user_id=self.user.id, project=project
  56. )
  57. condition_id = config_object.conditions.get().id
  58. assert (
  59. format_mri_field(f"count_unique(c:custom/span_attribute_{condition_id}@none)")
  60. == "count_unique(browser.name)"
  61. )
  62. def test_span_metric_mri_field_value(self):
  63. config = {
  64. "spanAttribute": "my_duration",
  65. "aggregates": ["avg", "min", "max", "sum"],
  66. "unit": "millisecond",
  67. "tags": [],
  68. "conditions": [
  69. {"value": "browser.name:Chrome or browser.name:Firefox"},
  70. ],
  71. }
  72. project = self.create_project(organization=self.organization, name="my new project")
  73. config_object = self.create_span_attribute_extraction_config(
  74. dictionary=config, user_id=self.user.id, project=project
  75. )
  76. condition_id = config_object.conditions.get().id
  77. assert (
  78. format_mri_field_value(f"avg(c:custom/span_attribute_{condition_id}@none)", "1000")
  79. == "1 s"
  80. )
  81. def test_span_metric_does_not_exist(self):
  82. config = {
  83. "spanAttribute": "my_duration",
  84. "aggregates": ["avg", "min", "max", "sum"],
  85. "unit": "millisecond",
  86. "tags": [],
  87. "conditions": [
  88. {"value": "browser.name:Chrome or browser.name:Firefox"},
  89. ],
  90. }
  91. project = self.create_project(organization=self.organization, name="my new project")
  92. self.create_span_attribute_extraction_config(
  93. dictionary=config, user_id=self.user.id, project=project
  94. )
  95. non_existent_id = 65537
  96. assert (
  97. format_mri_field_value(f"avg(c:custom/span_attribute_{non_existent_id}@none)", "1000")
  98. == "1000"
  99. )
  100. assert (
  101. format_mri_field(f"avg(c:custom/span_attribute_{non_existent_id}@none)")
  102. == f"avg(c:custom/span_attribute_{non_existent_id}@none)"
  103. )