Skip to main content

A metrics utility for Ansible

Project description

metrics-utility

metrics-utility deals with collecting, analyzing and reporting metrics from Ansible Automation Platform (AAP) Controller instances.

It provides two interfaces - a CLI and a python library.

Also see below for dev setup, and other docs.

CLI

A metrics-utility CLI tool for collecting and reporting metrics from Controller, allowing users to:

  • Collect Controller usage data from the database, settings, and prometheus
  • Analyze the data and generate .xlsx reports
  • Support multiple storage adapters for data persistence (local directory, S3)
  • Push metrics data to console.redhat.com

It can run either standalone (against a specified postgres instance), or inside the Controller's python virtual environment. The controller mode allows the config collector to collect more settings and takes DB connection details from there.

It provides two subcommands:

  • gather_automation_controller_billing_data
    • collects data from controller, saves daily tarballs with .csv / .json inside
    • saves tarballs in specified storage
    • optionally sends to console
  • build_report
    • builds a .xlsx report
      • 3 report types - CCSP, CCSPv2, RENEWAL_GUIDANCE
      • the ccsp* reports use the collected tarballs as the source
      • the renewal* report reads from controller db

Example invocation:

pip install metrics-utility

# common
export METRICS_UTILITY_SHIP_PATH="./out"
export METRICS_UTILITY_SHIP_TARGET="directory"

# gather data
metrics-utility gather_automation_controller_billing_data --ship --until=10m
ls out/data/`date +%Y/%m/%d`/ # data/<year>/<month>/<day>/<uuid>-<since>-<until>-<index>-<collection>.tar.gz

# build report
export METRICS_UTILITY_REPORT_TYPE="CCSPv2"

metrics-utility build_report --month=`date +%Y-%m` # year-month
ls out/reports/`date +%Y/%m`/ # reports/<year>/<month>/<type>-<year>-<month>.xlsx

See docs/cli.md and docs/old-readme.md for details on the usage,
See docs/environment.md for a full list of environment variables,
See docs/awx.md for more on running against an awx dev env.

Python library

The metrics_utility.library library provides a lower-level python API exposing the same functionality using these abstractions:

  • collectors - functions that collect specific data, from database to a .csv, or from elsewhere into a python dict
  • packagers - packages multiple related .csv & .json into .tar.gz daily tarballs
  • extractors - extracts these tarballs, loading specific data into dicts or Pandas dataframe
  • rollups - group and aggregate dataframes, compute stats and optionally save them
  • reports - builds a xlsx report from a set of dataframes
  • storage - unified storage backend for filesystem, s3, segment, crc and db
  • instants - associated datetime-related helpers
  • tempdir & db locking helpers

The library uses no env variables, and doesn't rely on Controller environment. The CLI is expected to use the library where possible, but is not limited to it.

Example use:

from metrics_utility.library.collectors.controller import config, main_jobevent
from metrics_utility.library.instants import last_day, this_day
from metrics_utility.library import lock, storage

db = ... # django.db.connection / psycopg 3

dir_storage = storage.StorageDirectory(base_path='./out')

with lock(db=db, key='my-unique-key'):
    # dict, will be converted to json
    config_dict = config(db=db).gather()

    # list of .csv filenames; since is included, until is excluded
    job_csvs = main_jobevent(db=db, since=last_day(), until=this_day()).gather()

# save in storage
dir_storage.put('config.json', dict=config_dict)
for index, file in enumerate(job_csvs):
    dir_storage.put(f'main_jobevent.{index}.csv', filename=file)
    os.remove(file)

See library README for details.
See workers/ for more library usage examples.

Developer setup

Prerequisites

  • Python 3.12 or later
  • uv
  • Docker compose
  • make, git

Dependencies are managed via pyproject.toml (& uv.lock). There is also setup.cfg with dependencies but those are only used for the controller mode.

The Docker compose environment is used to provide a quick postgres & minio instances on ports 5432 and 9000/9001, but they can be replaced with local setup. See docker-compose.yaml for details of the mc setup (substitute the minio hostname for localhost), and tools/docker/*.sql for users & data to import in postgres (start with roles.sql and latest.sql). (Or don't, and use docker.)

uv is also not required as long as you can manage your own python venv and install dependencies from pyproject.toml.

Optionally, uvx pre-commit install to run ruff checks from a pre-commit hook, defined in .pre-commit-config.yaml. Or you can run make lint / make fix manually.

Installation

# Clone the repository
git clone https://github.com/ansible/metrics-utility.git
cd metrics-utility

# Install dependencies using uv
uv sync

Run

cd metrics-utility
make compose
cd metrics-utility
uv run ./manage.py --help
uv run ./manage.py gather_automation_controller_billing_data --help
uv run ./manage.py build_report --help

make clean resets the docker environment, make lint & make fix run the linters & formatters, make psql runs psql in the postgres container.

Tests

Some tests depend on a running postgres & minio instance - run make compose to get one.

make test runs the full test suite, make coverage produces a coverage report.

Use uv run pytest -s -v for running tests with verbose output, also accepts test filenames.

See docs/tests-compose.md to run the tests inside the docker compose environment.

Documentation

More documentation is available in docs/, and elsewhere:

Please follow our Contributor's Guide for details on submitting changes and documentation standards.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metrics_utility-0.7.20260118.tar.gz (875.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metrics_utility-0.7.20260118-py3-none-any.whl (369.9 kB view details)

Uploaded Python 3

File details

Details for the file metrics_utility-0.7.20260118.tar.gz.

File metadata

  • Download URL: metrics_utility-0.7.20260118.tar.gz
  • Upload date:
  • Size: 875.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for metrics_utility-0.7.20260118.tar.gz
Algorithm Hash digest
SHA256 11800d00ce4952408cd3b49243049bec0a68cee48f0ace300c30413f5918d485
MD5 2cc0697d939e976399242eeef0d73021
BLAKE2b-256 481a3b783113deff44319c9a988f203f5549f57b0a85cfc584df0c2639723752

See more details on using hashes here.

File details

Details for the file metrics_utility-0.7.20260118-py3-none-any.whl.

File metadata

  • Download URL: metrics_utility-0.7.20260118-py3-none-any.whl
  • Upload date:
  • Size: 369.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for metrics_utility-0.7.20260118-py3-none-any.whl
Algorithm Hash digest
SHA256 12ede8ce6d9d9d889b74925f0b4deb445d6b9e29c172170194631946b9119c89
MD5 3f52a43e1d5d90c6b48f5176607f3b55
BLAKE2b-256 1d876527e88b7b48331d9ae4374e4093f86b31c88a1671b06a4261dc737982fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page