Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add database connection to the metrics endpoint #12427

Merged
merged 6 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion awx/main/analytics/collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
counts = {}
for cls in (
Expand Down Expand Up @@ -172,6 +172,13 @@ def counts(since, **kwargs):
.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


Expand Down
4 changes: 4 additions & 0 deletions awx/main/analytics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_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()
SYSTEM_INFO.info(
{
Expand Down Expand Up @@ -163,6 +165,8 @@ def metrics():
USER_SESSIONS.labels(type='user').set(current_counts['active_user_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)
statuses = all_job_data.get('status', {})
for status, value in statuses.items():
Expand Down
1 change: 1 addition & 0 deletions awx/main/tests/functional/analytics/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_empty():
"workflow_job_template": 0,
"unified_job": 0,
"pending_jobs": 0,
"database_connections": 1,
}


Expand Down
1 change: 1 addition & 0 deletions awx/main/tests/functional/analytics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'awx_license_instance_total': 0,
'awx_license_instance_free': 0,
'awx_pending_jobs_total': 0,
'awx_database_connections_total': 1,
}


Expand Down