DefaultProblemHandler.java
2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.dianping.cat.consumer.problem;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.unidal.helper.Splitters;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.config.server.ServerConfigManager;
import com.dianping.cat.consumer.problem.model.entity.Entity;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Heartbeat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
public class DefaultProblemHandler extends ProblemHandler {
public static final String ID = "default-problem";
@Inject
private ServerConfigManager m_configManager;
@Inject
private Set<String> m_errorTypes;
@Override
public void handle(Machine machine, MessageTree tree) {
Message message = tree.getMessage();
if (message instanceof Transaction) {
processTransaction(machine, (Transaction) message, tree);
} else if (message instanceof Event) {
processEvent(machine, (Event) message, tree);
} else if (message instanceof Heartbeat) {
processHeartbeat(machine, (Heartbeat) message, tree);
}
}
private void processEvent(Machine machine, Event message, MessageTree tree) {
if (!message.getStatus().equals(Message.SUCCESS) && m_errorTypes.contains(message.getType())) {
String type = ProblemType.ERROR.getName();
String status = message.getName();
Entity entity = findOrCreateEntity(machine, type, status);
updateEntity(tree, entity, 0);
}
}
private void processHeartbeat(Machine machine, Heartbeat heartbeat, MessageTree tree) {
String type = heartbeat.getType().toLowerCase();
if ("heartbeat".equals(type)) {
String status = heartbeat.getName();
Entity entity = findOrCreateEntity(machine, type, status);
updateEntity(tree, entity, 0);
}
}
private void processTransaction(Machine machine, Transaction transaction, MessageTree tree) {
String transactionStatus = transaction.getStatus();
if (!transactionStatus.equals(Transaction.SUCCESS)) {
String type = transaction.getType();
String name = transaction.getName();
Entity entity = findOrCreateEntity(machine, type, name);
updateEntity(tree, entity, 0);
}
List<Message> children = transaction.getChildren();
for (Message message : children) {
if (message instanceof Transaction) {
processTransaction(machine, (Transaction) message, tree);
} else if (message instanceof Event) {
processEvent(machine, (Event) message, tree);
} else if (message instanceof Heartbeat) {
processHeartbeat(machine, (Heartbeat) message, tree);
}
}
}
public void setErrorType(String type) {
m_errorTypes = new HashSet<String>(Splitters.by(',').noEmptyItem().split(type));
}
}