feat: Implement BeforeSend hook for message modification and filtering.#154
feat: Implement BeforeSend hook for message modification and filtering.#154hyeomans wants to merge 2 commits intoPostHog:masterfrom
Conversation
Implements a `BeforeSend` hook that allows users to modify or drop messages before they're sent to PostHog, matching the Ruby SDK's `before_send` functionality.
|
Hey PostHog team! 👋 I noticed issue #135 about implementing a What I've implemented:
The implementation allows users to modify/filter events, scrub sensitive data, add metadata, and implement sampling - all before events are sent to PostHog. Happy to make any changes or add more tests based on your feedback! Full transparency: I recently applied for a Product Engineer position and have a call with your recruiter this Thursday. I wanted to contribute something meaningful to show my interest in PostHog and demonstrate that I can ship quality work. Regardless of the outcome, I hope this is useful for the community! 🚀 Looking forward to your feedback! |
|
Hey @hyeomans! Thanks for this! Mind fixing the merge conflicts before we can take a look at this? |
| // } | ||
| // }, | ||
| // }) | ||
| BeforeSend func(Message) Message |
There was a problem hiding this comment.
Was working recently on flutter implementation of this and one of the feedback I got is that other sdks support a single function and or an array of before functions so that they can be chained. Should we be consistent here as well? cc @marandaneto
There was a problem hiding this comment.
If I may suggest: a BeforeSendHooks []BeforeSendFunc function could be added in a follow-up PR.
That would let consumers register either a single BeforeSend hook or multiple hooks that run in order.
We could also support defining both (BeforeSend + BeforeSendHooks) and execute them sequentially inside processBeforeSend (dropping the message if any hook returns nil, and preserving the existing panic/validation handling).
Implements a
BeforeSendhook that allows users to modify or drop messages before they're sent to PostHog, matching the Ruby SDK'sbefore_sendfunctionality.Motivation
Users need the ability to:
This is a common pattern across SDKs - the Ruby SDK already has this feature, and now Go does too.
Changes
Core Implementation (
posthog.go)extractEventName()helper - Extracts descriptive event names for loggingprocessBeforeSend()method - Core hook processing with:Enqueue()- Called after type-specific enrichment but beforeAPIfy(), so users work with strongly-typed Message objectsDocumentation (
config.go)Example Usage
Add metadata to all events
Error Handling
The implementation is resilient to buggy user hooks:
Design Decisions
Hook Placement
After enrichment, before APIfy() - This ensures:
Ruby SDK Compatibility
Breaking Changes
Checklist
Related Issues
Closes #135