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

Fix memory leak issue in SentryFlutterPlugin #1588

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Fixing memory leak issue in SentryFlutterPlugin (Android Plugin) ([#1588](https://github.com/getsentry/sentry-dart/pull/1588))

### Dependencies

- Bump Android SDK from v6.25.2 to v6.28.0 ([#1586](https://github.com/getsentry/sentry-dart/pull/1586))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.sentry.Breadcrumb
import io.sentry.DateUtils
import io.sentry.Hint
import io.sentry.HubAdapter
import io.sentry.Sentry
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.Sentry
import io.sentry.DateUtils
import io.sentry.SentryOptions
import io.sentry.android.core.ActivityFramesTracker
import io.sentry.android.core.AppStartState
import io.sentry.android.core.BuildConfig.VERSION_NAME
Expand All @@ -26,7 +28,6 @@ import io.sentry.protocol.DebugImage
import io.sentry.protocol.SdkVersion
import io.sentry.protocol.SentryId
import io.sentry.protocol.User
import io.sentry.protocol.Geo
import java.io.File
import java.lang.ref.WeakReference
import java.util.Locale
Expand All @@ -40,10 +41,6 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private var framesTracker: ActivityFramesTracker? = null
private var autoPerformanceTracingEnabled = false

private val flutterSdk = "sentry.dart.flutter"
private val androidSdk = "sentry.java.android.flutter"
private val nativeSdk = "sentry.native.android.flutter"

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
context = flutterPluginBinding.applicationContext
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "sentry_flutter")
Expand Down Expand Up @@ -127,9 +124,15 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
args.getIfNotNull<String>("environment") { options.environment = it }
args.getIfNotNull<String>("release") { options.release = it }
args.getIfNotNull<String>("dist") { options.dist = it }
args.getIfNotNull<Boolean>("enableAutoSessionTracking") { options.isEnableAutoSessionTracking = it }
args.getIfNotNull<Long>("autoSessionTrackingIntervalMillis") { options.sessionTrackingIntervalMillis = it }
args.getIfNotNull<Long>("anrTimeoutIntervalMillis") { options.anrTimeoutIntervalMillis = it }
args.getIfNotNull<Boolean>("enableAutoSessionTracking") {
options.isEnableAutoSessionTracking = it
}
args.getIfNotNull<Long>("autoSessionTrackingIntervalMillis") {
options.sessionTrackingIntervalMillis = it
}
args.getIfNotNull<Long>("anrTimeoutIntervalMillis") {
options.anrTimeoutIntervalMillis = it
}
args.getIfNotNull<Boolean>("attachThreads") { options.isAttachThreads = it }
args.getIfNotNull<Boolean>("attachStacktrace") { options.isAttachStacktrace = it }
args.getIfNotNull<Boolean>("enableAutoNativeBreadcrumbs") {
Expand Down Expand Up @@ -183,12 +186,7 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
options.sdkVersion = sdkVersion
options.sentryClientName = "$androidSdk/$VERSION_NAME"
options.nativeSdkName = nativeSdk

options.setBeforeSend { event, _ ->
setEventOriginTag(event)
addPackages(event, options.sdkVersion)
event
}
options.beforeSend = BeforeSendCallbackImpl(options.sdkVersion)

args.getIfNotNull<Int>("connectionTimeoutMillis") { options.connectionTimeoutMillis = it }
args.getIfNotNull<Int>("readTimeoutMillis") { options.readTimeoutMillis = it }
Expand Down Expand Up @@ -408,30 +406,50 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
result.success("")
}

private fun setEventOriginTag(event: SentryEvent) {
event.sdk?.let {
when (it.name) {
flutterSdk -> setEventEnvironmentTag(event, "flutter", "dart")
androidSdk -> setEventEnvironmentTag(event, environment = "java")
nativeSdk -> setEventEnvironmentTag(event, environment = "native")
else -> return
}
private class BeforeSendCallbackImpl(
private val sdkVersion: SdkVersion?
) : SentryOptions.BeforeSendCallback {
override fun execute(event: SentryEvent, hint: Hint): SentryEvent {
setEventOriginTag(event)
addPackages(event, sdkVersion)
return event
}
}

private fun setEventEnvironmentTag(event: SentryEvent, origin: String = "android", environment: String) {
event.setTag("event.origin", origin)
event.setTag("event.environment", environment)
}

private fun addPackages(event: SentryEvent, sdk: SdkVersion?) {
event.sdk?.let {
if (it.name == flutterSdk) {
sdk?.packageSet?.forEach { sentryPackage ->
it.addPackage(sentryPackage.name, sentryPackage.version)
companion object {

private const val flutterSdk = "sentry.dart.flutter"
private const val androidSdk = "sentry.java.android.flutter"
private const val nativeSdk = "sentry.native.android.flutter"
private fun setEventOriginTag(event: SentryEvent) {
event.sdk?.let {
when (it.name) {
flutterSdk -> setEventEnvironmentTag(event, "flutter", "dart")
androidSdk -> setEventEnvironmentTag(event, environment = "java")
nativeSdk -> setEventEnvironmentTag(event, environment = "native")
else -> return
}
sdk?.integrationSet?.forEach { integration ->
it.addIntegration(integration)
}
}

private fun setEventEnvironmentTag(
event: SentryEvent,
origin: String = "android",
environment: String
) {
event.setTag("event.origin", origin)
event.setTag("event.environment", environment)
}

private fun addPackages(event: SentryEvent, sdk: SdkVersion?) {
event.sdk?.let {
if (it.name == flutterSdk) {
sdk?.packageSet?.forEach { sentryPackage ->
it.addPackage(sentryPackage.name, sentryPackage.version)
}
sdk?.integrationSet?.forEach { integration ->
it.addIntegration(integration)
}
}
}
}
Expand Down
Loading