|
1 | 1 | package cn.iocoder.springboot.lab61.catdemo.controller;
|
2 | 2 |
|
3 | 3 | import com.dianping.cat.Cat;
|
| 4 | +import com.dianping.cat.message.Event; |
4 | 5 | import com.dianping.cat.message.Transaction;
|
5 | 6 | import org.springframework.web.bind.annotation.GetMapping;
|
6 | 7 | import org.springframework.web.bind.annotation.RequestMapping;
|
|
10 | 11 | @RequestMapping("/demo")
|
11 | 12 | public class DemoController {
|
12 | 13 |
|
| 14 | + /** |
| 15 | + * 监控模型 Transaction 的示例 |
| 16 | + */ |
13 | 17 | @GetMapping("/transaction")
|
14 |
| - public String echo() { |
| 18 | + public String transaction() { |
| 19 | + // 创建 Transaction 对象 |
15 | 20 | Transaction t = Cat.newTransaction("URL", "pageName");
|
16 | 21 | try {
|
| 22 | + // ... yourBusiness(); 业务逻辑 |
17 | 23 |
|
18 |
| -// yourBusiness(); |
19 |
| - |
| 24 | + // 设置 Transaction 的状态为成功 |
20 | 25 | t.setStatus(Transaction.SUCCESS);
|
21 | 26 | } catch (Exception e) {
|
| 27 | + // 设置 Transaction 的状态为异常 |
22 | 28 | t.setStatus(e);
|
23 | 29 | } finally {
|
| 30 | + // 标记 Transaction 结束 |
24 | 31 | t.complete();
|
25 | 32 | }
|
26 | 33 | return "success";
|
27 | 34 | }
|
28 | 35 |
|
| 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 | + } |
29 | 73 |
|
| 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 | + } |
30 | 91 |
|
31 | 92 | }
|
0 commit comments