test prometheus metrics output

This commit is contained in:
Wayne Witzel III
2019-04-08 09:10:36 -04:00
parent 3fb3079264
commit bb5c7a98f3
2 changed files with 77 additions and 32 deletions

View File

@@ -1,6 +1,5 @@
import os
from datetime import datetime from datetime import datetime
from django.conf import settings
from prometheus_client import ( from prometheus_client import (
REGISTRY, REGISTRY,
PROCESS_COLLECTOR, PROCESS_COLLECTOR,
@@ -11,31 +10,19 @@ from prometheus_client import (
generate_latest 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.conf.license import get_license
from awx.main.utils import (get_awx_version, get_ansible_version, from awx.main.utils import (get_awx_version, get_ansible_version)
get_custom_venv_choices)
from awx.main import models
from awx.main.analytics.collectors import ( from awx.main.analytics.collectors import (
counts, counts,
instance_info, instance_info,
job_instance_counts job_instance_counts,
) )
from django.contrib.sessions.models import Session
from awx.main.analytics import register
REGISTRY.unregister(PROCESS_COLLECTOR) REGISTRY.unregister(PROCESS_COLLECTOR)
REGISTRY.unregister(PLATFORM_COLLECTOR) REGISTRY.unregister(PLATFORM_COLLECTOR)
REGISTRY.unregister(GC_COLLECTOR) REGISTRY.unregister(GC_COLLECTOR)
SYSTEM_INFO = Info('awx_system', 'AWX System Information') SYSTEM_INFO = Info('awx_system', 'AWX System Information')
ORG_COUNT = Gauge('awx_organizations_total', 'Number of organizations') ORG_COUNT = Gauge('awx_organizations_total', 'Number of organizations')
USER_COUNT = Gauge('awx_users_total', 'Number of users') 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(): def metrics():
license_info = get_license(show_key=False) license_info = get_license(show_key=False)
SYSTEM_INFO.info({'system_uuid': settings.SYSTEM_UUID, SYSTEM_INFO.info({
'tower_url_base': settings.TOWER_URL_BASE, 'system_uuid': settings.SYSTEM_UUID,
'tower_version': get_awx_version(), 'tower_url_base': settings.TOWER_URL_BASE,
'ansible_version': get_ansible_version(), 'tower_version': get_awx_version(),
'license_type': license_info.get('license_type', 'UNLICENSED'), 'ansible_version': get_ansible_version(),
'free_instances': str(license_info.get('free instances', 0)), 'license_type': license_info.get('license_type', 'UNLICENSED'),
'license_expiry': str(license_info.get('time_remaining', 0)), 'free_instances': str(license_info.get('free instances', 0)),
'pendo_tracking': settings.PENDO_TRACKING_STATE, 'license_expiry': str(license_info.get('time_remaining', 0)),
'external_logger_enabled': str(settings.LOG_AGGREGATOR_ENABLED), 'pendo_tracking': settings.PENDO_TRACKING_STATE,
'external_logger_type': getattr(settings, 'LOG_AGGREGATOR_TYPE', 'None')}) 'external_logger_enabled': str(settings.LOG_AGGREGATOR_ENABLED),
'external_logger_type': getattr(settings, 'LOG_AGGREGATOR_TYPE', 'None')
})
current_counts = counts(datetime.now()) current_counts = counts(datetime.now())
@@ -101,11 +90,12 @@ def metrics():
INSTANCE_CAPACITY.labels(type=uuid).set(instance_data[uuid]['capacity']) INSTANCE_CAPACITY.labels(type=uuid).set(instance_data[uuid]['capacity'])
INSTANCE_CPU.labels(type=uuid).set(instance_data[uuid]['cpu']) INSTANCE_CPU.labels(type=uuid).set(instance_data[uuid]['cpu'])
INSTANCE_MEMORY.labels(type=uuid).set(instance_data[uuid]['memory']) INSTANCE_MEMORY.labels(type=uuid).set(instance_data[uuid]['memory'])
INSTANCE_INFO.labels(type=uuid).info({'enabled': str(instance_data[uuid]['enabled']), INSTANCE_INFO.labels(type=uuid).info({
'last_isolated_check': getattr(instance_data[uuid], 'last_isolated_check', 'None'), 'enabled': str(instance_data[uuid]['enabled']),
'managed_by_policy': str(instance_data[uuid]['managed_by_policy']), 'last_isolated_check': getattr(instance_data[uuid], 'last_isolated_check', 'None'),
'version': instance_data[uuid]['version'] 'managed_by_policy': str(instance_data[uuid]['managed_by_policy']),
}) 'version': instance_data[uuid]['version']
})
instance_data = job_instance_counts(datetime.now()) instance_data = job_instance_counts(datetime.now())
for node in instance_data: for node in instance_data:

View File

@@ -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