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:
[]
= { = "../firewood-macros" }
= "0.24"
= "0.1"
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
Example Output
For #[metrics("firewood.query", "data retrieval")]:
firewood.example{success="true"}- Count of successful queriesfirewood.example{success="false"}- Count of failed queriesfirewood.example_ms{success="true"}- Timing of successful queriesfirewood.example_ms{success="false"}- Timing of failed queries
Requirements
- Functions must return a
Result<T, E>type - The
metricsandcoarsetimecrates 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 the coarsetime crate, which is known to be extremely fast
- 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.