From bb5c7a98f33d64e73db1b151ca5f49672a679b81 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Mon, 8 Apr 2019 09:10:36 -0400 Subject: [PATCH] test prometheus metrics output --- awx/main/analytics/metrics.py | 54 ++++++++---------- .../functional/analytics/test_metrics.py | 55 +++++++++++++++++++ 2 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 awx/main/tests/functional/analytics/test_metrics.py diff --git a/awx/main/analytics/metrics.py b/awx/main/analytics/metrics.py index aa4cad177a..ee97efc695 100644 --- a/awx/main/analytics/metrics.py +++ b/awx/main/analytics/metrics.py @@ -1,6 +1,5 @@ -import os from datetime import datetime - +from django.conf import settings from prometheus_client import ( REGISTRY, PROCESS_COLLECTOR, @@ -11,31 +10,19 @@ from prometheus_client import ( generate_latest ) -from django.contrib.sessions.models import Session - -# Temporary Imports -from django.db import connection -from django.db.models import Count -from django.conf import settings - from awx.conf.license import get_license -from awx.main.utils import (get_awx_version, get_ansible_version, - get_custom_venv_choices) -from awx.main import models +from awx.main.utils import (get_awx_version, get_ansible_version) from awx.main.analytics.collectors import ( counts, instance_info, - job_instance_counts - ) -from django.contrib.sessions.models import Session -from awx.main.analytics import register + job_instance_counts, +) REGISTRY.unregister(PROCESS_COLLECTOR) REGISTRY.unregister(PLATFORM_COLLECTOR) REGISTRY.unregister(GC_COLLECTOR) - SYSTEM_INFO = Info('awx_system', 'AWX System Information') ORG_COUNT = Gauge('awx_organizations_total', 'Number of organizations') USER_COUNT = Gauge('awx_users_total', 'Number of users') @@ -61,16 +48,18 @@ INSTANCE_STATUS = Gauge('awx_instance_status_total', 'Status of Job launched', [ def metrics(): license_info = get_license(show_key=False) - SYSTEM_INFO.info({'system_uuid': settings.SYSTEM_UUID, - 'tower_url_base': settings.TOWER_URL_BASE, - 'tower_version': get_awx_version(), - 'ansible_version': get_ansible_version(), - 'license_type': license_info.get('license_type', 'UNLICENSED'), - 'free_instances': str(license_info.get('free instances', 0)), - 'license_expiry': str(license_info.get('time_remaining', 0)), - 'pendo_tracking': settings.PENDO_TRACKING_STATE, - 'external_logger_enabled': str(settings.LOG_AGGREGATOR_ENABLED), - 'external_logger_type': getattr(settings, 'LOG_AGGREGATOR_TYPE', 'None')}) + SYSTEM_INFO.info({ + 'system_uuid': settings.SYSTEM_UUID, + 'tower_url_base': settings.TOWER_URL_BASE, + 'tower_version': get_awx_version(), + 'ansible_version': get_ansible_version(), + 'license_type': license_info.get('license_type', 'UNLICENSED'), + 'free_instances': str(license_info.get('free instances', 0)), + 'license_expiry': str(license_info.get('time_remaining', 0)), + 'pendo_tracking': settings.PENDO_TRACKING_STATE, + 'external_logger_enabled': str(settings.LOG_AGGREGATOR_ENABLED), + 'external_logger_type': getattr(settings, 'LOG_AGGREGATOR_TYPE', 'None') + }) current_counts = counts(datetime.now()) @@ -101,11 +90,12 @@ def metrics(): INSTANCE_CAPACITY.labels(type=uuid).set(instance_data[uuid]['capacity']) INSTANCE_CPU.labels(type=uuid).set(instance_data[uuid]['cpu']) INSTANCE_MEMORY.labels(type=uuid).set(instance_data[uuid]['memory']) - INSTANCE_INFO.labels(type=uuid).info({'enabled': str(instance_data[uuid]['enabled']), - 'last_isolated_check': getattr(instance_data[uuid], 'last_isolated_check', 'None'), - 'managed_by_policy': str(instance_data[uuid]['managed_by_policy']), - 'version': instance_data[uuid]['version'] - }) + INSTANCE_INFO.labels(type=uuid).info({ + 'enabled': str(instance_data[uuid]['enabled']), + 'last_isolated_check': getattr(instance_data[uuid], 'last_isolated_check', 'None'), + 'managed_by_policy': str(instance_data[uuid]['managed_by_policy']), + 'version': instance_data[uuid]['version'] + }) instance_data = job_instance_counts(datetime.now()) for node in instance_data: diff --git a/awx/main/tests/functional/analytics/test_metrics.py b/awx/main/tests/functional/analytics/test_metrics.py new file mode 100644 index 0000000000..314a300969 --- /dev/null +++ b/awx/main/tests/functional/analytics/test_metrics.py @@ -0,0 +1,55 @@ +import pytest + +from prometheus_client.parser import text_string_to_metric_families +from awx.main import models +from awx.main.analytics.metrics import metrics + +EXPECTED_VALUES = { + 'awx_system_info':1.0, + 'awx_organizations_total':1.0, + 'awx_users_total':1.0, + 'awx_teams_total':1.0, + 'awx_inventories_total':1.0, + 'awx_projects_total':1.0, + 'awx_job_templates_total':1.0, + 'awx_workflow_job_templates_total':1.0, + 'awx_hosts_total':1.0, + 'awx_hosts_total':1.0, + 'awx_schedules_total':1.0, + 'awx_inventory_scripts_total':1.0, + 'awx_sessions_total':0.0, + 'awx_sessions_total':0.0, + 'awx_sessions_total':0.0, + 'awx_custom_virtualenvs_total':0.0, + 'awx_running_jobs_total':0.0, + 'awx_instance_capacity':100.0, + 'awx_instance_cpu':0.0, + 'awx_instance_memory':0.0, + 'awx_instance_info':1.0, +} +@pytest.mark.django_db +def test_metrics_counts(organization_factory, job_template_factory, + workflow_job_template_factory): + + + objs = organization_factory('org', superusers=['admin']) + jt = job_template_factory('test', organization=objs.organization, + inventory='test_inv', project='test_project', + credential='test_cred') + workflow_job_template_factory('test') + models.Team(organization=objs.organization).save() + models.Host(inventory=jt.inventory).save() + models.Schedule( + rrule='DTSTART;TZID=America/New_York:20300504T150000', + unified_job_template=jt.job_template + ).save() + models.CustomInventoryScript(organization=objs.organization).save() + + output = metrics() + gauges = text_string_to_metric_families(output.decode('UTF-8')) + + for gauge in gauges: + for sample in gauge.samples: + # name, label, value, timestamp, exemplar + name, _, value, _, _ = sample + assert EXPECTED_VALUES[name] == value \ No newline at end of file