-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Technical Analytics: Milestone 3 - New App Health Metrics (#5320)
<!-- READ ME FIRST: Please fill in the explanation section below and check off every point from the Essential Checklist! --> ## Explanation <!-- - Explain what your PR does. If this PR fixes an existing bug, please include - "Fixes #bugnum:" in the explanation so that GitHub can auto-close the issue - when this PR is merged. --> When merged, this PR will; - Introduce the ability to log all network calls as well as failed network calls. - Introduce the ability to log all console errors logged via the `ConsoleLogger`. - Introduce the ability to log the time spent by a user with the app in the foreground. ## Essential Checklist <!-- Please tick the relevant boxes by putting an "x" in them. --> - [ ] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)). ## For UI-specific PRs only <!-- Delete these section if this PR does not include UI-related changes. --> If your PR includes UI-related changes, then: - Add screenshots for portrait/landscape for both a tablet & phone of the before & after UI changes - For the screenshots above, include both English and pseudo-localized (RTL) screenshots (see [RTL guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines)) - Add a video showing the full UX flow with a screen reader enabled (see [accessibility guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide)) - For PRs introducing new UI elements or color changes, both light and dark mode screenshots must be included - Add a screenshot demonstrating that you ran affected Espresso tests locally & that they're passing
- Loading branch information
Showing
36 changed files
with
1,315 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
data/src/main/java/org/oppia/android/data/backends/gae/NetworkLoggingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.oppia.android.data.backends.gae | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.launch | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import org.oppia.android.app.model.EventLog.RetrofitCallContext | ||
import org.oppia.android.app.model.EventLog.RetrofitCallFailedContext | ||
import org.oppia.android.util.threading.BackgroundDispatcher | ||
import java.io.IOException | ||
import java.lang.Exception | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
/** | ||
* Interceptor on top of Retrofit to log network requests and responses. | ||
*/ | ||
@Singleton | ||
class NetworkLoggingInterceptor @Inject constructor( | ||
@BackgroundDispatcher private val backgroundDispatcher: CoroutineDispatcher, | ||
) : Interceptor { | ||
private val _logNetworkCallFlow = MutableSharedFlow<RetrofitCallContext>() | ||
/** | ||
* A flow that emits a [RetrofitCallContext] when a network call is made. | ||
*/ | ||
val logNetworkCallFlow: SharedFlow<RetrofitCallContext> = _logNetworkCallFlow | ||
|
||
private val _logFailedNetworkCallFlow = MutableSharedFlow<RetrofitCallFailedContext>() | ||
|
||
/** | ||
* A flow that emits a [RetrofitCallFailedContext] when a network call fails. | ||
*/ | ||
val logFailedNetworkCallFlow: SharedFlow<RetrofitCallFailedContext> = _logFailedNetworkCallFlow | ||
|
||
@Throws(IOException::class) | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request() | ||
|
||
return try { | ||
val response = chain.proceed(request) | ||
|
||
val responseBody = response.body?.string() | ||
|
||
CoroutineScope(backgroundDispatcher).launch { | ||
_logNetworkCallFlow.emit( | ||
RetrofitCallContext.newBuilder() | ||
.setRequestUrl(request.url.toString()) | ||
.setHeaders(request.headers.toString()) | ||
.setResponseStatusCode(response.code) | ||
.setBody(responseBody ?: "") | ||
.build() | ||
) | ||
} | ||
|
||
if (!response.isSuccessful) { | ||
CoroutineScope(backgroundDispatcher).launch { | ||
_logFailedNetworkCallFlow.emit( | ||
RetrofitCallFailedContext.newBuilder() | ||
.setRequestUrl(request.url.toString()) | ||
.setHeaders(request.headers.toString()) | ||
.setResponseStatusCode(response.code) | ||
.setErrorMessage(responseBody ?: "") | ||
.build() | ||
) | ||
} | ||
} | ||
|
||
response | ||
} catch (exception: Exception) { | ||
CoroutineScope(backgroundDispatcher).launch { | ||
_logFailedNetworkCallFlow.emit( | ||
RetrofitCallFailedContext.newBuilder() | ||
.setRequestUrl(request.url.toString()) | ||
.setHeaders(request.headers.toString()) | ||
.setResponseStatusCode(0) | ||
.setErrorMessage(exception.toString()) | ||
.build() | ||
) | ||
} | ||
chain.proceed(request) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.