tests.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.shortcuts import reverse
  2. from model_bakery import baker
  3. from prometheus_client import Metric
  4. from rest_framework import status
  5. from rest_framework.test import APIClient, APITestCase
  6. from events.tests.tests import get_sample_value, parse_prometheus_text
  7. from .metrics import clear_metrics_cache, organizations_metric, projects_metric
  8. class ObservabilityAPITestCase(APITestCase):
  9. def setUp(self):
  10. self.client = APIClient()
  11. self.user = baker.make("users.user", is_staff=True)
  12. self.client.force_login(self.user)
  13. self.url = reverse("prometheus-django-metrics")
  14. def _get_metrics(self) -> list[Metric]:
  15. resp = self.client.get(self.url)
  16. return parse_prometheus_text(resp.content.decode("utf-8"))
  17. def test_get_metrics_and_cache(self):
  18. clear_metrics_cache()
  19. with self.assertNumQueries(3):
  20. resp = self.client.get(self.url)
  21. self.assertEqual(resp.status_code, status.HTTP_200_OK)
  22. with self.assertNumQueries(2):
  23. resp = self.client.get(self.url)
  24. self.assertEqual(resp.status_code, status.HTTP_200_OK)
  25. def test_org_metric(self):
  26. before_orgs_metric = get_sample_value(
  27. self._get_metrics(),
  28. organizations_metric._name,
  29. organizations_metric._type,
  30. {},
  31. )
  32. # create new org; must invalidate the cache
  33. org = baker.make("organizations_ext.Organization")
  34. metrics = self._get_metrics()
  35. orgs_metric = get_sample_value(
  36. metrics, organizations_metric._name, organizations_metric._type, {}
  37. )
  38. self.assertEqual(orgs_metric, before_orgs_metric + 1)
  39. # delete org and test again
  40. org.delete()
  41. metrics = self._get_metrics()
  42. orgs_metric = get_sample_value(
  43. metrics, organizations_metric._name, organizations_metric._type, {}
  44. )
  45. self.assertEqual(orgs_metric, before_orgs_metric)
  46. def test_project_metric(self):
  47. # create new org
  48. org = baker.make("organizations_ext.Organization")
  49. # no projects yet
  50. metrics = self._get_metrics()
  51. projs_metric = get_sample_value(
  52. metrics,
  53. projects_metric._name,
  54. projects_metric._type,
  55. {"organization": org.slug},
  56. )
  57. self.assertEqual(projs_metric, 0)
  58. # create new project
  59. proj = baker.make("projects.Project", organization=org)
  60. # test
  61. metrics = self._get_metrics()
  62. projs_metric = get_sample_value(
  63. metrics,
  64. projects_metric._name,
  65. projects_metric._type,
  66. {"organization": org.slug},
  67. )
  68. self.assertEqual(projs_metric, 1)
  69. # delete project
  70. proj.delete()
  71. # test
  72. metrics = self._get_metrics()
  73. projs_metric = get_sample_value(
  74. metrics,
  75. projects_metric._name,
  76. projects_metric._type,
  77. {"organization": org.slug},
  78. )
  79. self.assertEqual(projs_metric, 0)