metrics.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.core.cache import cache
  2. from django.db.models import Count
  3. from prometheus_client import Counter, Gauge
  4. organizations_metric = Gauge("glitchtip_organizations", "Number of organizations")
  5. projects_metric = Gauge(
  6. "glitchtip_projects", "Number of projects per organization", ["organization"]
  7. )
  8. issues_counter = Counter(
  9. "glitchtip_issues",
  10. "Issue creation counter per project",
  11. ["project", "organization"],
  12. )
  13. events_counter = Counter(
  14. "glitchtip_events",
  15. "Events creation counter per project",
  16. ["project", "organization", "issue"],
  17. )
  18. OBSERVABILITY_ORG_CACHE_KEY = "observability_org_metrics"
  19. def compile_metrics():
  20. """Update and cache the organization and project metrics"""
  21. from organizations_ext.models import Organization # avoid circular import
  22. orgs = cache.get_or_set(
  23. OBSERVABILITY_ORG_CACHE_KEY,
  24. lambda: Organization.objects.annotate(Count("projects"))
  25. .values("slug", "projects__count")
  26. .all(),
  27. timeout=None,
  28. )
  29. for org in orgs:
  30. projects_metric.labels(org["slug"]).set(org["projects__count"])
  31. organizations_metric.set(orgs.count())
  32. def clear_metrics_cache():
  33. cache.delete(OBSERVABILITY_ORG_CACHE_KEY)