Skip to content

Commit

Permalink
add initial jvm impl
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Mar 31, 2023
1 parent 2b14023 commit 1066447
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions sentry-kotlin-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
implementation(Config.Libs.kotlinStd)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.extensions.toKmpSentryLevel
import io.sentry.kotlin.multiplatform.protocol.Message

public actual class SentryEvent(cocoaSentryEvent: CocoaSentryEvent) : SentryBaseEvent() {
public actual var level: SentryLevel? = cocoaSentryEvent.level?.toKmpSentryLevel()
public actual var message: Message?
get() = TODO("Not yet implemented")
set(value) {}
public actual var logger: String?
get() = TODO("Not yet implemented")
set(value) {}
public actual var fingerprint: List<String>?
get() = TODO("Not yet implemented")
set(value) {}

public actual fun isCrashed(): Boolean {
TODO("Not yet implemented")
}

public actual fun isError(): Boolean {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform

import cocoapods.Sentry.SentryAttachment
import cocoapods.Sentry.SentryBreadcrumb
import cocoapods.Sentry.SentryEvent
import cocoapods.Sentry.SentryId
import cocoapods.Sentry.SentryLevel
import cocoapods.Sentry.SentryOptions
Expand All @@ -17,3 +18,4 @@ internal typealias CocoaSentryId = SentryId
internal typealias CocoaSentryLevel = SentryLevel
internal typealias CocoaAttachment = SentryAttachment
internal typealias CocoaUserFeedback = SentryUserFeedback
internal typealias CocoaSentryEvent = SentryEvent
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.extensions.toKmpMessage
import io.sentry.kotlin.multiplatform.extensions.toKmpSentryLevel
import io.sentry.kotlin.multiplatform.protocol.Message

public actual class SentryEvent(jvmSentryEvent: JvmSentryEvent) : SentryBaseEvent() {
public actual var level: SentryLevel? = jvmSentryEvent.level?.toKmpSentryLevel()
public actual var message: Message? = jvmSentryEvent.message?.toKmpMessage()
public actual var logger: String? = jvmSentryEvent.logger
public actual var fingerprint: List<String>? = jvmSentryEvent.fingerprints?.toList()

public actual fun isCrashed(): Boolean {
TODO("Not yet implemented")
}

public actual fun isError(): Boolean {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ 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.Message
import io.sentry.protocol.SentryId
import io.sentry.protocol.User

Expand All @@ -17,3 +19,5 @@ internal typealias JvmSentryId = SentryId
internal typealias JvmSentryOptions = SentryOptions
internal typealias JvmAttachment = Attachment
internal typealias JvmUserFeedback = UserFeedback
internal typealias JvmSentryEvent = SentryEvent
internal typealias JvmMessage = Message
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 = message,
params = params,
formatted = formatted
)

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

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

internal fun JvmSentryEvent.applyKmpEvent(kmpEvent: SentryEvent): JvmSentryEvent {
level = kmpEvent.level?.toJvmSentryLevel()
message = kmpEvent.message?.toJvmMessage()
logger = kmpEvent.logger
fingerprints = kmpEvent.fingerprint
return this
}
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 @@ -18,6 +19,14 @@ 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.setBeforeSend { event, hint ->
val kmpSentryEvent = this.beforeSend?.invoke(SentryEvent(event))
kmpSentryEvent?.let { kmpSentryEvent ->
event.applyKmpEvent(kmpSentryEvent)
}
event
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
import io.sentry.kotlin.multiplatform.protocol.SentryId
import io.sentry.kotlin.multiplatform.protocol.User

public abstract class SentryBaseEvent(public var eventId: SentryId = SentryId.EMPTY_ID) {

// public val contexts = Contexts()
public var tags: MutableMap<String, String>? = null
set(value) {
field = value?.let { HashMap(it) }
}
public var release: String? = null
public var environment: String? = null
public var platform: String? = null
public var user: User? = null
protected var throwable: Throwable? = null
public var serverName: String? = null
public var dist: String? = null
private var breadcrumbs: MutableList<Breadcrumb>? = null
set(value) {
field = value?.let { ArrayList(it) }
}
private var extra: MutableMap<String, Any>? = null
set(value) {
field = value?.let { HashMap(it) }
}

public fun removeTag(key: String) {
tags?.remove(key)
}

public fun getTag(key: String): String? = tags?.get(key)

public fun setTag(key: String, value: String) {
if (tags == null) {
tags = HashMap()
}
tags!![key] = value
}

public fun addBreadcrumb(breadcrumb: Breadcrumb) {
if (breadcrumbs == null) {
breadcrumbs = mutableListOf()
}
breadcrumbs?.add(breadcrumb)
}

public fun setExtra(key: String, value: Any) {
if (extra == null) {
extra = HashMap()
}
extra!![key] = value
}

public fun removeExtra(key: String) {
extra?.remove(key)
}

public fun getExtra(key: String): Any? = extra?.get(key)

public fun addBreadcrumb(message: String?) {
addBreadcrumb(Breadcrumb(message = message))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.sentry.kotlin.multiplatform

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

public expect class SentryEvent : SentryBaseEvent {
public var message: Message?
public var logger: String?
public var level: SentryLevel?
public var fingerprint: List<String>?
public fun isCrashed(): Boolean
public fun isError(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public open class SentryOptions {
/** Hook that is triggered before a breadcrumb is sent to Sentry */
public var beforeBreadcrumb: ((Breadcrumb) -> Breadcrumb?)? = null

/** Hook that is triggered before an event is sent to Sentry */
public var beforeSend: ((SentryEvent) -> SentryEvent?)? = null

/** Information about the Sentry SDK that generated this event. */
public var sdk: SdkVersion? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.sentry.kotlin.multiplatform.protocol

public data class Message(
public var message: String? = null,
public var params: List<String>? = null,
public var formatted: String? = null
)

0 comments on commit 1066447

Please sign in to comment.