No description
Find a file
2025-10-23 22:30:53 +00:00
.cursor Implement ETS-based metrics cache with TTL (Phase 7.6) 2025-10-17 15:50:06 +00:00
.forgejo test: Try debian-trixie runner 2025-10-22 20:29:36 +00:00
.specstory Fix cache timing tests for deterministic CI behavior 2025-10-23 22:26:31 +00:00
config Fix code formatting (mix format) 2025-10-23 21:40:43 +00:00
docs docs: Complete cross_reference.md with comprehensive symbol index 2025-10-19 19:11:06 +00:00
lib Fix code formatting (mix format) 2025-10-23 21:40:43 +00:00
test Fix cache timing tests for deterministic CI behavior 2025-10-23 22:26:31 +00:00
testsuite test: Strengthen test suite per OpenTopics analysis 2025-10-19 01:03:10 +00:00
.CI-Token.example Add CI tools for accessing Forgejo Actions logs 2025-10-20 12:58:48 +00:00
.containerignore Add container build with dedicated sysfs mount 2025-10-17 14:05:13 +00:00
.credentials.example Make examples and help text generic 2025-10-20 21:53:01 +00:00
.cursorignore Add CI tools for accessing Forgejo Actions logs 2025-10-20 12:58:48 +00:00
.formatter.exs Initial project foundation and architecture design 2025-10-07 11:51:52 +00:00
.gitignore Add CI tools for accessing Forgejo Actions logs 2025-10-20 12:58:48 +00:00
Architecture.md feat: Implement stdout/stderr output stream routing 2025-10-17 14:42:39 +00:00
assignments.md Fix integration tests: handle application start failures gracefully 2025-10-23 21:19:33 +00:00
bootstrap.md Initial project foundation and architecture design 2025-10-07 11:51:52 +00:00
btrfs_elixporter Cache observability: 5 new exporter metrics + default TTL=1s 2025-10-17 19:48:41 +00:00
CI-EXPLORATION-README.md ci: manual checkout; clean workflow; docs updated 2025-10-17 20:49:05 +00:00
CI-FINAL-SUMMARY.md Add final summary documentation 2025-10-20 21:41:44 +00:00
CI-Specs.md ci: manual checkout; clean workflow; docs updated 2025-10-17 20:49:05 +00:00
ci-tool.sh Make log URLs configurable via CI_WEB_BASE 2025-10-20 22:25:41 +00:00
CI-TOOLS-README.md Update documentation with generic examples 2025-10-20 21:57:23 +00:00
CI-TOOLS-SUMMARY.md Add CI tools for accessing Forgejo Actions logs 2025-10-20 12:58:48 +00:00
CI-WORKFLOW-GUIDE.md Update documentation with generic examples 2025-10-20 21:57:23 +00:00
ci.sh Make log URLs configurable via CI_WEB_BASE 2025-10-20 22:25:41 +00:00
CONTAINER.md Minimize container to 139 MB with Debian 13 2025-10-17 14:22:37 +00:00
Containerfile Minimize container to 139 MB with Debian 13 2025-10-17 14:22:37 +00:00
current_plan.md Update plan: Note cache timing test intermittency 2025-10-23 21:49:52 +00:00
elixproject.md Initial project foundation and architecture design 2025-10-07 11:51:52 +00:00
get-session-cookie.sh Make examples and help text generic 2025-10-20 21:53:01 +00:00
implementation suggestions.md Capture testing session logs; plan: add function-call tracing\n\n- Add .cursor runtime logs and metrics output from manual verification\n- Update current_plan.md to prioritize observability tracing tasks\n- Record Q&A session in .specstory history for provenance\n\nContext: Verified daemon start, /metrics response, and port-conflict behavior. Next step: instrument critical functions with trace_call to surface entry/exit and durations in debug mode. 2025-10-16 21:45:59 +00:00
Learning_log.md Update Learning_log.md with TechSpec compliance work 2025-10-23 21:06:29 +00:00
mix.exs Final Assignment: Runtime abstraction for CLI error message testing 2025-10-23 20:27:17 +00:00
mix.lock chore: Update mix.lock and fix test file whitespace 2025-10-18 23:02:40 +00:00
OpenTopics.md docs: Update plan and log test suite improvements 2025-10-19 01:26:53 +00:00
README.md docs: Complete Phase 9 documentation and fix inline docs 2025-10-19 20:00:22 +00:00
SysfsMetricsMapping.md Multi-line sysfs parsing: error_stats, commit_stats, size_classes, Synology 2025-10-17 13:01:06 +00:00
TechSpec.md TechSpec compliance: Complete 6 of 9 assignments 2025-10-23 15:42:27 +00:00
wisdom.md Enhance wisdom.md with history insights 2025-10-10 08:05:56 +00:00

btrfs_elixporter

A Prometheus metrics exporter for btrfs filesystem statistics, written in Elixir.

Overview

btrfs_elixporter reads btrfs filesystem statistics from the Linux kernel's sysfs interface (/sys/fs/btrfs/) and exposes them in Prometheus format over HTTP. It supports both daemon mode for continuous monitoring and oneshot mode for debugging.

Tutorial Project

This project serves as an Elixir learning resource. The codebase is comprehensively documented to teach Elixir concepts, patterns, and best practices. If you're learning Elixir, this project demonstrates:

  • OTP applications and supervision trees
  • GenServer behavior
  • Pattern matching and guards
  • Error handling strategies
  • HTTP server implementation
  • File I/O and directory traversal
  • String parsing and data transformation
  • Testing with ExUnit
  • Documentation with ExDoc

See docs/code/README.md for detailed tutorial documentation.

Features

  • Auto-discovery: Automatically discovers and monitors all btrfs filesystems
  • Complete metrics: Exports all available sysfs statistics
  • Smart consolidation: Groups related metrics with labels (Prometheus best practices)
  • Daemon mode: Long-running HTTP server for continuous monitoring
  • Oneshot mode: Read-once output for debugging and testing
  • Configurable caching: Optional caching to handle high request rates
  • Error tracking: Failures tracked as metrics, not silent errors
  • IPv6 support: Full IPv4 and IPv6 support

Quick Start

Installation

# Clone the repository
git clone <repository-url>
cd btrfs_elixporter

# Get dependencies
mix deps.get

# Compile
mix compile

# Run tests
mix test

Running in Daemon Mode

# Default: localhost:9573, /metrics endpoint
mix run --no-halt

# Custom port
BTRFS_EXPORTER_PORT=9100 mix run --no-halt

# Custom bind address (all interfaces)
mix run --no-halt -- --bind 0.0.0.0

Access metrics at: http://localhost:9573/metrics

Running in Oneshot Mode

# Read from live sysfs and print to stdout
mix run -- --oneshot

# Read from test fixture
mix run -- --oneshot --path testsuite/test1/btrfs

Running with Docker/Podman

Build and run as a container:

# Build the container
docker build -t btrfs_elixporter:latest -f Containerfile .

# Run in daemon mode
docker run -d \
  --name btrfs_exporter \
  -p 9573:9573 \
  -v /sys/fs/btrfs:/mnt/btrfs-sysfs:ro \
  btrfs_elixporter:latest

See CONTAINER.md for complete container usage guide.

Configuration

Configuration supports three sources with precedence: CLI flags > Environment variables > Defaults

Command-Line Flags

--oneshot                  # Enable oneshot mode (read once, print, exit)
--path <path>              # Sysfs root path (default: /sys/fs/btrfs)
--port <port>              # HTTP server port (default: 9573)
--bind <address>           # Bind address (default: 127.0.0.1 and ::1)
--endpoint <path>          # Metrics endpoint (default: /metrics)
--cache-duration <seconds> # Cache TTL in seconds (default: 1)

Environment Variables

BTRFS_EXPORTER_PATH           # Sysfs path
BTRFS_EXPORTER_PORT           # HTTP port
BTRFS_EXPORTER_BIND           # Bind address
BTRFS_EXPORTER_ENDPOINT       # Metrics endpoint path
BTRFS_EXPORTER_CACHE_DURATION # Cache duration

Examples

# Daemon on custom port
mix run --no-halt -- --port 9100

# Oneshot with test data
mix run -- --oneshot --path testsuite/test2/btrfs

# Disable caching (always fresh data)
mix run --no-halt -- --cache-duration 0

# External access (security warning: no authentication!)
mix run --no-halt -- --bind 0.0.0.0 --port 9573

Metrics

All btrfs sysfs statistics are exported as Prometheus metrics. Examples:

# Allocation metrics (consolidated with labels)
btrfs_allocation_bytes{fsid="...", type="data", state="used"} 123456789
btrfs_allocation_bytes{fsid="...", type="metadata", state="reserved"} 0

# RAID profile metrics
btrfs_allocation_raid_bytes{fsid="...", type="data", profile="raid1", state="total"} 165295423488

# Device errors (consolidated)
btrfs_device_errors_total{fsid="...", device_id="1", error_type="write"} 0
btrfs_device_errors_total{fsid="...", device_id="1", error_type="read"} 0

# Commit statistics
btrfs_commit_stats_commits_total{fsid="..."} 12
btrfs_commit_stats_duration_milliseconds{fsid="...", statistic="last"} 12
btrfs_commit_stats_duration_milliseconds{fsid="...", statistic="max"} 305

# Cache metadata
btrfs_exporter_last_poll_timestamp_seconds{fsid="..."} 1728345678

# Error tracking
btrfs_error_lastTS{error="read_error", path="...", fsid="..."} 1728345678
btrfs_error_count{error="read_error", path="...", fsid="..."} 5

See SysfsMetricsMapping.md for complete transformation rules.

Documentation

Technical Specifications

Tutorial Documentation

Generated API Documentation

# Generate HTML documentation
mix docs

# Open in browser
open doc/index.html

Testing

The project includes comprehensive unit tests (121 tests, 0 failures):

# Run all tests
mix test
# Output: 121 tests, 0 failures (3.6s)

# Run with coverage
mix test --cover

# Run specific test file
mix test test/btrfs_elixporter/config_test.exs

# Run tests with trace output
mix test --trace

# Regenerate documentation
mix docs

Test coverage:

  • Config: 44 tests (CLI parsing, ENV vars, precedence)
  • SysfsReader: 28 tests (filesystem discovery, file I/O, symlink handling)
  • DataCollector: 19 tests (parsing, consolidation, Synology extensions)
  • MetricsFormatter: 16 tests (Prometheus format, escaping, sorting)
  • MetricsCache: 12 tests (ETS caching, TTL expiry)

Test data is provided in testsuite/ with various btrfs configurations (RAID levels, multiple filesystems, qgroups, vendor extensions).

Development

# Format code
mix format

# Check formatting
mix format --check-formatted

# Run type checking (if Dialyzer configured)
mix dialyzer

# Interactive shell with project loaded
iex -S mix

Architecture

High-level components:

  1. Application Supervisor - OTP application entry point
  2. HTTP Server - Serves /metrics endpoint
  3. Metrics Collector - Discovers filesystems and reads sysfs
  4. Sysfs Reader - File I/O abstraction (supports real and test paths)
  5. Metrics Formatter - Converts to Prometheus text format
  6. Cache Manager - Optional TTL-based caching
  7. Error Tracker - Accumulates error metrics
  8. Config Manager - Multi-source configuration

See docs/code/architecture.md for detailed design (to be written during implementation).

Contributing

  1. Read the tutorial documentation to understand the codebase
  2. Follow existing patterns and conventions
  3. Add inline documentation (@moduledoc, @doc)
  4. Update tutorial docs for significant changes
  5. Add tests for new functionality
  6. Ensure all tests pass: mix test
  7. Format code: mix format

License

[TODO: Add license]

Authors

[TODO: Add authors]

Acknowledgments

  • btrfs kernel developers for the sysfs interface
  • Prometheus project for exporter guidelines
  • Elixir community for excellent documentation standards