Firewood Macros
A Rust procedural macro crate providing zero-allocation metrics instrumentation for the Firewood database.
Overview
This crate provides the #[metrics] attribute macro that automatically instruments functions with performance metrics collection. The macro is designed for high-performance applications where allocation overhead during metrics collection is unacceptable.
Features
- Zero Runtime Allocations: Uses compile-time string concatenation and static label arrays
- Automatic Timing: Measures function execution time with microsecond precision
- Success/Failure Tracking: Automatically labels metrics based on
Resultreturn values - Metric Descriptions: Optional human-readable descriptions for better observability
- Compile-time Validation: Ensures functions return
Result<T, E>types
Usage
Add the dependency to your Cargo.toml:
[]
= true
= "0.24"
Basic Usage
use metrics;
With Description
Generated Metrics
For each instrumented function, the macro generates two metrics:
- Count Metric (base name): Tracks the number of function calls
- Timing Metric (base name + "_ms"): Tracks execution time in milliseconds
Both metrics include a success label:
success="true"forOk(_)resultssuccess="false"forErr(_)results
Note: Metrics are registered without a namespace prefix in code. When exported (via FFI or benchmark tools), the firewood. prefix is automatically added by the exporter layer.
Example Output
For #[metrics("query", "data retrieval")]:
query{success="true"}- Count of successful queries (exported asfirewood.query)query{success="false"}- Count of failed queries (exported asfirewood.query)query_ms{success="true"}- Timing of successful queries (exported asfirewood.query_ms)query_ms{success="false"}- Timing of failed queries (exported asfirewood.query_ms)
Requirements
- Functions must return a
Result<T, E>type - The
metricscrate must be available in scope - Rust 1.70+ (for
is_some_andmethod)
Performance Characteristics
Zero Allocations
The macro generates code that avoids all runtime allocations:
// Static label arrays (no allocation)
static __METRICS_LABELS_SUCCESS: & = &;
static __METRICS_LABELS_ERROR: & = &;
// Compile-time string concatenation (no allocation)
counter!
Minimal Overhead
- Single timestamp capture at function start using
std::time::Instant, which uses vDSO on modern Linux (no syscall overhead) - Branch-free label selection based on
Result::is_err() - Direct counter increments without intermediate allocations
Implementation Details
Code Generation
The macro transforms this:
Into approximately this:
Testing
The crate includes comprehensive tests:
License
This crate is part of the Firewood project and follows the same licensing terms. See LICENSE.md at the top level for details.