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: beforeSend / fingerprinting #70

Merged
merged 25 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Kotlin
kotlin.version=1.8.0
buenaflor marked this conversation as resolved.
Show resolved Hide resolved
kotlin.incremental.multiplatform=true
kotlin.code.style=official
kotlin.mpp.stability.nowarn=true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.extensions.toKmpBreadcrumb
import io.sentry.kotlin.multiplatform.extensions.toKmpMessage
import io.sentry.kotlin.multiplatform.extensions.toKmpSentryException
import io.sentry.kotlin.multiplatform.extensions.toKmpSentryLevel
import io.sentry.kotlin.multiplatform.extensions.toKmpUser
import io.sentry.kotlin.multiplatform.protocol.Message
import io.sentry.kotlin.multiplatform.protocol.SentryException
import io.sentry.kotlin.multiplatform.protocol.SentryId
import io.sentry.kotlin.multiplatform.protocol.User

public actual class SentryEvent actual constructor() : SentryBaseEvent() {

public actual var level: SentryLevel? = null
public actual var message: Message? = null
public actual var logger: String? = null
public actual var fingerprint: List<String>? = null
public actual var exceptions: List<SentryException>? = null
public override var release: String? = null
public override var environment: String? = null
public override var platform: String? = null
public override var user: User? = null
public override var serverName: String? = null
public override var dist: String? = null

public constructor(cocoaSentryEvent: CocoaSentryEvent?) : this() {
this.eventId = SentryId(cocoaSentryEvent?.eventId.toString())
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
this.level = cocoaSentryEvent?.level?.toKmpSentryLevel()
this.message = cocoaSentryEvent?.message?.toKmpMessage()
this.logger = cocoaSentryEvent?.logger
this.fingerprint = cocoaSentryEvent?.fingerprint()?.toList() as? List<String>
this.exceptions =
cocoaSentryEvent?.exceptions?.map { (it as CocoaSentryException).toKmpSentryException() }
?.toList()
this.release = cocoaSentryEvent?.releaseName
this.environment = cocoaSentryEvent?.environment
this.platform = cocoaSentryEvent?.platform
this.user = cocoaSentryEvent?.user?.toKmpUser()
this.serverName = cocoaSentryEvent?.serverName
this.dist = cocoaSentryEvent?.dist
this.mutableContexts =
cocoaSentryEvent?.context?.mapKeys { it.key as String }?.mapValues { it.value as Any }
cocoaSentryEvent?.breadcrumbs?.mapNotNull { it as? CocoaBreadcrumb }?.forEach {
this.addBreadcrumb(it.toKmpBreadcrumb())
}
cocoaSentryEvent?.tags?.filterValues { it is String }?.forEach { (key, value) ->
this.setTag(key as String, value as String)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package io.sentry.kotlin.multiplatform

import cocoapods.Sentry.SentryAttachment
import cocoapods.Sentry.SentryBreadcrumb
import cocoapods.Sentry.SentryEvent
import cocoapods.Sentry.SentryException
import cocoapods.Sentry.SentryId
import cocoapods.Sentry.SentryLevel
import cocoapods.Sentry.SentryMessage
import cocoapods.Sentry.SentryOptions
import cocoapods.Sentry.SentryScope
import cocoapods.Sentry.SentryUser
Expand All @@ -17,3 +20,6 @@ internal typealias CocoaSentryId = SentryId
internal typealias CocoaSentryLevel = SentryLevel
internal typealias CocoaAttachment = SentryAttachment
internal typealias CocoaUserFeedback = SentryUserFeedback
internal typealias CocoaSentryEvent = SentryEvent
internal typealias CocoaMessage = SentryMessage
internal typealias CocoaSentryException = SentryException
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.CocoaMessage
import io.sentry.kotlin.multiplatform.protocol.Message

internal fun CocoaMessage.toKmpMessage() = Message(
message = this.message,
params = this.params as? List<String>,
formatted = this.formatted
)

internal fun Message.toCocoaMessage(): CocoaMessage {
val scope = this@toCocoaMessage
val cocoaMessage = scope.formatted?.let { CocoaMessage(it) } ?: CocoaMessage()
return cocoaMessage.apply {
this.message = scope.message
this.params = scope.params
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.sentry.kotlin.multiplatform.extensions

import cocoapods.Sentry.SentryId
import io.sentry.kotlin.multiplatform.CocoaSentryEvent
import io.sentry.kotlin.multiplatform.SentryEvent

internal fun CocoaSentryEvent.applyKmpEvent(kmpEvent: SentryEvent): CocoaSentryEvent {
kmpEvent.level?.let { this.level = it.toCocoaSentryLevel() }
kmpEvent.platform?.let { this.platform = it }
this.message = kmpEvent.message?.toCocoaMessage()
this.logger = kmpEvent.logger
this.fingerprint = kmpEvent.fingerprint
this.releaseName = kmpEvent.release
this.environment = kmpEvent.environment
this.user = kmpEvent.user?.toCocoaUser()
this.serverName = kmpEvent.serverName
this.dist = kmpEvent.dist
this.breadcrumbs = kmpEvent.breadcrumbs?.map { it.toCocoaBreadcrumb() }?.toMutableList()
this.tags = kmpEvent.tags?.toMutableMap()
this.eventId = SentryId(kmpEvent.eventId.toString())
return this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.CocoaSentryException
import io.sentry.kotlin.multiplatform.protocol.SentryException

internal fun CocoaSentryException.toKmpSentryException() = SentryException(
type = this.type,
value = this.value,
module = this.module,
threadId = this.threadId as Long?
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.sentry.kotlin.multiplatform.extensions

import PrivateSentrySDKOnly.Sentry.PrivateSentrySDKOnly
import cocoapods.Sentry.SentryEvent
import io.sentry.kotlin.multiplatform.BuildKonfig
import io.sentry.kotlin.multiplatform.CocoaSentryEvent
import io.sentry.kotlin.multiplatform.CocoaSentryOptions
import io.sentry.kotlin.multiplatform.SentryEvent
import io.sentry.kotlin.multiplatform.SentryOptions
import io.sentry.kotlin.multiplatform.nsexception.dropKotlinCrashEvent
import kotlinx.cinterop.convert
Expand Down Expand Up @@ -48,7 +49,12 @@ internal fun CocoaSentryOptions.applyCocoaBaseOptions(options: SentryOptions) {
sdk?.set("packages", packages)

event?.sdk = sdk
dropKotlinCrashEvent(event as NSExceptionSentryEvent?) as SentryEvent?

val modifiedEvent = options.beforeSend?.invoke(SentryEvent(event))?.let {
event?.applyKmpEvent(it)
}

dropKotlinCrashEvent(modifiedEvent as NSExceptionSentryEvent?) as CocoaSentryEvent?
}

val sdkName = options.sdk?.name ?: BuildKonfig.SENTRY_KMP_COCOA_SDK_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import io.sentry.kotlin.multiplatform.protocol.User

internal fun User.toCocoaUser() = CocoaUser().apply {
val scope = this@toCocoaUser
userId = scope.id
username = scope.username
email = scope.email
ipAddress = scope.ipAddress
this.userId = scope.id
buenaflor marked this conversation as resolved.
Show resolved Hide resolved
this.username = scope.username
this.email = scope.email
this.ipAddress = scope.ipAddress
}

internal fun CocoaUser.toKmpUser() = User().apply {
val scope = this@toKmpUser
id = scope.userId.toString()
username = scope.username.toString()
email = scope.email.toString()
ipAddress = scope.ipAddress.toString()
setData(scope.data)
this.id = scope.userId
this.username = scope.username
this.email = scope.email
this.ipAddress = scope.ipAddress
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.extensions.toKmpBreadcrumb
import io.sentry.kotlin.multiplatform.extensions.toKmpMessage
import io.sentry.kotlin.multiplatform.extensions.toKmpSentryException
import io.sentry.kotlin.multiplatform.extensions.toKmpSentryLevel
import io.sentry.kotlin.multiplatform.extensions.toKmpUser
import io.sentry.kotlin.multiplatform.protocol.Message
import io.sentry.kotlin.multiplatform.protocol.SentryException
import io.sentry.kotlin.multiplatform.protocol.SentryId
import io.sentry.kotlin.multiplatform.protocol.User

public actual class SentryEvent actual constructor() : SentryBaseEvent() {

public actual var level: SentryLevel? = null
public actual var message: Message? = null
public actual var logger: String? = null
public actual var fingerprint: List<String>? = null
public actual var exceptions: List<SentryException>? = null
public override var release: String? = null
public override var environment: String? = null
public override var platform: String? = null
public override var user: User? = null
public override var serverName: String? = null
public override var dist: String? = null

public constructor(jvmSentryEvent: JvmSentryEvent) : this() {
this.eventId = SentryId(jvmSentryEvent.eventId.toString())
this.level = jvmSentryEvent.level?.toKmpSentryLevel()
this.message = jvmSentryEvent.message?.toKmpMessage()
this.logger = jvmSentryEvent.logger
this.fingerprint = jvmSentryEvent.fingerprints?.toList()
this.exceptions = jvmSentryEvent.exceptions?.map { it.toKmpSentryException() }?.toList()
this.release = jvmSentryEvent.release
this.environment = jvmSentryEvent.environment
this.platform = jvmSentryEvent.platform
this.user = jvmSentryEvent.user?.toKmpUser()
this.serverName = jvmSentryEvent.serverName
this.dist = jvmSentryEvent.dist
this.mutableContexts = jvmSentryEvent.contexts
jvmSentryEvent.breadcrumbs?.forEach { breadcrumb ->
this.addBreadcrumb(breadcrumb.toKmpBreadcrumb())
}
jvmSentryEvent.tags?.forEach { (key, value) ->
this.setTag(key, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package io.sentry.kotlin.multiplatform
import io.sentry.Attachment
import io.sentry.Breadcrumb
import io.sentry.Scope
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.UserFeedback
import io.sentry.protocol.Contexts
import io.sentry.protocol.Message
import io.sentry.protocol.SentryException
import io.sentry.protocol.SentryId
import io.sentry.protocol.User

Expand All @@ -17,3 +21,7 @@ internal typealias JvmSentryId = SentryId
internal typealias JvmSentryOptions = SentryOptions
internal typealias JvmAttachment = Attachment
internal typealias JvmUserFeedback = UserFeedback
internal typealias JvmSentryEvent = SentryEvent
internal typealias JvmMessage = Message
internal typealias JvmSentryException = SentryException
internal typealias JvmContexts = Contexts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.JvmMessage
import io.sentry.kotlin.multiplatform.protocol.Message

internal fun JvmMessage.toKmpMessage() = Message(
message = this.message,
params = this.params,
formatted = this.formatted
)

internal fun Message.toJvmMessage() = JvmMessage().apply {
val scope = this@toJvmMessage
this.message = scope.message
this.params = scope.params
this.formatted = scope.formatted
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.JvmSentryEvent
import io.sentry.kotlin.multiplatform.JvmSentryId
import io.sentry.kotlin.multiplatform.SentryEvent

internal fun JvmSentryEvent.applyKmpEvent(kmpEvent: SentryEvent): JvmSentryEvent {
this.level = kmpEvent.level?.toJvmSentryLevel()
this.message = kmpEvent.message?.toJvmMessage()
this.logger = kmpEvent.logger
this.fingerprints = kmpEvent.fingerprint
this.release = kmpEvent.release
this.environment = kmpEvent.environment
this.platform = kmpEvent.platform
this.user = kmpEvent.user?.toJvmUser()
this.serverName = kmpEvent.serverName
this.dist = kmpEvent.dist
this.breadcrumbs = kmpEvent.breadcrumbs?.map { it.toJvmBreadcrumb() }?.toMutableList()
this.eventId = JvmSentryId(kmpEvent.eventId.toString())
this.tags = kmpEvent.tags?.toMutableMap()
return this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.JvmSentryException
import io.sentry.kotlin.multiplatform.protocol.SentryException

internal fun JvmSentryException.toKmpSentryException() = SentryException(
type = this.type,
value = this.value,
module = this.module,
threadId = this.threadId
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.BuildKonfig
import io.sentry.kotlin.multiplatform.JvmSentryOptions
import io.sentry.kotlin.multiplatform.SentryEvent
import io.sentry.kotlin.multiplatform.SentryOptions

internal fun SentryOptions.toJvmSentryOptionsCallback(): (JvmSentryOptions) -> Unit = {
Expand All @@ -16,7 +17,10 @@ internal fun SentryOptions.toJvmSentryOptionsCallback(): (JvmSentryOptions) -> U
}

if (it.sdkVersion?.packages?.none { it.name == BuildKonfig.SENTRY_JAVA_PACKAGE_NAME } == true) {
it.sdkVersion?.addPackage(BuildKonfig.SENTRY_JAVA_PACKAGE_NAME, BuildKonfig.SENTRY_JAVA_VERSION)
it.sdkVersion?.addPackage(
BuildKonfig.SENTRY_JAVA_PACKAGE_NAME,
BuildKonfig.SENTRY_JAVA_VERSION
)
}
}

Expand All @@ -39,4 +43,9 @@ internal fun JvmSentryOptions.applyJvmBaseOptions(options: SentryOptions) {
this.setBeforeBreadcrumb { jvmBreadcrumb, _ ->
options.beforeBreadcrumb?.invoke(jvmBreadcrumb.toKmpBreadcrumb())?.toJvmBreadcrumb()
}
this.setBeforeSend { jvmSentryEvent, hint ->
options.beforeSend?.invoke(SentryEvent(jvmSentryEvent))?.let {
jvmSentryEvent.applyKmpEvent(it)
buenaflor marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import io.sentry.kotlin.multiplatform.protocol.User

internal fun User.toJvmUser() = JvmUser().apply {
val scope = this@toJvmUser
id = scope.id
username = scope.username
email = scope.email
ipAddress = scope.ipAddress
others = scope.other?.toMutableMap()
unknown = scope.unknown?.toMutableMap()
this.id = scope.id
this.username = scope.username
this.email = scope.email
this.ipAddress = scope.ipAddress
this.others = scope.other?.toMutableMap()
this.unknown = scope.unknown?.toMutableMap()
}

internal fun JvmUser.toKmpUser() = User().apply {
val scope = this@toKmpUser
id = scope.id.toString()
username = scope.username.toString()
email = scope.email.toString()
ipAddress = scope.ipAddress.toString()
other = scope.others?.toMutableMap()?.let { it }
unknown = scope.unknown?.toMutableMap()?.let { it }
this.id = scope.id
this.username = scope.username
this.email = scope.email
this.ipAddress = scope.ipAddress
this.other = scope.others?.toMutableMap()
this.unknown = scope.unknown?.toMutableMap()
}
Loading