TP-68392 | Remove headers in metric event (#11125)

This commit is contained in:
Abhinav Gupta
2024-06-03 15:50:54 +05:30
committed by GitHub
parent 92e2d12396
commit 5c23f73e01
2 changed files with 27 additions and 13 deletions

View File

@@ -7,6 +7,10 @@
package com.navi.common.network
@Target(AnnotationTarget.FUNCTION) annotation class DisableRequestBodyLogging
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class EnableRequestBodyLogging
@Target(AnnotationTarget.FUNCTION) annotation class EnableResponseLogging
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class EnableResponseLogging

View File

@@ -12,7 +12,7 @@ import com.navi.analytics.utils.NaviTrackEvent
import com.navi.base.network.util.ApiConstants
import com.navi.common.extensions.safeSubstring
import com.navi.common.network.BaseHttpClient
import com.navi.common.network.DisableRequestBodyLogging
import com.navi.common.network.EnableRequestBodyLogging
import com.navi.common.network.EnableResponseLogging
import com.navi.common.network.handleException
import com.navi.common.network.logIOException
@@ -26,6 +26,7 @@ import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import okio.Buffer
class MetricInterceptor : Interceptor {
@@ -67,16 +68,9 @@ class MetricInterceptor : Interceptor {
0L
}
val shouldDisableRequestBodyLogging = request.getAnnotation<DisableRequestBodyLogging>()
val shouldEnableRequestBodyLogging = request.getAnnotation<EnableRequestBodyLogging>()
val shouldEnableResponseLogging = request.getAnnotation<EnableResponseLogging>()
val requestString = response.request.toString()
val requestBody =
requestString.safeSubstring(
0,
minOf(MAX_REQUEST_BODY_LOG_SIZE_LENGTH, requestString.length)
)
val map =
mutableMapOf(
Pair(Constants.API_ENDPOINT, endPoint.toString()),
@@ -86,10 +80,15 @@ class MetricInterceptor : Interceptor {
Pair(VERTICAL, request.headers[BaseHttpClient.X_TARGET].orEmpty())
)
if (shouldDisableRequestBodyLogging == null) {
if (shouldEnableRequestBodyLogging != null) {
val requestString = requestBodyToString(request)
val requestBody =
requestString.safeSubstring(
0,
minOf(MAX_REQUEST_BODY_LOG_SIZE_LENGTH, requestString.length)
)
map[Constants.REQUEST_BODY] = requestBody
}
if (shouldEnableResponseLogging != null) {
map[Constants.RESPONSE] = response.body.toString()
}
@@ -99,4 +98,15 @@ class MetricInterceptor : Interceptor {
eventValues = map
)
}
private fun requestBodyToString(request: Request): String {
return try {
Buffer().use { buffer ->
request.newBuilder().build().body?.writeTo(buffer)
buffer.readUtf8()
}
} catch (e: Exception) {
Constants.EMPTY
}
}
}