Skip to content

Add FirstMatch routing strategy#47

Merged
samber merged 14 commits intosamber:mainfrom
sashaaro:first-match
Jan 6, 2026
Merged

Add FirstMatch routing strategy#47
samber merged 14 commits intosamber:mainfrom
sashaaro:first-match

Conversation

@sashaaro
Copy link
Contributor

This PR introduces a new FirstMatch routing strategy that sends log records to the first matching handler only, complementing the existing Fanout behavior which broadcasts to all matching handlers.

Motivation
In some logging scenarios, you want to route logs to exactly one handler based on priority rules, rather than broadcasting to all matching handlers. For example:

  • Send database queries to a dedicated DB log channel
  • Send HTTP requests to a request monitoring channel
  • Send everything else to a general logs channel

Without FirstMatch, all handlers would receive logs if their predicates match, leading to duplicate processing.

@sashaaro sashaaro mentioned this pull request Dec 23, 2025
@codecov
Copy link

codecov bot commented Dec 24, 2025

Codecov Report

❌ Patch coverage is 73.27586% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 29.20%. Comparing base (cb917e6) to head (c270ea4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
router.go 65.00% 12 Missing and 2 partials ⚠️
firstmatch.go 76.31% 9 Missing ⚠️
router_predicate.go 78.94% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            main      #47       +/-   ##
==========================================
+ Coverage   8.65%   29.20%   +20.55%     
==========================================
  Files         15       16        +1     
  Lines        393      493      +100     
==========================================
+ Hits          34      144      +110     
+ Misses       357      340       -17     
- Partials       2        9        +7     
Flag Coverage Δ
unittests 29.20% <73.27%> (+20.55%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sashaaro sashaaro requested a review from samber December 25, 2025 04:43
@sashaaro sashaaro marked this pull request as draft December 25, 2025 06:28
@sashaaro
Copy link
Contributor Author

sashaaro commented Dec 25, 2025

I’ve looked deeper into this and tried to resolve some issues.
I’m concerned that this needs more tests, and I’m also worried about the repeated slices.Clone calls in WithAttrs and WithGroup for every inner handler - this may have some overhead.
I don't know, now I’m not sure this approach fully aligns with the intended slog design.
I hope probably I will come back to this after the holidays and try to finish or rethink parts of it.

@sashaaro sashaaro force-pushed the first-match branch 8 times, most recently from 2ba3ce2 to bdad6cc Compare December 31, 2025 09:25
@sashaaro sashaaro force-pushed the first-match branch 5 times, most recently from 86ab7d0 to bdfeff7 Compare December 31, 2025 09:53
@sashaaro sashaaro marked this pull request as ready for review December 31, 2025 09:59
@sashaaro
Copy link
Contributor Author

sashaaro commented Jan 5, 2026

@samber seems ready for review.

@samber samber merged commit b32274a into samber:main Jan 6, 2026
5 of 6 checks passed
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