Skip to content

Make less decide requests when enriching events #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2025

Conversation

haacked
Copy link
Collaborator

@haacked haacked commented Apr 4, 2025

When sendFeatureFlags is true, the client makes a Decide requests to get the latest feature flags in order to enrich the captured event with the current feature flag values.

However, when processing a batch, it doesn't make sense to make a decide request multiple times for the same distinctId. This PR adds an in-memory cache that lasts for the duration of the batch.

So far, I don't think a lot of the other client libraries actually use the /batch endpoint to batch requests. And if they do, I don't think they're caching /decide requests. This is a change I'd like to consider doing for all of the clients.

haacked added 2 commits April 4, 2025 14:20
When `sendFeatureFlags` is `true`, the client makes a `Decide` requests to get the latest feature flags in order to enrich the captured event with the current feature flag values.

However, when processing a batch, it doesn't make sense to make a decide request multiple times for the same `distinctId`. This commit adds an in-memory cache that lasts for the duration of the batch.
@haacked haacked requested review from Copilot and a team April 4, 2025 21:22
Copy link
Contributor

@Copilot 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.

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

src/PostHog/Library/AsyncBatchHandler.cs:92

  • [nitpick] Consider using a more descriptive parameter name instead of '_' in the lambda to improve code clarity, for example 'context'.
public bool Enqueue(Task<TItem> item) => Enqueue(new BatchItem<TItem, TBatchContext>(_ => item));

src/PostHog/PostHogClient.cs:157

  • [nitpick] Consider extracting the inline lambda 'BatchTask' into a well-named private method to improve readability, particularly for the ternary logic.
Task<CapturedEvent> BatchTask(CapturedEventBatchContext context) => sendFeatureFlags ? AddFreshFeatureFlagDataAsync(context.FeatureFlagCache, distinctId, groups, capturedEvent) : _featureFlagsLoader.IsLoaded && eventName != "$feature_flag_called" ? AddLocalFeatureFlagDataAsync(distinctId, groups, capturedEvent) : Task.FromResult(capturedEvent);

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR optimizes feature flag enrichment by adding in-memory caching to avoid duplicate decide calls during batch processing.

  • Updated src/PostHog/PostHogClient.cs to integrate a batch context using a FallbackFeatureFlagCache.
  • Added src/PostHog/Capture/CapturedEventBatchContext.cs to encapsulate cache context for batch processing.
  • Introduced src/PostHog/Features/MemoryFeatureFlagCache.cs and FallbackFeatureFlagCache.cs for caching.
  • Refactored src/PostHog/Library/AsyncBatchHandler.cs to accept a batch context, reducing redundant decide requests.
  • Minor syntax improvement in src/PostHog/Config/PostHogOptions.cs using target-typed new.

13 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

haacked and others added 2 commits April 4, 2025 14:26
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@haacked haacked merged commit dd3d879 into main Apr 4, 2025
4 checks passed
@haacked haacked deleted the haacked/reduce-decide-calls branch April 4, 2025 21:34
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.

1 participant