Repository navigation
Expand file tree
/
Copy pathSentryBuddy.kt
More file actions
211 lines (184 loc) · 7.12 KB
/
Copy pathSentryBuddy.kt
File metadata and controls
211 lines (184 loc) · 7.12 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package io.sentry.android.buddy
import android.app.Application
import io.sentry.Sentry
import io.sentry.SentryOptions
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.TestOnly
@ApiStatus.Experimental
public object SentryBuddy {
private val lock: Any = Any()
private var recorder: BuddyRecorder? = null
private var lifecycleCallbacks: BuddyActivityLifecycleCallbacks? = null
private var installedApplication: Application? = null
private var previousBeforeSend: SentryOptions.BeforeSendCallback? = null
private var buddyBeforeSend: SentryOptions.BeforeSendCallback? = null
private var previousBeforeSendTransaction: SentryOptions.BeforeSendTransactionCallback? = null
private var buddyBeforeSendTransaction: SentryOptions.BeforeSendTransactionCallback? = null
private var previousBeforeBreadcrumb: SentryOptions.BeforeBreadcrumbCallback? = null
private var buddyBeforeBreadcrumb: SentryOptions.BeforeBreadcrumbCallback? = null
private var previousTracesSampler: SentryOptions.TracesSamplerCallback? = null
private var buddyTracesSampler: SentryOptions.TracesSamplerCallback? = null
@JvmStatic
public fun install(application: Application) {
install(application, SentryBuddyOptions())
}
@JvmStatic
public fun install(application: Application, configure: SentryBuddyOptions.() -> Unit) {
install(application, SentryBuddyOptions().apply(configure))
}
@JvmStatic
public fun install(application: Application, options: SentryBuddyOptions) {
synchronized(lock) {
if (!options.enabled) {
uninstallLocked()
return
}
if (installedApplication === application && recorder != null) {
lifecycleCallbacks?.updateOverlay(options)
return
}
uninstallLocked()
val sentryFacade = RealBuddySentryFacade()
val newRecorder =
BuddyRecorder(
metadataProvider = AndroidBuddyMetadataProvider(application, sentryFacade),
sentryFacade = sentryFacade,
)
val callbacks = BuddyActivityLifecycleCallbacks(newRecorder, overlayManager(options))
application.registerActivityLifecycleCallbacks(callbacks)
installEventObserver(newRecorder)
installBreadcrumbObserver(newRecorder)
installTransactionObserver(newRecorder)
installTracesSampler(newRecorder)
recorder = newRecorder
lifecycleCallbacks = callbacks
installedApplication = application
}
}
@JvmStatic
public fun startRecording(intent: BuddyFlowIntent) {
val recorder = requireInstalled()
recorder.start(intent)
lifecycleCallbacks?.recordCurrentScreen()
lifecycleCallbacks?.recordingEvent("Flow recording started")
}
@JvmStatic
public fun recordStep(name: String) {
recordStep(name, emptyMap())
}
@JvmStatic
public fun recordStep(name: String, data: Map<String, Any?>) {
requireInstalled().recordStep(name, data)
lifecycleCallbacks?.recordingEvent("Step: $name")
}
@JvmStatic
public fun stopRecording(): BuddyFlowRecording {
return requireInstalled().stop()
}
internal fun liveFeedSnapshot(): BuddyLiveFeed = requireInstalled().liveFeedSnapshot()
internal fun markLiveFeedSeen(): BuddyLiveFeed = requireInstalled().markLiveFeedSeen()
internal fun addLiveFeedListener(listener: (BuddyLiveFeed) -> Unit): () -> Unit =
requireInstalled().addLiveFeedListener(listener)
private fun requireInstalled(): BuddyRecorder {
return checkNotNull(recorder) { "SentryBuddy.install(application) must be called first." }
}
private fun uninstallLocked() {
restoreTracesSampler()
restoreTransactionObserver()
restoreBreadcrumbObserver()
restoreEventObserver()
lifecycleCallbacks?.let { callbacks ->
callbacks.detachAll()
installedApplication?.unregisterActivityLifecycleCallbacks(callbacks)
}
recorder = null
lifecycleCallbacks = null
installedApplication = null
}
private fun installTransactionObserver(recorder: BuddyRecorder) {
val sentryOptions = Sentry.getCurrentScopes().options
val original = sentryOptions.beforeSendTransaction
val observer = RealBuddySentryFacade.transactionObserver(recorder, original)
previousBeforeSendTransaction = original
buddyBeforeSendTransaction = observer
sentryOptions.beforeSendTransaction = observer
}
private fun installEventObserver(recorder: BuddyRecorder) {
val sentryOptions = Sentry.getCurrentScopes().options
val original = sentryOptions.beforeSend
val observer = RealBuddySentryFacade.eventObserver(recorder, original)
previousBeforeSend = original
buddyBeforeSend = observer
sentryOptions.beforeSend = observer
}
private fun installBreadcrumbObserver(recorder: BuddyRecorder) {
val sentryOptions = Sentry.getCurrentScopes().options
val original = sentryOptions.beforeBreadcrumb
val observer = RealBuddySentryFacade.breadcrumbObserver(recorder, original)
previousBeforeBreadcrumb = original
buddyBeforeBreadcrumb = observer
sentryOptions.beforeBreadcrumb = observer
}
private fun installTracesSampler(recorder: BuddyRecorder) {
val sentryOptions = Sentry.getCurrentScopes().options
val original = sentryOptions.tracesSampler
val sampler = RealBuddySentryFacade.tracesSampler(recorder, original)
previousTracesSampler = original
buddyTracesSampler = sampler
sentryOptions.tracesSampler = sampler
}
private fun restoreTransactionObserver() {
val observer = buddyBeforeSendTransaction ?: return
val sentryOptions = Sentry.getCurrentScopes().options
if (sentryOptions.beforeSendTransaction === observer) {
sentryOptions.beforeSendTransaction = previousBeforeSendTransaction
}
previousBeforeSendTransaction = null
buddyBeforeSendTransaction = null
}
private fun restoreEventObserver() {
val observer = buddyBeforeSend ?: return
val sentryOptions = Sentry.getCurrentScopes().options
if (sentryOptions.beforeSend === observer) {
sentryOptions.beforeSend = previousBeforeSend
}
previousBeforeSend = null
buddyBeforeSend = null
}
private fun restoreBreadcrumbObserver() {
val observer = buddyBeforeBreadcrumb ?: return
val sentryOptions = Sentry.getCurrentScopes().options
if (sentryOptions.beforeBreadcrumb === observer) {
sentryOptions.beforeBreadcrumb = previousBeforeBreadcrumb
}
previousBeforeBreadcrumb = null
buddyBeforeBreadcrumb = null
}
private fun restoreTracesSampler() {
val sampler = buddyTracesSampler ?: return
val sentryOptions = Sentry.getCurrentScopes().options
if (sentryOptions.tracesSampler === sampler) {
sentryOptions.tracesSampler = previousTracesSampler
}
previousTracesSampler = null
buddyTracesSampler = null
}
private fun overlayManager(options: SentryBuddyOptions): BuddyOverlayManager? {
if (!options.showOverlay) {
return null
}
return BuddyOverlayManager(
SentryBuddySessionController(
flowAnalysesApi = options.flowAnalysesApi,
openUrlApi = options.openUrlApi,
)
)
.also { it.updateOptions(options) }
}
@TestOnly
internal fun resetForTest() {
synchronized(lock) {
uninstallLocked()
}
}
}