Skip to main content

A module to extract metrics from Terrafor, scripts

Project description

The static source code measurement tool for Terraform

TerrafomMetrics is a Python-based static source code measurement tool to characterize Infrastructure-as-Code. It helps quantify the characteristics of infrastructure code to support DevOps engineers when maintaining and evolving it. It currently supports 25 source code metrics, though other metrics can be derived by combining the implemented ones.

How to install

Installation is made simple by the PyPI repository. Download the tool and install it with:

pip install terraformmetrics

or, alternatively from the source code project directory:

pip install -r requirements.txt
pip install .

How to use

Command-line

Run terraform-metrics --help for instructions about the usage:

usage: terraform-metrics [-h] [--omit-zero-metrics] [-d DEST] [-o] [-v] src

Extract metrics from Terraform scripts.

positional arguments:
  src                   source file (tf file) or
                        directory

optional arguments:
  -h, --help            show this help message and exit
  --omit-zero-metrics   omit metrics with value equal 0
  -d DEST, --dest DEST  destination path to save results
  -o, --output          shows output
  -v, --version         show program's version number and exit

This is an example where metrics are calculated on the main.tf file assuming that it is run from the folder where the file is located. The result will be saved in the report.json file

terraform-metrics --omit-zero-metrics main.tf --dest report.json

Python

TerraformMetrics currently supports up to 25 source code metrics, implemented in Python. To extract the value for a given metric follow this pattern:

from terraformmetrics.<general|terraformspec>.metric import Metric

script = 'a valid yaml script'
value = Metric(script).count()

where metric and Metric have to be replaced with the name of the desired metric module to compute the value of a specific metric.
The difference between the general and the terraformspec modules lies in the fact that the terraformspec module contains metrics specific to terraform, while the general module contains metrics that can be generalized to other languages (for example, the lines of code).

For example, to count the number of lines of code:

from terraformmetrics.terraformspec.num_resources import NumResources

script = """ 
     resource "opc_storage_container" "accs-apps" {
       name = "my-accs-apps"
     }

     resource "opc_storage_object" "example-java-app" {
       name         = "employees-web-app.zip"
       container    = "${opc_storage_container.accs-apps.name}"
       file         = "./employees-web-app.zip"
       etag         = "${md5(file("./employees-web-app.zip"))}"
       content_type = "application/zip;charset=UTF-8"
     }
     """

value = NumResources(script).count()
print('Number of resources: ', value)

To extract the value for the 25 metrics at once, import the terraformmetrics.metrics_extractor package and call the method extract_all() (in this case the return value will be a json object):

from terraformmetrics.metrics_extractor import extract_all

script = """ 
     resource "opc_storage_container" "accs-apps" {
       name = "my-accs-apps"
     }

     resource "opc_storage_object" "example-java-app" {
       name         = "employees-web-app.zip"
       container    = "${opc_storage_container.accs-apps.name}"
       file         = "./employees-web-app.zip"
       etag         = "${md5(file("./employees-web-app.zip"))}"
       content_type = "application/zip;charset=UTF-8"
     }
     """

metrics = extract_all(script)
print('Lines of executable code:', metrics['lines_code'])

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

terraformmetrics-1.0.6.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

terraformmetrics-1.0.6-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file terraformmetrics-1.0.6.tar.gz.

File metadata

  • Download URL: terraformmetrics-1.0.6.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for terraformmetrics-1.0.6.tar.gz
Algorithm Hash digest
SHA256 535fa68a266324c928fed7368c40660ae105ae16d6b66b39e8a9ff0ffb03e40b
MD5 e4eb0b0f6e59d13a8fcdee424d237b24
BLAKE2b-256 74265034f4364c2c2c3b7ab0d12bc0a17cca4d858393c68eafd7a6a874d76cf4

See more details on using hashes here.

File details

Details for the file terraformmetrics-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for terraformmetrics-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 88372b2ea234a0a115230f45d42ea22531168a4e778a735101cb6d721f12fe75
MD5 caff9a3d98fe7936c31ed250698f2682
BLAKE2b-256 9588e8e4405f616138dd7e24a67a0e7b1e5a0e9d9615f49796f9c03ea93e5617

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