使用CAT进行打点:
- 使用CAT进行打点,以下是CatUtil的主要代码:
/**
* 开启一个新事务记录事件
*
* @param txType
* @param txName
* @param kvs
*/
public static void logEvent(String txType, String txName, KVPair... kvs) {
if (kvs == null) {
return;
}
Transaction t = null;
try {
MessageProducer producer = Cat.getProducer();
t = producer.newTransaction(txType, txName);
for (KVPair kv : kvs) {
producer.logEvent(kv.getKey(), kv.getValue());
}
t.setStatus(Transaction.SUCCESS);
} catch (Throwable th) {
t.setStatus(th);
log.error("faid to parse token and calculate time", th);
} finally {
complete(t);
}
}
/**
* 结束事务
*
* @param t
*/
public static void complete(Transaction t) {
try {
if (t != null && !t.isCompleted()) {
t.complete();
}
} catch (Throwable thr) {
log.error("fail to complete transaction", thr);
}
}
简单KV对象:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class KVPair {
private String key;
private String value;
public static KVPair of(String key, String value) {
return new KVPair(key, value);
}
}
在需要打点的地方调用logEvent方法,如下所示:
CatUtil.logEvent(AUTH_TIME, "AUTH_TIME_EVENT", KVPair.of(AUTH_TIME, "AUTH_TIME_ALL=" + fullTimeSpent));