-
Notifications
You must be signed in to change notification settings - Fork 56
Observability PII session context #231
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
Open
iamEvanYT
wants to merge
6
commits into
main
Choose a base branch
from
cursor/observability-pii-session-context-6cec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a31fd1a
Improve PostHog observability: add PII sanitization and session context
cursoragent 72c55e7
Add lifecycle events, performance metrics, crash reporter, and disabl…
cursoragent 96cd98c
refactor: remove the forked posthog-node
iamEvanYT 9324906
fix and improvements
iamEvanYT 6d2ea49
fix
iamEvanYT d81ca60
fix
iamEvanYT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { performance } from "node:perf_hooks"; | ||
|
|
||
| // Use the Node process start time rather than module evaluation time. | ||
| export const appStartTimestamp = performance.timeOrigin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/main/controllers/posthog-controller/exception-autocapture.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { BucketedRateLimiter, ErrorTracking as CoreErrorTracking, createLogger } from "@posthog/core"; | ||
| import { getSessionId } from "./session"; | ||
| import { PostHog } from "posthog-node"; | ||
|
|
||
| const SHUTDOWN_TIMEOUT_MS = 2000; | ||
|
|
||
| const logger = createLogger("[PostHog exception autocapture]"); | ||
|
|
||
| type ErrorHandler = { _posthogErrorHandler: boolean } & ((error: Error) => void); | ||
|
|
||
| const errorPropertiesBuilder = new CoreErrorTracking.ErrorPropertiesBuilder( | ||
| [ | ||
| new CoreErrorTracking.EventCoercer(), | ||
| new CoreErrorTracking.ErrorCoercer(), | ||
| new CoreErrorTracking.ObjectCoercer(), | ||
| new CoreErrorTracking.StringCoercer(), | ||
| new CoreErrorTracking.PrimitiveCoercer() | ||
| ], | ||
| CoreErrorTracking.createStackParser("node:javascript", CoreErrorTracking.nodeStackLineParser) | ||
| ); | ||
|
|
||
| const rateLimiter = new BucketedRateLimiter<string>({ | ||
| refillRate: 1, | ||
| bucketSize: 10, | ||
| refillInterval: 10000, | ||
| _logger: logger | ||
| }); | ||
|
|
||
| function isPreviouslyCapturedError(error: unknown): boolean { | ||
| return ( | ||
| typeof error === "object" && | ||
| error !== null && | ||
| "__posthog_previously_captured_error" in error && | ||
| error.__posthog_previously_captured_error === true | ||
| ); | ||
| } | ||
|
|
||
| async function buildExceptionEventMessage(exception: unknown, hint: CoreErrorTracking.EventHint, distinctId: string) { | ||
| const exceptionProperties = errorPropertiesBuilder.buildFromUnknown(exception, hint); | ||
| exceptionProperties.$exception_list = await errorPropertiesBuilder.modifyFrames(exceptionProperties.$exception_list); | ||
|
|
||
| return { | ||
| event: "$exception", | ||
| distinctId, | ||
| properties: { | ||
| ...exceptionProperties, | ||
| $session_id: getSessionId() | ||
| }, | ||
| _originatedFromCaptureException: true as const | ||
| }; | ||
| } | ||
|
|
||
| async function captureAutocapturedException( | ||
| client: PostHog, | ||
| exception: unknown, | ||
| hint: CoreErrorTracking.EventHint, | ||
| distinctId: string | ||
| ): Promise<void> { | ||
| if (isPreviouslyCapturedError(exception)) { | ||
| return; | ||
| } | ||
|
|
||
| const eventMessage = await buildExceptionEventMessage(exception, hint, distinctId); | ||
|
|
||
| const exceptionType = eventMessage.properties?.$exception_list?.[0]?.type ?? "Exception"; | ||
| const isRateLimited = rateLimiter.consumeRateLimit(exceptionType); | ||
| if (isRateLimited) { | ||
| logger.info("Skipping exception capture because of client rate limiting.", { | ||
| exception: exceptionType | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| client.capture(eventMessage); | ||
| } | ||
|
|
||
| function makeUncaughtExceptionHandler( | ||
| captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void, | ||
| onFatalFn: (exception: Error) => void | ||
| ): ErrorHandler { | ||
| let calledFatalError = false; | ||
|
|
||
| return Object.assign( | ||
| (error: Error): void => { | ||
| const userProvidedListenersCount = global.process.listeners("uncaughtException").filter((listener) => { | ||
| return ( | ||
| listener.name !== "domainUncaughtExceptionClear" && (listener as ErrorHandler)._posthogErrorHandler !== true | ||
| ); | ||
| }).length; | ||
|
|
||
| const processWouldExit = userProvidedListenersCount === 0; | ||
|
|
||
| captureFn(error, { | ||
| mechanism: { | ||
| type: "onuncaughtexception", | ||
| handled: false | ||
| } | ||
| }); | ||
|
|
||
| if (!calledFatalError && processWouldExit) { | ||
| calledFatalError = true; | ||
| onFatalFn(error); | ||
| } | ||
| }, | ||
| { _posthogErrorHandler: true } | ||
| ); | ||
| } | ||
|
|
||
| function addUncaughtExceptionListener( | ||
| captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void, | ||
| onFatalFn: (exception: Error) => void | ||
| ): void { | ||
| global.process.on("uncaughtException", makeUncaughtExceptionHandler(captureFn, onFatalFn)); | ||
| } | ||
|
|
||
| function addUnhandledRejectionListener( | ||
| captureFn: (exception: unknown, hint: CoreErrorTracking.EventHint) => void | ||
| ): void { | ||
| global.process.on("unhandledRejection", (reason: unknown) => { | ||
| captureFn(reason, { | ||
| mechanism: { | ||
| type: "onunhandledrejection", | ||
| handled: false | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export function enableExceptionAutocapture(client: PostHog, distinctId: string): void { | ||
| addUncaughtExceptionListener( | ||
| (exception, hint) => { | ||
| void captureAutocapturedException(client, exception, hint, distinctId); | ||
| }, | ||
| async (error) => { | ||
| console.error(error); | ||
| await client.shutdown(SHUTDOWN_TIMEOUT_MS).catch(() => {}); | ||
| process.exit(1); | ||
| } | ||
| ); | ||
|
|
||
| addUnhandledRejectionListener((reason, hint) => { | ||
| void captureAutocapturedException(client, reason, hint, distinctId); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.