Commit 04541bdc by YKai

feat:接口返回错误日志埋点补充参数

parent 018badf8
package com.ydl.ydlnet.builder.api
import com.google.gson.Gson
import com.ydl.ydlnet.YDLHttpUtils.Companion.obtainApi
import com.ydl.ydlnet.builder.bean.ApiErrorActionBean
import com.ydl.ydlnet.builder.bean.BaseAPIResponse
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.RequestBody
/**
* Created by Ykai on 2021/3/15.
*/
class ApiUtil{
companion object {
/**
* 行为数据埋点统计
*/
fun actionDataCount(actionDataParams: ApiErrorActionBean): Observable<BaseAPIResponse<String>> {
val body = RequestBody.create(
MediaType.parse("application/json; charset=utf-8"),
Gson().toJson(actionDataParams)
)
return obtainApi(NetApi::class.java).actionDataCount(body)
}
}
}
\ No newline at end of file
package com.ydl.ydlnet.builder.api
import com.ydl.ydlnet.builder.bean.BaseAPIResponse
import io.reactivex.Observable
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
/**
* Created by Ykai on 2021/3/15.
*/
interface NetApi {
/**
* 行为动作埋点统计
*/
@Headers("Domain-Name:JAVA_BASE_URL")
@POST("data/bigdata/maidian/writeMaiDianData")
fun actionDataCount(@Body body: RequestBody): Observable<BaseAPIResponse<String>>
}
\ No newline at end of file
......@@ -2,13 +2,10 @@ package com.ydl.ydlnet.builder.interceptor.log;
import android.support.annotation.Nullable;
import com.ydl.ydlnet.utils.ApiErrorCountUtils;
import com.ydl.ydlnet.utils.CharacterHandler;
import com.ydl.ydlnet.utils.NetLogUtils;
import com.ydl.ydlnet.utils.UrlEncoderUtils;
import com.ydl.ydlnet.utils.ZipHelper;
import com.ydl.ydlnet.utils.log.AliYunLogConfig;
import com.ydl.ydlnet.utils.log.AliYunLogHelper;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
......@@ -90,21 +87,6 @@ public class RequestLogInterceptor implements Interceptor {
mPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1),
isSuccessful, code, header, segmentList, message, url);
}
} else {
int code = originalResponse.code();
// 接口返回错误的情况下,埋点告诉服务器原因
if (code != 200) {
String params = "";
Request rq = originalResponse.request();
if (rq.method().equals("GET")) {
params = rq.url().query();
} else if (rq.method().equals("POST")) {
params = parseParams(rq);
}
String message = originalResponse.message();
ApiErrorCountUtils.Companion.baiDuCount("pardId-ydl_user_error_business", "error_log", params, message);
AliYunLogHelper.Companion.getInstance().sendLog(AliYunLogConfig.API, "error_log--- params:" + params + " ---- message:" + message);
}
}
return originalResponse;
}
......
package com.ydl.ydlnet.utils
import android.annotation.SuppressLint
import android.util.Log
import com.ydl.ydlnet.builder.api.ApiUtil
import com.ydl.ydlnet.builder.bean.ApiErrorActionBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
/**
* Created by Ykai on 2021/3/15.
*/
class ApiErrorCountUtils {
companion object {
private const val TAG: String = "ActionCountUtils"
/**
* 接口返回错误埋点调用该方法
* @param partId pardId-ydl_user_error_business
* @param position error_log
* @param sign1 访问接口对应的参数
* @param sign2 接口返回的错误信息
*/
fun baiDuCount(
partId: String,
position: String,
sign1: String,
sign2: String
) {
val actionDataParams = ApiErrorActionBean.Builder()
actionDataParams.partId = partId
actionDataParams.position = position
actionDataParams.sign1 = sign1
actionDataParams.sign2 = sign2
//请求
request(actionDataParams.build())
}
/**
* 传入ActionDataParams参数,访问接口
*/
@SuppressLint("CheckResult")
private fun request(actionDataBean: ApiErrorActionBean) {
try {
ApiUtil.actionDataCount(actionDataBean)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Log.i(TAG, it.data)
}) {
Log.i(TAG, it.toString())
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
\ No newline at end of file
......@@ -16,9 +16,13 @@ import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.api.ApiRequestUtil
import com.ydl.ydlcommon.modular.ModularServiceManager
import com.ydl.ydlcommon.utils.YDLCacheUtils
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
import com.ydl.ydlcommon.utils.log.AliYunRichLogsHelper
import com.ydl.ydlnet.builder.config.OkHttpConfig
import com.ydl.ydlnet.builder.factory.ApiFactory
import com.ydl.ydlnet.builder.interceptor.log.RequestHandler
import com.ydl.ydlnet.builder.interceptor.log.RequestLogInterceptor
import com.ydl.ydlnet.utils.log.AliYunLogConfig
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.RxAppTool
import com.yidianling.common.tools.RxDeviceTool
......@@ -118,6 +122,7 @@ class HttpConfig {
var ENCRYPTION_APP_KEY = "" // 接口加密appKey
var ENCRYPTION_APP_SECRET = "" // 接口加密appSecret
var isEncryption = false
var appDebug:Boolean=false
fun getInstance(): HttpConfig {
return Holder.INSTANCE
......@@ -127,7 +132,7 @@ class HttpConfig {
fun createOkHttp(): OkHttpClient {
val globalConfig = BaseApp.instance.getGlobalConfig()
val context = BaseApp.getApp()
val appDebug = globalConfig.appDebug
appDebug = globalConfig.appDebug
val appName = globalConfig.appName
//获取证书
......@@ -144,7 +149,8 @@ class HttpConfig {
.setInterceptor(
commonParams(),
requestHead(appName),
addEncryptionHeaderParamsInterceptor()
addEncryptionHeaderParamsInterceptor(),
responseErrorInterceptor()
)
.setRequestHandler(getRequestHandler())
.setReadTimeout(15)
......@@ -170,7 +176,7 @@ class HttpConfig {
path += it.request().url().encodedPath().substring(5)
}
else -> {
path+it.request().url().encodedPath()
path + it.request().url().encodedPath()
}
}
val sign = EncryptionParams.getSign(path, timestamp)
......@@ -180,12 +186,39 @@ class HttpConfig {
.addHeader("timestamp", timestamp)
.build()
it.proceed(request)
}else{
} else {
it.proceed(it.request())
}
}
}
/**
* 接口返回错误日志埋点
*/
private fun responseErrorInterceptor(): Interceptor {
return Interceptor {
val request: Request = it.request()
val originalResponse: Response = it.proceed(request)
if (!appDebug){
val code = originalResponse.code()
// 接口返回错误的情况下,埋点告诉服务器原因
if (code != 200) {
var params = ""
if (request.method() == "GET") {
params = request.url().query().toString()
} else if (request.method() == "POST") {
params = RequestLogInterceptor.parseParams(request)
}
val message = originalResponse.message()
val api = request.url().encodedPath()
ActionCountUtils.baiDuCount("ydl_user_error_business","error_log",api,params,message)
AliYunRichLogsHelper.getInstance().sendRichLog(AliYunLogConfig.API, "error_log---api:$api---params:$params ---- message:$message")
}
}
originalResponse
}
}
private fun getRequestHandler(): RequestHandler {
return object : RequestHandler {
override fun onHttpResultResponse(
......
......@@ -134,10 +134,10 @@ class ActionCountUtils {
actionDataParams.appVersion(RxDeviceTool.getAppVersionName(BaseApp.getApp()))
actionDataParams.appId(appId)
actionDataParams.api(api)
actionDataParams.manufacturer(android.os.Build.BRAND.trim().toUpperCase())
actionDataParams.model(android.os.Build.MODEL)
actionDataParams.manufacturer(Build.BRAND.trim().toUpperCase())
actionDataParams.model(Build.MODEL)
actionDataParams.os(BIConstants.OS)
actionDataParams.osVersion(android.os.Build.VERSION.RELEASE)
actionDataParams.osVersion(Build.VERSION.RELEASE)
actionDataParams.wifi(if (isWifi()) 0 else 1)
var deviceId = ""
/*
......@@ -243,10 +243,10 @@ class ActionCountUtils {
actionDataParams.appVersion(RxDeviceTool.getAppVersionName(BaseApp.getApp()))
actionDataParams.appId(appId)
actionDataParams.api(api)
actionDataParams.manufacturer(android.os.Build.BRAND.trim().toUpperCase())
actionDataParams.model(android.os.Build.MODEL)
actionDataParams.manufacturer(Build.BRAND.trim().toUpperCase())
actionDataParams.model(Build.MODEL)
actionDataParams.os(BIConstants.OS)
actionDataParams.osVersion(android.os.Build.VERSION.RELEASE)
actionDataParams.osVersion(Build.VERSION.RELEASE)
actionDataParams.wifi(if (isWifi()) 0 else 1)
actionDataParams.screenWidth(RxDeviceTool.getScreenWidth(RxTool.getContext()!!))
actionDataParams.screenHeight(RxDeviceTool.getScreenHeight(RxTool.getContext()!!))
......@@ -287,7 +287,7 @@ class ActionCountUtils {
baiduActionDataParams.androidId(DeviceTool.getAndroidID())//MD5加密的androidId
baiduActionDataParams.ip("")
baiduActionDataParams.ipv6("")//给服务端传IPV6地址DeviceTool.getLocalIpV6()
baiduActionDataParams.tp(android.os.Build.MODEL)
baiduActionDataParams.tp(Build.MODEL)
baiduActionDataParams.network(DeviceTool.getNetworkState(RxTool.getContext()))
baiduActionDataParams.convertParam("")
baiduActionDataParams.uid(uid)
......@@ -316,8 +316,52 @@ class ActionCountUtils {
}
/**
* 接口返回错误埋点调用该方法
* @param partId pardId-ydl_user_error_business
* @param position error_log
* @param sign1 访问接口对应的参数
* @param sign2 接口返回的错误信息
*/
fun baiDuCount(
partId: String,
position: String,
api: String,
sign1: String,
sign2: String
) {
val actionDataParams = ActionDataBean.Builder()
actionDataParams.partId = partId
actionDataParams.position = position
actionDataParams.sign1 = sign1
actionDataParams.sign2 = sign2
actionDataParams.api = api
actionDataParams.appVersion(RxDeviceTool.getAppVersionName(BaseApp.getApp()))
val deviceId = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
val topTaskActivity = ActivityManager.getInstance().getTopTaskActivity()
//获取imei
Utils.getIMEI(topTaskActivity)
} else {
//android Q及以上取oaid
DeviceIDHelper.getInstance().deviceId
}
actionDataParams.deviceId=deviceId
actionDataParams.time(System.currentTimeMillis())
actionDataParams.appId(appId)
actionDataParams.manufacturer(Build.BRAND.trim().toUpperCase())
actionDataParams.model(Build.MODEL)
actionDataParams.os(BIConstants.OS)
actionDataParams.osVersion(Build.VERSION.RELEASE)
actionDataParams.wifi(if (isWifi()) 0 else 1)
actionDataParams.uid = ModularServiceManager.getPlatformUserService()!!.getUser()?.userId
//请求
request(actionDataParams.build())
}
/**
* 传入ActionDataParams参数,访问接口
*/
@SuppressLint("CheckResult")
fun request(actionDataBean: ActionDataBean) {
try {
ApiRequestUtil.actionDataCount(actionDataBean)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment