Skip to content

Commit 404f28d

Browse files
author
YunaiV
committed
增加 CAT 入门
1 parent 9311ff2 commit 404f28d

File tree

1 file changed

+64
-3
lines changed
  • lab-61/lab-61-demo/src/main/java/cn/iocoder/springboot/lab61/catdemo/controller

1 file changed

+64
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.iocoder.springboot.lab61.catdemo.controller;
22

33
import com.dianping.cat.Cat;
4+
import com.dianping.cat.message.Event;
45
import com.dianping.cat.message.Transaction;
56
import org.springframework.web.bind.annotation.GetMapping;
67
import org.springframework.web.bind.annotation.RequestMapping;
@@ -10,22 +11,82 @@
1011
@RequestMapping("/demo")
1112
public class DemoController {
1213

14+
/**
15+
* 监控模型 Transaction 的示例
16+
*/
1317
@GetMapping("/transaction")
14-
public String echo() {
18+
public String transaction() {
19+
// 创建 Transaction 对象
1520
Transaction t = Cat.newTransaction("URL", "pageName");
1621
try {
22+
// ... yourBusiness(); 业务逻辑
1723

18-
// yourBusiness();
19-
24+
// 设置 Transaction 的状态为成功
2025
t.setStatus(Transaction.SUCCESS);
2126
} catch (Exception e) {
27+
// 设置 Transaction 的状态为异常
2228
t.setStatus(e);
2329
} finally {
30+
// 标记 Transaction 结束
2431
t.complete();
2532
}
2633
return "success";
2734
}
2835

36+
/**
37+
* 监控模型 Event 的示例 01
38+
*/
39+
@GetMapping("/event-01")
40+
public String event01() {
41+
// Cat.logEvent("URL.Server", "127.0.0.1");
42+
Cat.logEvent("URL.Server2", "127.0.0.1", Event.SUCCESS, "data");
43+
return "success";
44+
}
45+
46+
/**
47+
* 监控模型 Event 的示例 02
48+
*/
49+
@GetMapping("/event-02")
50+
public String event02() {
51+
try {
52+
int result = 1 / 0;
53+
} catch (Throwable e) {
54+
Cat.logError(e);
55+
// Cat.logError("custom-message", e);
56+
}
57+
return "success";
58+
}
59+
60+
/**
61+
* 监控模型 Event 的示例 03
62+
*/
63+
@GetMapping("/event-03")
64+
public String event03() {
65+
try {
66+
int result = 1 / 0;
67+
} catch (Throwable e) {
68+
Cat.logErrorWithCategory("custom-category", e);
69+
// Cat.logErrorWithCategory("custom-category", "custom-message", e);
70+
}
71+
return "success";
72+
}
2973

74+
/**
75+
* 监控模型 Metric 示例 01
76+
*/
77+
@GetMapping("/metric-01")
78+
public String metric01() {
79+
Cat.logMetricForCount("visit.count", 1);
80+
return "success";
81+
}
82+
83+
/**
84+
* 监控模型 Metric 示例 02
85+
*/
86+
@GetMapping("/metric-02")
87+
public String metric02() {
88+
Cat.logMetricForDuration("visit.duration", 10L);
89+
return "success";
90+
}
3091

3192
}

0 commit comments

Comments
 (0)