Skip to content

feat: Add conditional and router sinks for event routing#60

Merged
willibrandon merged 6 commits intomainfrom
feature/conditional-router-sinks
Aug 22, 2025
Merged

feat: Add conditional and router sinks for event routing#60
willibrandon merged 6 commits intomainfrom
feature/conditional-router-sinks

Conversation

@willibrandon
Copy link
Owner

@willibrandon willibrandon commented Aug 22, 2025

Description

Implements advanced event routing infrastructure with conditional and router sinks, with production-ready features including route priorities, groups, health checks, circuit breakers, and Prometheus/Grafana integration for monitoring.

Key highlights:

  • Zero-overhead conditional routing - 3.7ns when predicate returns false
  • Flexible routing modes - FirstMatch (exclusive) and AllMatch (broadcast)
  • Production resilience - Circuit breakers, health checks, fallback sinks
  • Enterprise monitoring - Prometheus metrics, Grafana dashboards
  • Runtime management - Add/remove routes and groups dynamically

Type of change

  • Bug fix
  • New feature
  • Performance improvement
  • Documentation update

Checklist

  • Tests pass (go test ./...)
  • Linter passes (golangci-lint run)
  • Benchmarks checked (if performance-related)
  • Documentation updated (if needed)
  • Zero-allocation promise maintained (if applicable)

Additional notes

  • Adds 11 documented routing patterns covering real-world scenarios
  • Maintains backward compatibility - no breaking changes
  • Test coverage for all new features
  • Performance validated: conditional sink adds only 3.7ns overhead when inactive
  • JSON configuration support via existing configuration builder

Fixes #43

…lities

- Add ConditionalSink for zero-overhead predicate-based routing (3.7ns when false)
- Add RouterSink with FirstMatch/AllMatch modes and runtime route management
- Implement route priority system and route groups for organization
- Add PredicateBuilder for fluent predicate composition
- Include route statistics tracking and TestEvent() for debugging
- Add health checks (HealthCheckable interface) for all sinks
- Implement CircuitBreakerSink with automatic failure recovery
- Add Prometheus metrics exporter and Grafana dashboard template
- Support JSON configuration for routing in appsettings.json
- Add selflog support for diagnostics
- Document 11 production routing patterns with examples

The implementation provides enterprise-grade event routing with zero-overhead design, production monitoring, and resilience patterns. Routes can be managed at runtime, grouped for organization, and protected with circuit breakers.
@willibrandon willibrandon self-assigned this Aug 22, 2025
@willibrandon willibrandon added documentation Improvements or additions to documentation enhancement New feature or request sinks Changes to log event sinks (output destinations) labels Aug 22, 2025
Remove sync.RWMutex from RouterStats struct to make it safe to copy. Split into routerStatsInternal (with mutex) for internal use and RouterStats (without mutex) for public API.
The router was sharing slice references between goroutines when reading routes in Emit()/TestEvent() while RemoveRoute() was modifying the underlying array. Fixed by making defensive copies of the routes slice before releasing the mutex.
@willibrandon willibrandon requested a review from Copilot August 22, 2025 06:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements comprehensive event routing infrastructure for mtlog, adding conditional and router sinks that enable sophisticated log event routing similar to Serilog's capabilities. The implementation brings production-ready routing with zero-overhead conditional filtering, flexible routing modes, health checks, circuit breakers, and monitoring integration.

Key Changes

  • Zero-overhead conditional routing - 3.7ns when predicate returns false with built-in predicates and fluent predicate builder
  • Flexible router sink - FirstMatch (exclusive) and AllMatch (broadcast) routing modes with priority-based route ordering
  • Production resilience features - Health checks, circuit breakers, fallback sinks, and comprehensive error handling

Reviewed Changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
sinks/conditional.go Core conditional sink implementation with panic recovery and built-in predicates
sinks/router.go Advanced router sink with dynamic route management and statistics tracking
sinks/predicate_builder.go Fluent API for building complex routing predicates with logical operators
sinks/health.go Health check infrastructure for monitoring sink connectivity and status
sinks/circuit_breaker.go Circuit breaker implementation for resilient logging with fallback mechanisms
sinks/router_metrics.go Prometheus metrics exporter for router statistics and monitoring integration
options.go Integration with mtlog's option system for easy configuration
configuration/builder.go JSON configuration support for conditional and router sinks
examples/ Comprehensive examples demonstrating routing patterns and usage scenarios
docs/ Complete documentation including routing patterns and best practices
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

- Replace inefficient recursive contains() with strings.Contains()
- Fix route naming to avoid rune conversion issues for indices > 25
- Use strings.HasPrefix() instead of manual string slicing
@willibrandon willibrandon merged commit ca38c4d into main Aug 22, 2025
26 checks passed
@willibrandon willibrandon changed the title feat: Add conditional and router sinks for advanced event routing feat: Add conditional and router sinks for event routing Aug 22, 2025
@willibrandon willibrandon deleted the feature/conditional-router-sinks branch August 22, 2025 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request sinks Changes to log event sinks (output destinations)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement conditional and router sinks for event routing

1 participant