Эх сурвалжийг харах

HUE-9194 [useradmin] Make active users metrics available to prometheus

Ying Chen 5 жил өмнө
parent
commit
c78eaf1dc9

+ 7 - 0
apps/useradmin/src/useradmin/metrics.py

@@ -17,6 +17,7 @@
 import logging
 
 from datetime import datetime, timedelta
+from prometheus_client import Gauge
 
 from desktop.lib.metrics import global_registry
 from desktop.lib.security_util import get_localhost_name
@@ -40,6 +41,9 @@ global_registry().gauge_callback(
     numerator='users',
 )
 
+prometheus_active_users = Gauge('hue_active_users', 'Hue Active Users in All Instances')
+prometheus_active_users.set_function(active_users)
+
 def active_users_per_instance():
   from useradmin.models import UserProfile
   try:
@@ -56,3 +60,6 @@ global_registry().gauge_callback(
     description='Number of users that were active in the last hour on specific instance',
     numerator='users',
 )
+
+prometheus_active_users_instance = Gauge('hue_local_active_users', 'Hue Active Users in Local Instance')
+prometheus_active_users_instance.set_function(active_users_per_instance)

+ 32 - 3
apps/useradmin/src/useradmin/tests.py

@@ -39,7 +39,7 @@ import desktop.conf
 
 from desktop import appmanager
 from desktop.auth.backend import is_admin, create_user
-from desktop.conf import APP_BLACKLIST, ENABLE_ORGANIZATIONS
+from desktop.conf import APP_BLACKLIST, ENABLE_ORGANIZATIONS, ENABLE_PROMETHEUS
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.test_utils import grant_access
 from desktop.views import home
@@ -75,6 +75,12 @@ def reset_all_groups():
     grp.delete()
 
 
+def reset_all_user_profile():
+  """Reset to a clean state by deleting all user profiles"""
+  for up in UserProfile.objects.all():
+    up.delete()
+
+
 class LdapTestConnection(object):
   """
   Test class which mimics the behaviour of LdapConnection (from ldap_access.py).
@@ -301,8 +307,10 @@ class TestUserProfile(BaseUserAdminTests):
 
 class TestUserAdminMetrics(BaseUserAdminTests):
 
-  @override_settings(AUTHENTICATION_BACKENDS=['desktop.auth.backend.AllowFirstUserDjangoBackend'])
-  def test_active_users(self):
+  def setUp(self):
+    super(TestUserAdminMetrics, self).setUp()
+    reset_all_user_profile()
+
     with patch('useradmin.middleware.get_localhost_name') as get_hostname:
       get_hostname.return_value = 'host1'
 
@@ -327,6 +335,14 @@ class TestUserAdminMetrics(BaseUserAdminTests):
       userprofile3.hostname = 'host2'
       userprofile3.save()
 
+
+  def tearDown(self):
+    reset_all_user_profile()
+    super(TestUserAdminMetrics, self).tearDown()
+
+
+  @override_settings(AUTHENTICATION_BACKENDS=['desktop.auth.backend.AllowFirstUserDjangoBackend'])
+  def test_active_users(self):
     with patch('useradmin.metrics.get_localhost_name') as get_hostname:
       get_hostname.return_value = 'host1'
       assert_equal(3, active_users())
@@ -340,6 +356,19 @@ class TestUserAdminMetrics(BaseUserAdminTests):
       assert_equal(2, metric['users.active.instance']['value'])
 
 
+  @override_settings(AUTHENTICATION_BACKENDS=['desktop.auth.backend.AllowFirstUserDjangoBackend'])
+  def test_active_users_prometheus(self):
+    if not ENABLE_PROMETHEUS.get():
+      raise SkipTest
+
+    with patch('useradmin.metrics.get_localhost_name') as get_hostname:
+      get_hostname.return_value = 'host1'
+      c = Client()
+      response = c.get('/metrics')
+      assert_true(b'hue_active_users 3.0' in response.content, response.content)
+      assert_true(b'hue_local_active_users 2.0' in response.content, response.content)
+
+
 class TestUserAdmin(BaseUserAdminTests):
 
   def test_group_permissions(self):