自定义接口监控

功能介绍

统计应用对某个外部接口(特别是网络类的接口,如连接、登陆、下载等)的调用情况。当开发者用到某个外部接口,可调用该函数将一些指标进行上报,MTA将统计出每个接口的调用情况,并在接口可用性发生变化时进行告警通知; 对于调用量很大的接口,也可以采样上报,云监控统计将根据sampling参数在展现页面进行数量的还原。

快速入口:

Android接口监控代码集成

(1)代码说明

    /**
     * 监控指定的接口(特别是网络类的接口,如连接、登陆、下载等)情况。
     * 客户开发过程中有很多接口,这些接口的是否正确调用,执行完成的耗时等信息都是和用户体验息息相关的,
     * 希望通过次数据帮助客户监控接口的健康度,一旦发生问题,也能帮助客户快速定位原因。
     *
     * @param ctx     Context上下文对象
     * @param monitor 监控对象及内容
     */
    public static void reportAppMonitorStat(Context ctx, StatAppMonitor monitor);

monitor 监控对象,需要根据接口情况设置接口名称、耗时、返回值类型、返回码、请求包大小、响应包大小和采样率等信息。

(2)调用位置:

被监控的接口

StatAppMonitor方法名列表

接口名

说明

setInterfaceName(String interfaceName)

设置监控的接口名称

setReqSize(long reqSize)

请求包大小,单位:byte

setRespSize(long respSize)

响应包大小,单位:byte

setResultType(int resultType)

SUCCESS_RESULT_TYPE;</br> FAILURE_RESULT_TYPE;</br> LOGIC_FAILURE_RESULT_TYPE;

setMillisecondsConsume(long millisecondsConsume)

调用耗时,单位:毫秒(ms)

setReturnCode(int returnCode)

监控接口业务返回码

setSampling(int sampling)

采样率: 默认为1,表示100%。如果是1/2,则填2,如果是1/4,则填4,若是1/n,则填n

     //新建监控接口对象
     StatAppMonitor monitor = new StatAppMonitor("requestApi");
     try {
         // 被监控的接口
         int retCode = xxx.xxxxx();
         long difftime = System.currentTimeMillis() - starttime;
         // 设置接口耗时
         monitor.setMillisecondsConsume(difftime);
         // 设置接口返回码
         monitor.setReturnCode(retCode);
         // 设置请求包大小,若有的话
         monitor.setReqSize(1000);
         // 设置响应包大小,若有的话
         monitor.setRespSize(2000);
         if (retCode == 0) {
             // 标记为成功
             monitor.setResultType(StatAppMonitor.SUCCESS_RESULT_TYPE);
         } else {
             // 标记为逻辑失败,可能由网络未连接等原因引起的
             // 但对于业务来说不是致命的,是可容忍的
             monitor.setResultType(StatAppMonitor.LOGIC_FAILURE_RESULT_TYPE);
         }
     } catch (Exception e) {
         // 接口调用出现异常,致命的,标识为失败
         monitor.setResultType(StatAppMonitor.FAILURE_RESULT_TYPE);
     }
     // 上报接口监控
     StatService.reportAppMonitorStat(ctx, monitor);

iOS接口监控使用指南

(1)接口

/**
 接口统计的枚举值
 */
typedef enum {
    /**
     接口调用成功
     */
    MTA_SUCCESS = 0,

    /**
     接口调用失败
     */
    MTA_FAILURE = 1,

    /**
     接口调用出现逻辑错误
     */
    MTA_LOGIC_FAILURE = 2
} MTAAppMonitorErrorType;


/**
 接口统计的数据结构
 */
@interface MTAAppMonitorStat : NSObject

/**
 监控业务接口名
 */
@property (nonatomic, retain) NSString *interface;

/**
 上传请求包量,单位字节
 */
@property uint32_t requestPackageSize;

/**
 接收应答包量,单位字节
 */
@property uint32_t responsePackageSize;

/**
 消耗的时间,单位毫秒
 */
@property uint64_t consumedMilliseconds;

/**
 业务返回的应答码
 */
@property int32_t returnCode;

/**
 业务返回类型
 */
@property MTAAppMonitorErrorType resultType;

/**
 上报采样率,默认0含义为无采样
 */
@property uint32_t sampling;
@end

/**
 对网络接口的调用情况进行统计
 参数的详细信息请看接口统计数据结构中的相关说明

 @param stat 接口统计的数据,详情请看接口统计数据结构的相关说明
 */
+ (void)reportAppMonitorStat:(MTAAppMonitorStat *)stat;

/**
 对网络接口的调用情况进行统计
 并指定上报方式
 参数的详细信息请看接口统计数据结构中的相关说明

 @param stat 接口统计的数据,详情请看接口统计数据结构的相关说明
 @param appkey 需要上报的appKey,若传入nil,则上报到启动函数中的appkey
 @param isRealTime 是否实时上报,若传入YES,则忽略全局上报策略实时上报。否则按照全局策略上报。
 */
+ (void)reportAppMonitorStat:(MTAAppMonitorStat *)stat appkey:(NSString *)appkey isRealTime:(BOOL)isRealTime;

(2)示例

-(IBAction) clickNormaltButton:(id)sender{
    MTAAppMonitorStat* stat = [[MTAAppMonitorStat alloc] init]; 
    [stat setInterface:@"interface1"];
    // ...
    [stat setRetsultType: SUCCESS];
    [MTA reportAppMonitorStat:stat];
}

Last updated