metrics.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. async def compile_metrics():
  20. """Update and cache the organization and project metrics"""
  21. from apps.organizations_ext.models import Organization # avoid circular import
  22. orgs = cache.get(OBSERVABILITY_ORG_CACHE_KEY)
  23. if orgs is None:
  24. orgs = [
  25. org
  26. async for org in Organization.objects.annotate(Count("projects"))
  27. .values("slug", "projects__count")
  28. .all()
  29. ]
  30. cache.set(OBSERVABILITY_ORG_CACHE_KEY, orgs, 60 * 60)
  31. for org in orgs:
  32. projects_metric.labels(org["slug"]).set(org["projects__count"])
  33. organizations_metric.set(len(orgs))
  34. def clear_metrics_cache():
  35. cache.delete(OBSERVABILITY_ORG_CACHE_KEY)