Skip to content

Commit

Permalink
Merge pull request #395 from Infomaniak/upload-sentry-log
Browse files Browse the repository at this point in the history
add more sentry log in uploadWorker and remove useless logs
  • Loading branch information
JorisBodin authored Dec 13, 2021
2 parents 1702911 + 5093356 commit 764b04b
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
*/
package com.infomaniak.drive.data.api

import android.util.Log
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryLevel
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.internal.http.CallServerInterceptor
Expand Down Expand Up @@ -57,12 +53,6 @@ class ProgressRequestBody(
}

if (isCalledByCallServerInterceptor) {
Log.d("ProgressRequestBody", "writeTo > called by server")
Sentry.addBreadcrumb(Breadcrumb().apply {
category = "Upload"
message = "writeTo > called by server"
level = SentryLevel.INFO
})
val progressOutputStream = ProgressOutputStream(sink.outputStream(), onProgress, contentLength())
val progressSink: BufferedSink = progressOutputStream.sink().buffer()
requestBody.writeTo(progressSink)
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class UploadTask(
val restartUpload = uploadedChunks?.let { needToResetUpload(it) } ?: false

Sentry.addBreadcrumb(Breadcrumb().apply {
category = "Upload"
message = "start with $totalChunks chunks and $uploadedChunks uploadedChunks"
category = UploadWorker.BREADCRUMB_TAG
message = "start ${uploadFile.fileName} with $totalChunks chunks and $uploadedChunks uploadedChunks"
level = SentryLevel.INFO
})

Expand Down Expand Up @@ -173,13 +173,6 @@ class UploadTask(
val uploadRequestBody = ProgressRequestBody(data.toRequestBody()) { currentBytes, bytesWritten, contentLength ->
launch {
progressMutex.withLock {
if (bytesWritten == contentLength) {
Sentry.addBreadcrumb(Breadcrumb().apply {
category = "Upload"
message = "$bytesWritten bytes were written"
level = SentryLevel.INFO
})
}
updateProgress(currentBytes, bytesWritten, contentLength, url)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import com.infomaniak.drive.utils.SyncUtils.syncImmediately
import com.infomaniak.lib.core.utils.ApiController
import com.infomaniak.lib.core.utils.hasPermissions
import com.infomaniak.lib.core.utils.isNetworkException
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.*
Expand Down Expand Up @@ -168,6 +169,11 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
}
} finally {
applicationContext.cancelNotification(NotificationUtils.CURRENT_UPLOAD_ID)
Sentry.addBreadcrumb(Breadcrumb().apply {
category = BREADCRUMB_TAG
message = "finish with $uploadedCount files uploaded"
level = SentryLevel.INFO
})
}
}

Expand Down Expand Up @@ -384,6 +390,7 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
companion object {
const val TAG = "upload_worker"
const val PERIODIC_TAG = "upload_worker_periodic"
const val BREADCRUMB_TAG = "Upload"

const val FILENAME = "filename"
const val PROGRESS = "progress"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/infomaniak/drive/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.infomaniak.drive.data.models.AppSettings
import com.infomaniak.drive.data.models.UISettings
import com.infomaniak.drive.data.models.UploadFile
import com.infomaniak.drive.data.services.DownloadReceiver
import com.infomaniak.drive.data.services.UploadWorker
import com.infomaniak.drive.launchInAppReview
import com.infomaniak.drive.utils.*
import com.infomaniak.drive.utils.SyncUtils.launchAllUpload
Expand Down Expand Up @@ -113,6 +114,11 @@ class MainActivity : BaseActivity() {

intent?.getIntExtra(INTENT_SHOW_PROGRESS, 0)?.let { folderId ->
if (folderId > 0) {
Sentry.addBreadcrumb(Breadcrumb().apply {
category = UploadWorker.BREADCRUMB_TAG
message = "Upload notification has been clicked"
level = SentryLevel.INFO
})
navController.navigate(R.id.fileListFragment)
mainViewModel.intentShowProgressByFolderId.value = folderId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
Expand Down Expand Up @@ -673,8 +672,11 @@ open class FileListFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
}

private fun setupDisplayMode(isListMode: Boolean) {
val navController = findNavController()
fileRecyclerView.layoutManager =
if (isListMode) LinearLayoutManager(requireContext()) else GridLayoutManager(requireContext(), 2)
if (isListMode) SentryLinearLayoutManager(navController, requireContext())
else SentryGridLayoutManager(navController, requireContext(), 2)

toggleDisplayButton.icon = ContextCompat.getDrawable(
requireContext(),
if (isListMode) R.drawable.ic_list else R.drawable.ic_grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ class UploadInProgressFragment : FileListFragment() {
if (isPendingFolders()) UploadFile.deleteAll(null)
else UploadFile.deleteAll(it)

fileAdapter.setFiles(arrayListOf())
fileRecyclerView.post {
fileAdapter.setFiles(arrayListOf())
}

needPopBackStack = UploadFile.getCurrentUserPendingUploadsCount(it) == 0
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Infomaniak kDrive - Android
* Copyright (C) 2021 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.drive.utils

import android.content.Context
import androidx.navigation.NavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.infomaniak.drive.utils.SentryLinearLayoutManager.Companion.layoutManagerSentryLog

/**
* TODO Temp fix
*/
class SentryGridLayoutManager(private val navController: NavController, context: Context?, spanCount: Int) :
GridLayoutManager(context, spanCount) {

override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
try {
super.onLayoutChildren(recycler, state)
} catch (exception: IndexOutOfBoundsException) {
layoutManagerSentryLog(navController, exception)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Infomaniak kDrive - Android
* Copyright (C) 2021 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.drive.utils

import android.content.Context
import androidx.navigation.NavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.sentry.Sentry
import io.sentry.SentryLevel

/**
* TODO Temp fix
*/
class SentryLinearLayoutManager(private val navController: NavController, context: Context?) : LinearLayoutManager(context) {

override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
try {
super.onLayoutChildren(recycler, state)
} catch (exception: IndexOutOfBoundsException) {
layoutManagerSentryLog(navController, exception)
}
}

companion object {
fun layoutManagerSentryLog(navController: NavController, exception: IndexOutOfBoundsException) {
exception.printStackTrace()
Sentry.withScope { scope ->
navController.currentDestination?.displayName?.let { name ->
scope.setExtra("navigation", name)
}
scope.level = SentryLevel.WARNING
scope.setExtra("message", "Data modified in different thread")
Sentry.captureException(exception)
}
}
}
}

0 comments on commit 764b04b

Please sign in to comment.