Executing a Historical Query
Usequery_history() to query historical data:
filter: &str- Query in the formatOP:SERIES:[TAGS]:[RANGE]
Ok(f64)- The aggregated value
0.0 for both valid zero results and some failures. There’s no way to distinguish between them.
Host function:
Region containing UTF-8 filter bytes. The host returns the aggregated value.
Query Syntax
Operators
AVG- Average of values in rangeMIN- Minimum value in rangeMAX- Maximum value in rangeSUM- Sum of values in rangeCOUNT- Number of events in range
Time Ranges
Historical queries typically specify a time range:sec, min, hour, day, week (singular and plural forms)
Absolute Time Ranges
You can also specify absolute timestamps in microseconds:[start_timestamp,end_timestamp] where both timestamps are Unix epoch microseconds.
Tag Filters
Use boolean operators to filter:Common Patterns
Baseline Comparison
Compare current values against historical baselines:Periodic Aggregation
Query historical data periodically:Multi-Series Aggregation
Query multiple series and correlate:Downsampling
Create lower-resolution aggregates:Combining with Live Queries
Use historical queries to initialize state, then maintain it with live queries:Performance Considerations
Query Cost
Historical queries scan stored data. Larger time ranges and broader tag filters increase query cost:Caching Results
If you query the same historical range repeatedly, cache the result:Query Frequency
Avoid executing historical queries in tight loops:Error Handling
Thequery_history() function returns 0.0 for both:
- Valid aggregate results that are actually zero
- Host-side failures
Next Steps
Live Queries
Subscribe to real-time stream updates
Writeback
Send results to external systems