Skip to content

Commit

Permalink
feat: re-enabled sentry error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianEstrada committed Apr 22, 2024
1 parent d76b270 commit e51bbac
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ jobs:
java-version: '17'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v2
- name: Replace and print SENTRY_DSN
run: |
find . -name logback.xml -exec sed -i 's/\${SENTRY_DSN}/${{ secrets.SENTRY_DSN }}/g' {} \;
echo "Modified logback.xml:"
find . -name logback.xml -exec cat {} \;
- name: Build
env:
GHL_USERNAME: ${{ secrets.GHL_USERNAME }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@ import org.koin.core.annotation.Singleton

@Singleton
class AppConfigRepository {
fun getByKey(

fun get(
key: String
): AppConfigDAO? {
return AppConfigDAO.find {
AppConfigTable.key eq key
}.firstOrNull()
}

fun create(
key: String,
value: String?
): AppConfigDAO {
return AppConfigDAO.new {
this.key = key
this.value = value
}
}

fun upsert(
key: String,
value: String?
): AppConfigDAO {
val configuration = getByKey(key)
val configuration = get(key)

if (configuration != null) {
configuration.value = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ class AppConfigService(
private val appConfigRepository: AppConfigRepository
) {

fun getOrCreate(
key: String,
defaultValue: String
): AppConfigDAO {
return transaction {
appConfigRepository.get(key) ?: appConfigRepository.create(key, defaultValue)
}
}

fun getByKey(
key: String
): String? {
return transaction {
appConfigRepository.getByKey(key)
appConfigRepository.get(key)
}?.value
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.monta.ocpp.emulator.user

import io.sentry.Sentry
import io.sentry.protocol.User
import org.koin.core.annotation.Singleton
import org.slf4j.LoggerFactory

@Singleton
class AnalyticsHelper(
private val appUserService: AppUserService
) {

companion object {
@JvmStatic
private val logger = LoggerFactory.getLogger(AnalyticsHelper::class.java)
}

fun initSentry() {
try {
Sentry.setUser(
User().apply {
id = appUserService.getUserId()
}
)
} catch (e: Exception) {
logger.warn("[sentry] failed to set user", e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.monta.ocpp.emulator.user

import com.monta.ocpp.emulator.configuration.AppConfigService
import org.koin.core.annotation.Singleton
import java.util.UUID

@Singleton
class AppUserService(
private val appConfigService: AppConfigService
) {

companion object {
private const val USER_ID_KEY = "user_id"
}

fun getUserId(): String {
val userIdConfig = appConfigService.getOrCreate(
key = USER_ID_KEY,
defaultValue = UUID.randomUUID().toString()
)
return userIdConfig.value ?: UUID.randomUUID().toString()
}
}
5 changes: 5 additions & 0 deletions v16/src/jvmMain/kotlin/com/monta/ocpp/emulator/v16/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.monta.ocpp.emulator.v16
import androidx.compose.ui.window.application
import com.monta.ocpp.emulator.CommonKoinModule
import com.monta.ocpp.emulator.common.util.injectAnywhere
import com.monta.ocpp.emulator.user.AnalyticsHelper
import com.monta.ocpp.emulator.v16.service.ocpp.connection.ConnectionManager
import com.monta.ocpp.emulator.v16.view.MainWindow
import com.monta.ocpp.emulator.v16.view.interceptor.EditMessageWindow
Expand Down Expand Up @@ -35,6 +36,10 @@ fun main() {
}
})

val analyticsHelper by injectAnywhere<AnalyticsHelper>()

analyticsHelper.initSentry()

application {
SendMessageWindow()
EditMessageWindow()
Expand Down
3 changes: 2 additions & 1 deletion v16/src/jvmMain/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<options>
<environment>production</environment>
<release>${jpackage.app-version}</release>
<dsn>https://74aa6b85c94f14103d00617c9626a52e@o915528.ingest.us.sentry.io/4504402951864320</dsn>
<dsn>${SENTRY_DSN}</dsn>
</options>
<!-- Optionally change minimum Event level. Default for Events is ERROR -->
<minimumEventLevel>ERROR</minimumEventLevel>
Expand All @@ -42,6 +42,7 @@
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
<appender-ref ref="SENTRY"/>
</root>

</configuration>
13 changes: 13 additions & 0 deletions v201/src/jvmMain/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@
</encoder>
</appender>

<appender name="SENTRY" class="io.sentry.logback.SentryAppender">
<options>
<environment>production</environment>
<release>${jpackage.app-version}</release>
<dsn>${SENTRY_DSN}</dsn>
</options>
<!-- Optionally change minimum Event level. Default for Events is ERROR -->
<minimumEventLevel>ERROR</minimumEventLevel>
<!-- Optionally change minimum Breadcrumbs level. Default for Breadcrumbs is INFO -->
<minimumBreadcrumbLevel>TRACE</minimumBreadcrumbLevel>
</appender>

<logger name="com.monta" level="TRACE"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
<appender-ref ref="SENTRY"/>
</root>

</configuration>

0 comments on commit e51bbac

Please sign in to comment.