-
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
Open
marandaneto
wants to merge
11
commits into
main
Choose a base branch
from
chore/exception-autocapture
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.
+614
−73
Open
Changes from 4 commits
Commits
Show all changes
11 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 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
75 changes: 75 additions & 0 deletions
75
posthog/src/main/java/com/posthog/exceptions/PostHogExceptionAutoCaptureIntegration.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,75 @@ | ||
package com.posthog.exceptions | ||
|
||
import com.posthog.PostHogConfig | ||
import com.posthog.PostHogIntegration | ||
import com.posthog.PostHogInterface | ||
import com.posthog.internal.exceptions.PostHogThrowable | ||
import com.posthog.internal.exceptions.UncaughtExceptionHandlerAdapter | ||
|
||
public class PostHogExceptionAutoCaptureIntegration : 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 | ||
} | ||
|
||
private companion object { | ||
@Volatile | ||
private var integrationInstalled = false | ||
} | ||
|
||
override fun install(postHog: PostHogInterface) { | ||
if (integrationInstalled) { | ||
return | ||
} | ||
this.postHog = postHog | ||
|
||
if (!config.exceptionAutocapture) { | ||
return | ||
} | ||
|
||
val currentExceptionHandler = adapterExceptionHandler.getDefaultUncaughtExceptionHandler() | ||
|
||
if (currentExceptionHandler != null) { | ||
if (currentExceptionHandler !is PostHogExceptionAutoCaptureIntegration) { | ||
defaultExceptionHandler = currentExceptionHandler | ||
} | ||
} else { | ||
defaultExceptionHandler = null | ||
} | ||
adapterExceptionHandler.setDefaultUncaughtExceptionHandler(this) | ||
|
||
integrationInstalled = true | ||
config.logger.log("Exception autocapture is enabled.") | ||
} | ||
|
||
override fun uninstall() { | ||
if (!integrationInstalled) { | ||
return | ||
} | ||
adapterExceptionHandler.setDefaultUncaughtExceptionHandler(defaultExceptionHandler) | ||
integrationInstalled = false | ||
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) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -48,60 +48,89 @@ internal class PostHogQueue( | |
|
||
private val delay: Long get() = (config.flushIntervalSeconds * 1000).toLong() | ||
|
||
override fun add(event: PostHogEvent) { | ||
executor.executeSafely { | ||
var removeFirst = false | ||
if (deque.size >= config.maxQueueSize) { | ||
removeFirst = true | ||
private fun addEventSync(event: PostHogEvent): Boolean { | ||
storagePrefix?.let { | ||
val dir = File(it, config.apiKey) | ||
|
||
if (!dirCreated) { | ||
dir.mkdirs() | ||
dirCreated = true | ||
} | ||
|
||
if (removeFirst) { | ||
try { | ||
val first: File | ||
synchronized(dequeLock) { | ||
first = deque.removeFirst() | ||
} | ||
first.deleteSafely(config) | ||
config.logger.log("Queue is full, the oldest event ${first.name} is dropped.") | ||
} catch (ignored: NoSuchElementException) { | ||
val uuid = event.uuid ?: TimeBasedEpochGenerator.generate() | ||
val file = File(dir, "$uuid.event") | ||
synchronized(dequeLock) { | ||
deque.add(file) | ||
} | ||
|
||
try { | ||
val os = config.encryption?.encrypt(file.outputStream()) ?: file.outputStream() | ||
os.use { theOutputStream -> | ||
config.serializer.serialize(event, theOutputStream.writer().buffered()) | ||
} | ||
config.logger.log("Queued Event ${event.event}: ${file.name}.") | ||
|
||
return true | ||
} catch (e: Throwable) { | ||
config.logger.log("Event ${event.event}: ${file.name} failed to parse: $e.") | ||
|
||
// if for some reason the file failed to serialize, lets delete it | ||
file.deleteSafely(config) | ||
} | ||
|
||
storagePrefix?.let { | ||
val dir = File(it, config.apiKey) | ||
return false | ||
} | ||
|
||
if (!dirCreated) { | ||
dir.mkdirs() | ||
dirCreated = true | ||
} | ||
// if there's no storagePrefix, we assume it failed | ||
return true | ||
} | ||
|
||
private fun removeEventSync() { | ||
var removeFirst = false | ||
if (deque.size >= config.maxQueueSize) { | ||
removeFirst = true | ||
} | ||
|
||
val uuid = event.uuid ?: TimeBasedEpochGenerator.generate() | ||
val file = File(dir, "$uuid.event") | ||
if (removeFirst) { | ||
try { | ||
val first: File | ||
synchronized(dequeLock) { | ||
deque.add(file) | ||
first = deque.removeFirst() | ||
} | ||
first.deleteSafely(config) | ||
config.logger.log("Queue is full, the oldest event ${first.name} is dropped.") | ||
} catch (ignored: NoSuchElementException) { | ||
} | ||
} | ||
} | ||
|
||
try { | ||
val os = config.encryption?.encrypt(file.outputStream()) ?: file.outputStream() | ||
os.use { theOutputStream -> | ||
config.serializer.serialize(event, theOutputStream.writer().buffered()) | ||
} | ||
config.logger.log("Queued Event ${event.event}: ${file.name}.") | ||
|
||
flushIfOverThreshold() | ||
} catch (e: Throwable) { | ||
config.logger.log("Event ${event.event}: ${file.name} failed to parse: $e.") | ||
private fun flushEventSync( | ||
event: PostHogEvent, | ||
isFatal: Boolean = false, | ||
) { | ||
removeEventSync() | ||
if (addEventSync(event)) { | ||
// this is best effort since we dont know if theres | ||
// enough time to flush events to the wire | ||
flushIfOverThreshold(isFatal) | ||
} | ||
} | ||
|
||
// if for some reason the file failed to serialize, lets delete it | ||
file.deleteSafely(config) | ||
} | ||
override fun add(event: PostHogEvent) { | ||
if (event.isFatalExceptionEvent()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice way of doing this without changing public API 👍 |
||
executor.submitSyncSafely { | ||
flushEventSync(event, true) | ||
} | ||
} else { | ||
executor.executeSafely { | ||
flushEventSync(event) | ||
Comment on lines
+120
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if its fatal, flush it sync otherwise async |
||
} | ||
} | ||
} | ||
|
||
private fun flushIfOverThreshold() { | ||
private fun flushIfOverThreshold(isFatal: Boolean) { | ||
if (isAboveThreshold(config.flushAt)) { | ||
flushBatch() | ||
flushBatch(isFatal) | ||
} | ||
} | ||
|
||
|
@@ -132,8 +161,8 @@ internal class PostHogQueue( | |
return events | ||
} | ||
|
||
private fun flushBatch() { | ||
if (!canFlushBatch()) { | ||
private fun flushBatch(isFatal: Boolean) { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!isFatal && !canFlushBatch()) { | ||
config.logger.log("Cannot flush the Queue.") | ||
return | ||
} | ||
|
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