-
Notifications
You must be signed in to change notification settings - Fork 31
chore: exception autocapture #305
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39026c9
chore: exception autocapture
marandaneto e610885
fixes
marandaneto 6086771
fix
marandaneto 2ac9c91
changelog
marandaneto def2707
lint
marandaneto a1c9876
fix
marandaneto 6ffdbed
rename
marandaneto 22367d4
add integration tests for PostHogErrorTrackingAutoCaptureIntegrationTest
marandaneto 3484cc0
test
marandaneto c0773fe
tests
marandaneto 58a3c13
fix
marandaneto 6a2eefb
fix test
marandaneto 2823268
ref
marandaneto 4298991
fix
marandaneto 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
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
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
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
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
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
82 changes: 82 additions & 0 deletions
82
...hog/src/main/java/com/posthog/errortracking/PostHogErrorTrackingAutoCaptureIntegration.kt
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,82 @@ | ||
| package com.posthog.errortracking | ||
|
|
||
| import com.posthog.PostHogConfig | ||
| import com.posthog.PostHogIntegration | ||
| import com.posthog.PostHogInterface | ||
| import com.posthog.internal.errortracking.PostHogThrowable | ||
| import com.posthog.internal.errortracking.UncaughtExceptionHandlerAdapter | ||
|
|
||
| public class PostHogErrorTrackingAutoCaptureIntegration : PostHogIntegration, Thread.UncaughtExceptionHandler { | ||
| private val config: PostHogConfig | ||
| private val adapterExceptionHandler: UncaughtExceptionHandlerAdapter | ||
| private var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null | ||
| private var postHog: PostHogInterface? = null | ||
|
|
||
| public constructor(config: PostHogConfig) { | ||
| this.config = config | ||
| this.adapterExceptionHandler = UncaughtExceptionHandlerAdapter.Adapter.getInstance() | ||
| } | ||
|
|
||
| internal constructor(config: PostHogConfig, adapterExceptionHandler: UncaughtExceptionHandlerAdapter) { | ||
| this.config = config | ||
| this.adapterExceptionHandler = adapterExceptionHandler | ||
| } | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private companion object { | ||
| @Volatile | ||
| private var integrationInstalled = false | ||
| } | ||
|
|
||
| override fun install(postHog: PostHogInterface) { | ||
| if (integrationInstalled) { | ||
| return | ||
| } | ||
|
|
||
| if (!config.errorTrackingConfig.autoCapture) { | ||
| return | ||
| } | ||
|
|
||
| this.postHog = postHog | ||
|
|
||
| val currentExceptionHandler = adapterExceptionHandler.getDefaultUncaughtExceptionHandler() | ||
|
|
||
| if (currentExceptionHandler != null) { | ||
| if (currentExceptionHandler !is PostHogErrorTrackingAutoCaptureIntegration) { | ||
| defaultExceptionHandler = currentExceptionHandler | ||
| installHandler() | ||
| } | ||
| // we don't install if the handler is us already | ||
| } else { | ||
| defaultExceptionHandler = null | ||
| installHandler() | ||
| } | ||
| } | ||
|
|
||
| private fun installHandler() { | ||
| adapterExceptionHandler.setDefaultUncaughtExceptionHandler(this) | ||
| integrationInstalled = true | ||
| config.logger.log("Exception autocapture is enabled.") | ||
| } | ||
|
|
||
| override fun uninstall() { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!integrationInstalled) { | ||
| return | ||
| } | ||
| adapterExceptionHandler.setDefaultUncaughtExceptionHandler(defaultExceptionHandler) | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| integrationInstalled = false | ||
| postHog = null | ||
| config.logger.log("Exception autocapture is disabled.") | ||
| } | ||
|
|
||
| override fun uncaughtException( | ||
| thread: Thread, | ||
| throwable: Throwable, | ||
| ) { | ||
| postHog?.let { postHog -> | ||
| postHog.captureException(PostHogThrowable(throwable, thread)) | ||
| postHog.flush() | ||
| } | ||
|
|
||
| defaultExceptionHandler?.uncaughtException(thread, throwable) | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
posthog/src/main/java/com/posthog/errortracking/PostHogErrorTrackingConfig.kt
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,30 @@ | ||
| package com.posthog.errortracking | ||
|
|
||
| import com.posthog.PostHog | ||
|
|
||
| public class PostHogErrorTrackingConfig | ||
| @JvmOverloads | ||
| public constructor( | ||
| /** | ||
| * Enable autocapture of exceptions | ||
| * This feature installs an uncaught exception handler (Thread.UncaughtExceptionHandler) that will capture exceptions | ||
| * | ||
| * Disabled by default | ||
| * | ||
| * You can manually capture exceptions by calling [PostHog.captureException] | ||
| */ | ||
| public var autoCapture: Boolean = false, | ||
| /** | ||
| * List of package names to be considered inApp frames for error tracking | ||
| * | ||
| * inApp Example: | ||
| * inAppIncludes=["com.yourapp"] | ||
| * All Exception stacktrace frames that start with com.yourapp will be considered inApp* | ||
| * | ||
| * On Android only frames coming from the app's package name will be considered inApp by default | ||
| * On Android, We add your app's package name to this list automatically (read from applicationId at runtime) | ||
| * | ||
| * If this list of package names is empty, all frames will be considered inApp | ||
| */ | ||
| public val inAppIncludes: MutableList<String> = mutableListOf(), | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok breaking change since the manual capture isnt released yet