Skip to content

Commit

Permalink
Sentry: runBlocking and sample rate
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Apr 2, 2024
1 parent d2ef82c commit 377d0f1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import dev.kord.rest.builder.message.allowedMentions
import dev.kord.rest.builder.message.create.MessageCreateBuilder
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import io.sentry.SentryLevel
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.logger.Level
import org.koin.dsl.bind
Expand Down Expand Up @@ -304,7 +305,7 @@ public open class ExtensibleBotBuilder {
* the given lambda.
* @param addDefaultIntents Whether to automatically add the required intents defined within each loaded extension
*
* @see Intents.IntentsBuilder
* @see Intents.Builder
*/
@BotBuilderDSL
public fun intents(
Expand Down Expand Up @@ -689,6 +690,9 @@ public open class ExtensibleBotBuilder {
/** Whether to ping users when responding to them. **/
public var pingInReply: Boolean = true

/** How many events to send to Sentry, as a percentage. Defaults to 1.0, meaning all events. **/
public var sampleRate: Double = 1.0

/** Builder used to construct a [SentryAdapter] instance, usually the constructor. **/
public open var builder: () -> SentryAdapter = ::SentryAdapter

Expand All @@ -706,6 +710,11 @@ public open class ExtensibleBotBuilder {
options.environment = environment
options.release = release
options.serverName = serverName

options.isAttachThreads = false
options.sampleRate = sampleRate

options.setDiagnosticLevel(SentryLevel.WARNING)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import com.kotlindiscord.kord.extensions.sentry.SentryAdapter
import com.kotlindiscord.kord.extensions.sentry.sentryId
import com.kotlindiscord.kord.extensions.utils.respond
import io.sentry.Sentry
import io.sentry.UserFeedback
import io.sentry.protocol.SentryId
import org.koin.core.component.inject
Expand Down Expand Up @@ -88,16 +87,13 @@ public class SentryExtension : Extension() {
}

val author = message.author!!
val feedback = UserFeedback(

sentry.adapter.sendFeedback(
arguments.id,
author.tag,
arguments.feedback,
author.id.toString(),
arguments.feedback
)

Sentry.captureUserFeedback(feedback)
sentry.adapter.removeEventId(arguments.id)

message.respond(
translate("extensions.sentry.thanks")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import com.kotlindiscord.kord.extensions.builders.ExtensibleBotBuilder
import com.kotlindiscord.kord.extensions.builders.SentryDataTypeBuilder
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
import com.kotlindiscord.kord.extensions.sentry.captures.SentryCapture
import com.kotlindiscord.kord.extensions.utils.runSuspended
import io.github.oshai.kotlinlogging.KotlinLogging
import io.sentry.*
import io.sentry.Sentry
import io.sentry.SentryOptions
import io.sentry.UserFeedback
import io.sentry.protocol.SentryId
import org.koin.core.component.inject

Expand Down Expand Up @@ -87,7 +90,7 @@ public open class SentryAdapter : KordExKoinComponent {
* **Note:** Doesn't use the [SentryCapture] system, and thus ignores the [SentryDataTypeBuilder].
* Disable the Sentry feedback extension if you don't want these to be submitted.
*/
public fun sendFeedback(
public suspend fun sendFeedback(
id: SentryId,

comments: String? = null,
Expand All @@ -102,7 +105,9 @@ public open class SentryAdapter : KordExKoinComponent {
if (comments != null) feedback.comments = comments
if (name != null) feedback.name = name

Sentry.captureUserFeedback(feedback)
runSuspended {
Sentry.captureUserFeedback(feedback)
}

if (removeId) {
removeEventId(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.kotlindiscord.kord.extensions.sentry.captures.SentryBreadcrumbCapture
import com.kotlindiscord.kord.extensions.sentry.captures.SentryExceptionCapture
import com.kotlindiscord.kord.extensions.sentry.captures.SentryScopeCapture
import com.kotlindiscord.kord.extensions.utils.MutableStringKeyedMap
import com.kotlindiscord.kord.extensions.utils.runSuspended
import io.sentry.*
import io.sentry.protocol.SentryId
import org.koin.core.component.inject
Expand Down Expand Up @@ -91,13 +92,15 @@ public class SentryContext : KordExKoinComponent {
if (adapter.checkCapturePredicates(capture)) {
lateinit var id: SentryId

Sentry.withScope {
capture.apply(it)
runSuspended {
Sentry.withScope {
capture.apply(it)

extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)
extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)

id = Sentry.captureEvent(event)
id = Sentry.captureEvent(event)
}
}

adapter.addEventId(id)
Expand All @@ -120,15 +123,11 @@ public class SentryContext : KordExKoinComponent {
body(capture)

if (adapter.checkCapturePredicates(capture)) {
lateinit var id: SentryId

Sentry.withScope {
val id = capture.captureThrowable {
capture.apply(it)

extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)

id = capture.captureThrowable()
}

adapter.addEventId(id)
Expand All @@ -151,13 +150,15 @@ public class SentryContext : KordExKoinComponent {
body(capture)

if (adapter.checkCapturePredicates(capture)) {
Sentry.withScope {
capture.apply(it)
runSuspended {
Sentry.withScope {
capture.apply(it)

extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)
extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)

Sentry.captureUserFeedback(feedback)
Sentry.captureUserFeedback(feedback)
}
}
}
}
Expand All @@ -176,13 +177,15 @@ public class SentryContext : KordExKoinComponent {
if (adapter.checkCapturePredicates(capture)) {
lateinit var id: SentryId

Sentry.withScope {
capture.apply(it)
runSuspended {
Sentry.withScope {
capture.apply(it)

extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)
extraContext.forEach(it::setContexts)
breadcrumbs.forEach(it::addBreadcrumb)

id = Sentry.captureMessage(message)
id = Sentry.captureMessage(message)
}
}

adapter.addEventId(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

package com.kotlindiscord.kord.extensions.sentry.captures

import com.kotlindiscord.kord.extensions.utils.runSuspended
import io.sentry.Hint
import io.sentry.IScope
import io.sentry.Sentry
import io.sentry.protocol.SentryId
import org.jetbrains.annotations.ApiStatus
Expand All @@ -21,16 +23,24 @@ public class SentryExceptionCapture(
) : SentryScopeCapture() {
/** @suppress Function meant for internal use. **/
@ApiStatus.Internal
public fun captureThrowable(): SentryId {
public suspend fun captureThrowable(
callback: (IScope) -> Unit = {},
): SentryId {
if (hints.isNotEmpty()) {
val sentryHint = Hint()

processMap(hints)
.forEach(sentryHint::set)

return Sentry.captureException(throwable, sentryHint)
return runSuspended {
Sentry.captureException(throwable, sentryHint, callback)
}
}

return Sentry.captureException(throwable)
return runSuspended {
Sentry.captureException(throwable) {
callback(it)
}
}
}
}

0 comments on commit 377d0f1

Please sign in to comment.