You need to enable JavaScript to run this app.
导航
网络请求使用的不是okhttp,是否可以上报网络监控数据到平台
最近更新时间:2024.01.08 16:31:19首次发布时间:2022.08.09 16:03:00

APMInsight支持Okhttp自动插桩网络监控,其他的网络库做网络监控,支持自己采集网络监控数据,通过APMInsight上报接口上报到服务器展示。下面是网络监控上报接口和详细的网络监控数据结构和字段含义说明。

上报接口

//上报的网络监控数据,具体数据结构和字段看下面的json结构
JSONObject network=new JSONObject();
ApmInsightAgent.monitorNetWork(network);

上报数据结构和字段说明

目标:监控网络成功率和全链路耗时

"1" : "未知错误",
"2" : "连接超时",
"3" : "SOCKET超时",
"4" : "IO出错",
"5" : "SOCKET出错",
"6" : "对方关闭了连接",
"7" : "绑定端口出错",
"8" : "连接出现异常",
"9" : "no route to host",
"10" : "端口不可用",
"11" : "未知主机,域名解析失败",
"12" : "连接被重置",
"13" : "连接被拒绝",
"14" : "服务器不可达,没找到到达服务器的路由节点",
"15" : "网络不可达",
"16" : "地址不可用",
"17" : "地址已经被使用",
"18" : "HTTP无响应",
"19" : "客户端协议出错",
"20" : "文件过大",
"21" : "跳转次数过多(一般发生在循环跳转)",
"31" : "未知客户端错误",
"32" : "磁盘空间不足",
"33" : "文件或目录不存在",
"34" : "磁盘配额不足",
"35" : "EROFS:",
"36" : "权限被拒绝",
"37" : "IO错误",
"38" : "json_exception",
"39" : "security_exception",
"40" : "没有网络",
"41" : "网络取消"

如果上报其他错误码,会以未知错误【错误码】 的方式在列表展示。

测试代码

确认平台配置了采样,端上采样命中,自定义设置 > 参数设置 > 网络分析。

JSONObject network= null;
try {
    network = new JSONObject("{\n" +
            "    \"data_type\":3,\n" +
            "    \"uri\":\"http://mock-api.com/Rz3yJMnM.mock/get\",\n" +
            "    \"timestamp\":1618901847169,\n" +
            "    \"ip\":\"10.79.136.68:8888\",\n" +
            "    \"duration\":539,\n" +
            "    \"status\":200\n" +
            "}");
    JSONObject log=new JSONObject("{\"dns\":{\"address_list\":[\"10.79.136.68\"]},\"socket\":{\"remote\":\"10.79.136.68:8888\",\"remote_host\":\"10.79.136.68\",\"remote_port\":\"8888\",\"socket_reused\":false},\"response\":{\"code\":200,\"sent_bytes\":132,\"received_bytes\":411,\"via_proxy\":true,\"network_accessed\":true},\"timing\":{\"request\":{\"duration\":539,\"request_sent_time\":1618901847302,\"response_recv_time\":1618901847304,\"start_time\":1618901847169},\"detailed_duration\":{\"ttfb\":1,\"dns\":0,\"tcp\":0,\"ssl\":0,\"send\":1,\"header_recv\":394,\"body_recv\":5}},\"base\":{\"method\":\"GET\",\"url\":\"http:\\/\\/mock-api.com\\/Rz3yJMnM.mock\\/get\"},\"error\":{\"error_code\":0},\"other\":{\"libcore\":\"okhttp\",\"version\":\"\",\"is_main_process\":true}}");
    network.put("request_log",log.toString());
} catch (JSONException e) {
    e.printStackTrace();
}
ApmInsightAgent.monitorNetWork(network);

过滤logcat日志关键字:ApmInsight 可以看到如下日志。

2021-04-20 18:52:29.637 31667-31667/com.example.apm.insight.demo D/ApmInsight: Receive:NetData

网络状态字段说明

上报日志中存在的网络日志字段。

"network_type": 5,        
"network_type_code": 13,

字段

说明

network_type

网络类型。

  • 0:NONE
  • 1:MOBILE
  • 2:MOBILE_2G
  • 3:MOBILE_3G
  • 4:WIFI
  • 5:MOBILE_4G

network_type_code

网络状态码。

/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = TelephonyProtoEnums.NETWORK_TYPE_UNKNOWN; // = 0.
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = TelephonyProtoEnums.NETWORK_TYPE_GPRS; // = 1.
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = TelephonyProtoEnums.NETWORK_TYPE_EDGE; // = 2.
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = TelephonyProtoEnums.NETWORK_TYPE_UMTS; // = 3.
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = TelephonyProtoEnums.NETWORK_TYPE_CDMA; // = 4.
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = TelephonyProtoEnums.NETWORK_TYPE_EVDO_0; // = 5.
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = TelephonyProtoEnums.NETWORK_TYPE_EVDO_A; // = 6.
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = TelephonyProtoEnums.NETWORK_TYPE_1XRTT; // = 7.
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = TelephonyProtoEnums.NETWORK_TYPE_HSDPA; // = 8.
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = TelephonyProtoEnums.NETWORK_TYPE_HSUPA; // = 9.
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = TelephonyProtoEnums.NETWORK_TYPE_HSPA; // = 10.
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = TelephonyProtoEnums.NETWORK_TYPE_IDEN; // = 11.
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = TelephonyProtoEnums.NETWORK_TYPE_EVDO_B; // = 12.
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = TelephonyProtoEnums.NETWORK_TYPE_LTE; // = 13.
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = TelephonyProtoEnums.NETWORK_TYPE_EHRPD; // = 14.
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = TelephonyProtoEnums.NETWORK_TYPE_HSPAP; // = 15.
/** Current network is GSM */
public static final int NETWORK_TYPE_GSM = TelephonyProtoEnums.NETWORK_TYPE_GSM; // = 16.
/** Current network is TD_SCDMA */
public static final int NETWORK_TYPE_TD_SCDMA =
        TelephonyProtoEnums.NETWORK_TYPE_TD_SCDMA; // = 17.
/** Current network is IWLAN */
public static final int NETWORK_TYPE_IWLAN = TelephonyProtoEnums.NETWORK_TYPE_IWLAN; // = 18.
/** Current network is LTE_CA {@hide} */
public static final int NETWORK_TYPE_LTE_CA = TelephonyProtoEnums.NETWORK_TYPE_LTE_CA; // = 19.
/**
* Current network is NR (New Radio) 5G.
* This will only be returned for 5G SA.
* For 5G NSA, the network type will be {@link #NETWORK_TYPE_LTE}.
*/
public static final int NETWORK_TYPE_NR = TelephonyProtoEnums.NETWORK_TYPE_NR; // 20.


//没有网络默认值
public static final int NET_DEFAULT = -10000;