Skip to content
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

feat: sampling that can be shared #1700

Merged
merged 2 commits into from
Jan 29, 2025
Merged

Conversation

pauldambra
Copy link
Member

the suggested implementation of session id sampling for before_send is different to that used in session recording

the one in before_send is nicer as its deterministic for each session id

by switching to the before_send implementation someone can set a sample rate for session recording, and sample events by session id in before_send and only get events for sessions that have recordings 🦾

(suggested by @camerondeleone in slack somewhere)

Copy link

vercel bot commented Jan 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Jan 29, 2025 4:25pm

@pauldambra pauldambra requested a review from veryayskiy January 29, 2025 15:38
@pauldambra pauldambra added the bump minor Bump minor version when this PR gets merged label Jan 29, 2025
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 introduces a unified sampling mechanism across PostHog JS, making session ID sampling consistent between before_send and session recording functionalities.

  • Added new /src/extensions/sampling.ts with deterministic hashing algorithm for consistent sampling decisions
  • Modified session recording sampling in /src/extensions/replay/sessionrecording.ts to use sampleOnProperty instead of Math.random()
  • Refactored sampling utilities from before-send.ts into shared implementation for better code reuse
  • Added tests in /src/__tests__/utils/before-send-utils.test.ts to verify consistent sampling behavior across different methods
  • Ensures events are only captured for sessions that have recordings when both sampling methods are used

5 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +24 to +25
export function sampleOnProperty(prop: string, percent: number): boolean {
return simpleHash(prop) % 100 < clampToRange(percent * 100, 0, 100)
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: No input validation for percent parameter. Should verify percent is between 0 and 1 before using clampToRange.

Suggested change
export function sampleOnProperty(prop: string, percent: number): boolean {
return simpleHash(prop) % 100 < clampToRange(percent * 100, 0, 100)
export function sampleOnProperty(prop: string, percent: number): boolean {
if (percent < 0 || percent > 1) {
throw new Error('percent must be between 0 and 1');
}
return simpleHash(prop) % 100 < clampToRange(percent * 100, 0, 100)

Copy link
Member Author

Choose a reason for hiding this comment

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

that's what clamp to range is doing, silly robot

Comment on lines +8 to +10
export function updateThreshold(currentValue: number | undefined, percent: number): number {
return (isUndefined(currentValue) ? 1 : currentValue) * percent
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: updateThreshold could return Infinity if percent is very large and multiplied multiple times. Consider adding upper bound check.

Copy link

github-actions bot commented Jan 29, 2025

Size Change: +1.22 kB (+0.04%)

Total Size: 3.28 MB

Filename Size Change
dist/array.full.es5.js 266 kB +122 B (+0.05%)
dist/array.full.js 369 kB +122 B (+0.03%)
dist/array.full.no-external.js 368 kB +122 B (+0.03%)
dist/array.js 181 kB +122 B (+0.07%)
dist/array.no-external.js 180 kB +122 B (+0.07%)
dist/main.js 182 kB +122 B (+0.07%)
dist/module.full.js 369 kB +122 B (+0.03%)
dist/module.full.no-external.js 368 kB +122 B (+0.03%)
dist/module.js 181 kB +122 B (+0.07%)
dist/module.no-external.js 180 kB +122 B (+0.07%)
ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 215 kB
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 68.8 kB
dist/surveys.js 71.8 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@pauldambra pauldambra removed the request for review from a team January 29, 2025 16:11
Copy link

@veryayskiy veryayskiy left a comment

Choose a reason for hiding this comment

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

lgtm

@pauldambra pauldambra merged commit a3a720f into main Jan 29, 2025
28 checks passed
@pauldambra pauldambra deleted the feat/sampling-that-can-be-shared branch January 29, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants