test_mri.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_value(self):
  44. config = {
  45. "spanAttribute": "my_duration",
  46. "aggregates": ["avg", "min", "max", "sum"],
  47. "unit": "millisecond",
  48. "tags": [],
  49. "conditions": [
  50. {"value": "browser.name:Chrome or browser.name:Firefox"},
  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_value(f"avg(c:custom/span_attribute_{condition_id}@none)", "1000")
  60. == "1 s"
  61. )