Configuration and Environment Variables

This section outlines how the balena-prometheus-exporter project leverages environment variables to manage its behavior.

BALENA_TOKEN

Purpose: The BALENA_TOKEN environment variable is crucial for authentication with the Balena Cloud API. It acts as the access key to retrieve fleet metrics.

Required: Yes

Default: None

Example:

docker run -d \
          --name balena-exporter \
          -p 8000:8000 \
          -e BALENA_TOKEN=YOUR_BALENA_TOKEN \
          balena-exporter
          

Source: README.md

Code Example:

# main.py
          BALENA_TOKEN = os.getenv("BALENA_TOKEN", False)
          

CRAWL_INTERVAL

Purpose: This variable determines the frequency, in seconds, at which the exporter fetches metrics from the Balena Cloud API.

Required: No

Default: 60 seconds

Example:

docker run -d \
          --name balena-exporter \
          -p 8000:8000 \
          -e BALENA_TOKEN=YOUR_BALENA_TOKEN \
          -e CRAWL_INTERVAL=30 \
          balena-exporter
          

Source: README.md

Code Example:

# main.py
          CRAWL_INTERVAL = os.getenv("CRAWL_INTERVAL", 60)
          

Code Utilization

Environment variables are accessed using the os.getenv() function.

Source: main.py

The BALENA_TOKEN is utilized within the get_fleet_metrics function to authorize API requests:

# main.py
          def get_fleet_metrics(self, fleet_id):
              headers = {
                  "Authorization": f"Bearer {BALENA_TOKEN}",
                  "Content-Type": "application/json",
              }
              # ...
          

The CRAWL_INTERVAL is used to define the sleep duration in the main loop:

# main.py
          while True:
              time.sleep(int(CRAWL_INTERVAL))
          

Top-Level Directory Explanations

tests/ - This directory contains all the unit and integration tests for the project. It includes the __init__.py file which makes it a package, and specific test files like test_exporter.py.