feat: add before send callback#259
Conversation
|
Reviews (1): Last reviewed commit: "feat: add before send callback" | Re-trigger Greptile |
| [Fact] | ||
| public async Task BeforeSendCanDropEventBeforeUpload() | ||
| { | ||
| var container = new TestContainer(services => services.Configure<PostHogOptions>(options => | ||
| { | ||
| options.BeforeSend = _ => null; | ||
| })); | ||
| var requestHandler = container.FakeHttpMessageHandler.AddBatchResponse(); | ||
| var client = container.Activate<PostHogClient>(); | ||
|
|
||
| Assert.True(client.Capture("test-user", "drop-me")); | ||
| await client.FlushAsync(); | ||
|
|
||
| Assert.Empty(requestHandler.ReceivedRequests); | ||
| } |
There was a problem hiding this comment.
Missing test for exception thrown by
BeforeSend
ApplyBeforeSend wraps the callback in a try/catch that logs an error and drops the event when the callback throws. That catch branch is never exercised by the two tests added here. If something changes in that error path (logging, return value, re-throw), it would go undetected. A third test that configures BeforeSend to throw and then asserts the event is not sent would fully cover this path.
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
PostHogOptions.BeforeSend, invoked after event enrichment (SDK properties, super properties,$is_server, groups, and feature flags) and before batch serialization. Returningnulldrops the event.💚 How did you test it?
dotnet test tests/UnitTests/UnitTests.csproj --filter 'FullyQualifiedName~PostHogClientTests.TheCaptureMethod'dotnet build src/PostHog/PostHog.csproj📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Implemented with the pi coding agent. The hook runs after the event is fully enriched by moving before-send processing into the batch resolution path, so feature flag enrichment is visible to the callback before serialization. A dropped event is filtered out of the outgoing batch.