mirror of
https://github.com/ansible/awx.git
synced 2026-05-06 17:07:36 -02:30
add database connection to the metrics endpoint (#12427)
* add database connection to the metrics endpoint * bump the counts collector version to 1.2 * check for postgresql as database so to not break the tests
This commit is contained in:
@@ -129,7 +129,7 @@ def config(since, **kwargs):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@register('counts', '1.1', description=_('Counts of objects such as organizations, inventories, and projects'))
|
@register('counts', '1.2', description=_('Counts of objects such as organizations, inventories, and projects'))
|
||||||
def counts(since, **kwargs):
|
def counts(since, **kwargs):
|
||||||
counts = {}
|
counts = {}
|
||||||
for cls in (
|
for cls in (
|
||||||
@@ -172,6 +172,13 @@ def counts(since, **kwargs):
|
|||||||
.count()
|
.count()
|
||||||
)
|
)
|
||||||
counts['pending_jobs'] = models.UnifiedJob.objects.exclude(launch_type='sync').filter(status__in=('pending',)).count()
|
counts['pending_jobs'] = models.UnifiedJob.objects.exclude(launch_type='sync').filter(status__in=('pending',)).count()
|
||||||
|
if connection.vendor == 'postgresql':
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(f"select count(*) from pg_stat_activity where datname=\'{connection.settings_dict['NAME']}\'")
|
||||||
|
counts['database_connections'] = cursor.fetchone()[0]
|
||||||
|
else:
|
||||||
|
# We should be using postgresql, but if we do that change that ever we should change the below value
|
||||||
|
counts['database_connections'] = 1
|
||||||
return counts
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -126,6 +126,8 @@ def metrics():
|
|||||||
LICENSE_INSTANCE_TOTAL = Gauge('awx_license_instance_total', 'Total number of managed hosts provided by your license', registry=REGISTRY)
|
LICENSE_INSTANCE_TOTAL = Gauge('awx_license_instance_total', 'Total number of managed hosts provided by your license', registry=REGISTRY)
|
||||||
LICENSE_INSTANCE_FREE = Gauge('awx_license_instance_free', 'Number of remaining managed hosts provided by your license', registry=REGISTRY)
|
LICENSE_INSTANCE_FREE = Gauge('awx_license_instance_free', 'Number of remaining managed hosts provided by your license', registry=REGISTRY)
|
||||||
|
|
||||||
|
DATABASE_CONNECTIONS = Gauge('awx_database_connections_total', 'Number of connections to database', registry=REGISTRY)
|
||||||
|
|
||||||
license_info = get_license()
|
license_info = get_license()
|
||||||
SYSTEM_INFO.info(
|
SYSTEM_INFO.info(
|
||||||
{
|
{
|
||||||
@@ -163,6 +165,8 @@ def metrics():
|
|||||||
USER_SESSIONS.labels(type='user').set(current_counts['active_user_sessions'])
|
USER_SESSIONS.labels(type='user').set(current_counts['active_user_sessions'])
|
||||||
USER_SESSIONS.labels(type='anonymous').set(current_counts['active_anonymous_sessions'])
|
USER_SESSIONS.labels(type='anonymous').set(current_counts['active_anonymous_sessions'])
|
||||||
|
|
||||||
|
DATABASE_CONNECTIONS.set(current_counts['database_connections'])
|
||||||
|
|
||||||
all_job_data = job_counts(None)
|
all_job_data = job_counts(None)
|
||||||
statuses = all_job_data.get('status', {})
|
statuses = all_job_data.get('status', {})
|
||||||
for status, value in statuses.items():
|
for status, value in statuses.items():
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ def test_empty():
|
|||||||
"workflow_job_template": 0,
|
"workflow_job_template": 0,
|
||||||
"unified_job": 0,
|
"unified_job": 0,
|
||||||
"pending_jobs": 0,
|
"pending_jobs": 0,
|
||||||
|
"database_connections": 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ EXPECTED_VALUES = {
|
|||||||
'awx_license_instance_total': 0,
|
'awx_license_instance_total': 0,
|
||||||
'awx_license_instance_free': 0,
|
'awx_license_instance_free': 0,
|
||||||
'awx_pending_jobs_total': 0,
|
'awx_pending_jobs_total': 0,
|
||||||
|
'awx_database_connections_total': 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user