-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentryFeature.kt
71 lines (58 loc) · 2.13 KB
/
sentryFeature.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package se.zensum.ktorSentry
import io.ktor.application.ApplicationCall
import io.ktor.application.ApplicationCallPipeline
import io.ktor.application.ApplicationFeature
import io.ktor.pipeline.PipelineContext
import io.ktor.util.AttributeKey
import io.sentry.Sentry
import io.sentry.SentryClient
private typealias CustomizeFn = SentryClient.() -> Unit
private inline fun <T> sentryWrap(fn: () -> T) = try {
fn()
} catch (ex: Exception) {
Sentry.capture(ex)
throw ex
} finally {
Sentry.clearContext()
}
class SentryFeature private constructor() {
class Configuration {
var dsn: String? = null
var appEnv: String? = null
var serverName: String? = null
var customizeFn: CustomizeFn? = null
fun customize(fn: CustomizeFn) { customizeFn = fn }
internal fun initClient() {
// If the user has specified a DSN
if (dsn != null) {
Sentry.init(dsn)
}
val client = Sentry.getStoredClient()
client.serverName = this.serverName
if(client.serverName == null) {
val user: String? = System.getProperty("user.name")
val hostname: String? = user?.let { "$it@" } + hostname()
client.serverName = hostname
}
appEnv?.let { env ->
client.environment = env
}
customizeFn?.invoke(Sentry.getStoredClient())
}
}
suspend fun intercept(context: PipelineContext<Unit, ApplicationCall>) {
sentryWrap { context.proceed() }
}
companion object Feature : ApplicationFeature<ApplicationCallPipeline, Configuration, SentryFeature> {
override val key = AttributeKey<SentryFeature>("SentryFeature")
override fun install(pipeline: ApplicationCallPipeline, configure: Configuration.() -> Unit): SentryFeature {
val cfg = Configuration().apply(configure)
cfg.initClient()
val result = SentryFeature()
pipeline.intercept(ApplicationCallPipeline.Call) {
result.intercept(this)
}
return result
}
}
}