feat: add before send callback#161
Conversation
|
Reviews (1): Last reviewed commit: "feat: add before send callback" | Re-trigger Greptile |
| defp run_before_send(before_send, event) when is_function(before_send, 1) do | ||
| case before_send.(event) do | ||
| nil -> | ||
| nil | ||
|
|
||
| %{} = event -> | ||
| event | ||
|
|
||
| other -> | ||
| Logger.error( | ||
| "PostHog before_send callback returned #{inspect(other)} instead of an event map or nil; dropping event" | ||
| ) | ||
|
|
||
| nil | ||
| end | ||
| rescue | ||
| exception -> | ||
| Logger.error( | ||
| "PostHog before_send callback raised; dropping event: #{Exception.message(exception)}" | ||
| ) | ||
|
|
||
| nil | ||
| end |
There was a problem hiding this comment.
Error-handling paths in
run_before_send are untested
run_before_send/2 has two error branches — when the callback returns a non-nil, non-map value and when the callback raises an exception — but neither is exercised by the new tests. Per the team's preference for parameterised tests, these could live alongside the existing before_send tests as additional cases (e.g., @tag test table entries, or describe-level setup with different callbacks). Without coverage, a future refactor of the rescue block or the other -> branch won't be caught by the test suite.
Context Used: Do not attempt to comment on incorrect alphabetica... (source)
| } | ||
|
|
||
| PostHog.Sender.send(event, name) | ||
| case run_before_send(Map.get(config, :before_send), event) do |
There was a problem hiding this comment.
Map.get(config, :before_send) is inconsistent with how all other config keys are accessed in bare_capture (e.g., config.global_properties). Since before_send always exists in the validated config (defaulting to nil), dot-access is sufficient and matches the surrounding style.
| case run_before_send(Map.get(config, :before_send), event) do | |
| case run_before_send(config.before_send, event) do |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Closing in favor of a PR from a branch on the PostHog repository. |
💡 Motivation and Context
Customers need a configuration hook to inspect, modify, or drop events before upload, matching the before-send behavior available in other PostHog SDKs.
This adds a
:before_sendconfig callback. It receives the fully enriched event map after global/system properties, UUID, timestamp, and redaction have been applied. Returningnildrops the event.💚 How did you test it?
mix format --check-formattedmix test test/posthog_test.exsmix posthog.public_api --check📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Implemented with the pi coding agent. The callback is applied just before handing the enriched event to the sender, with invalid return values or raised exceptions dropping only that event.