Skip to content

Conversation

torbensen
Copy link

Implements #2058 - Add optional filter field to endpoints that evaluates CEL expressions against message payload for content-based routing.

Motivation

Currently, webhook endpoints receive all messages for their configured event types without any content-based filtering. This can lead to unnecessary webhook calls when endpoints only need to process messages that meet specific criteria based on payload content. For example, a DeFi application might only want to receive webhook notifications for transactions above a certain amount, or an NFT marketplace might only want notifications for specific collections.

The lack of payload-based filtering means:

  • Increased webhook traffic and processing overhead
  • More complex filtering logic in webhook receivers
  • Potential for unnecessary API calls and resource usage
  • Less efficient message routing based on content

Solution

This PR adds CEL (Common Expression Language) filtering support to webhook endpoints, allowing content-based routing of messages before dispatch.

Key Changes:

  1. Database Schema: Added optional filter field to the endpoint table to store CEL expressions

  2. API Updates:

    • Added filter field to EndpointIn, EndpointUpdate, and EndpointPatch structs
    • Added validation for CEL expressions during endpoint creation/updates
    • Updated EndpointOut to include the filter field
  3. Core Filtering Logic:

    • Implemented CEL expression evaluation using the cel-parser crate
    • Added payload filtering in the message dispatch pipeline
    • Integrated filtering into both scheduled and immediate message processing
  4. Performance & Monitoring:

    • Added CelExecutionMetrics to track execution time and complexity
    • Implemented configurable thresholds for performance monitoring
    • Added comprehensive logging for debugging and optimization
  5. Validation & Safety:

    • CEL expression syntax validation
    • Length limits (max 2048 characters)
    • Complexity limits (max 50 operators)
    • Graceful error handling with detailed error messages

Usage Example:

// Create endpoint with CEL filter
let endpoint = EndpointIn {
    url: "https://example.com/webhook".to_string(),
    filter: Some("amount >= 1000.0 && token == 'USDC'".to_string()),
    // ... other fields
};

Test Coverage:

  • 15+ comprehensive test cases covering various filtering scenarios
  • Performance testing with large payloads and deep nested structures
  • Real-world Web3 scenarios (DeFi, NFT transactions)
  • Error handling and validation testing
  • Edge cases and boundary conditions

This solution enables more efficient webhook routing by filtering messages at the payload level, reducing unnecessary webhook calls and allowing for more sophisticated message routing based on content.

Implements svix#2058 - Add optional filter field to endpoints that evaluates
CEL expressions against message payload for content-based routing.
@torbensen torbensen requested a review from a team as a code owner September 15, 2025 13:02
@tasn
Copy link
Member

tasn commented Sep 15, 2025

Hey there, thank you for the contribution! Let me respond on the ticket you created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants