Conversation
- Add configurable default_label_selector parameter to QueryTemplateTools for better label filtering instead of scanning all streams - Create _build_selector(), _build_event_filter(), and _build_pattern_filter() helper methods for optimized query construction - Reduce default time range from 4 hours to 1 hour for faster queries - Put event ID filters first (most selective) before tool/pattern filters - Remove leading .* from hostname regex patterns per Grafana best practices - Update all query methods to use new optimized helpers - Document configuration options in blue_factory.py Addresses CAP-834
CAP-834 Optimize LogQL Query Templates to Prevent Blue Agent Timeouts
Description: Objective: Refactor LogQL query templates to eliminate anti-patterns and optimize performance, ensuring blue agent queries execute successfully within acceptable timeframes. Scope of Work:
Dependencies:
Acceptance Criteria:
Additional Notes:
|
- Add _optimize_logql_query() function to warn about broad selectors - Call optimizer in rate_limited_wrapper before executing queries - Add LogQL Performance Optimization section to agent system prompt - Update example queries to use specific label selectors - Guide agent to use list_loki_label_values before constructing queries This complements the QueryTemplateTools fix by also addressing queries constructed directly by the LLM agent when using MCP tools.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Key Changes:
Added:
QueryTemplateTools:_build_selector: Composes efficient label selectors, prioritizing narrowhost/job filters as recommended by Grafana Loki best practices
_build_event_filter: Selectively filters by event IDs using contains orsimple regex for performance
_build_pattern_filter: Generates case-insensitive regex filters forattacker/tool patterns
Changed:
{job=~".+"}with an option to overrideusing
default_label_selectorfor better targetingdefault_hours_backforspeedier queries (configurable per query)
regex
|=) before regex (|~) wherever possible for fastermatching
hours_backas an optional parameter, defaulting to the optimizedvalue
detect_port_scanning,detect_brute_force),reorganized the logical construction of queries for clarity and efficiency,
and updated docstrings to reflect the new defaults and optimizations
_build_selectorfor labeltargeting instead of regex line filters, improving query performance and
readability
rationale throughout the query template code
Removed:
hostname=~".*host.*") infavor of direct label selectors
by a 1-hour default to minimize load and improve responsiveness
Addresses CAP-834