DependencyAnalyzer.java
6.67 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.dianping.cat.consumer.dependency;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.config.server.ServerFilterConfigManager;
import com.dianping.cat.consumer.dependency.model.entity.Dependency;
import com.dianping.cat.consumer.dependency.model.entity.DependencyReport;
import com.dianping.cat.consumer.dependency.model.entity.Index;
import com.dianping.cat.consumer.dependency.model.entity.Segment;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.report.DefaultReportManager.StoragePolicy;
import com.dianping.cat.report.ReportManager;
public class DependencyAnalyzer extends AbstractMessageAnalyzer<DependencyReport> implements LogEnabled {
public static final String ID = "dependency";
@Inject(ID)
private ReportManager<DependencyReport> m_reportManager;
@Inject
private ServerFilterConfigManager m_serverFilterConfigManager;
@Inject
private DatabaseParser m_parser;
private Set<String> m_types = new HashSet<String>(Arrays.asList("URL", "SQL", "Call", "PigeonCall", "Service",
"PigeonService"));
private Set<String> m_exceptions = new HashSet<String>(Arrays.asList("Exception", "RuntimeException", "Error"));
@Override
public synchronized void doCheckpoint(boolean atEnd) {
if (atEnd && !isLocalMode()) {
m_reportManager.storeHourlyReports(getStartTime(), StoragePolicy.FILE_AND_DB, m_index);
} else {
m_reportManager.storeHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
private DependencyReport findOrCreateReport(String domain) {
return m_reportManager.getHourlyReport(getStartTime(), domain, true);
}
@Override
public int getAnanlyzerCount() {
return 2;
}
@Override
public DependencyReport getReport(String domain) {
DependencyReport report = m_reportManager.getHourlyReport(getStartTime(), domain, false);
report.getDomainNames().addAll(m_reportManager.getDomains(getStartTime()));
return report;
}
@Override
public ReportManager<DependencyReport> getReportManager() {
return m_reportManager;
}
private boolean isCache(String type) {
return type.startsWith("Cache.");
}
@Override
protected void loadReports() {
m_reportManager.loadHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
private String parseDatabase(Transaction t) {
List<Message> messages = t.getChildren();
for (Message message : messages) {
if (message instanceof Event) {
String type = message.getType();
if (type.equals("SQL.Database")) {
return m_parser.parseDatabaseName(message.getName());
}
}
}
return null;
}
private String parseServerName(Transaction t) {
List<Message> messages = t.getChildren();
for (Message message : messages) {
if (message instanceof Event) {
if (message.getType().equals("PigeonCall.app")) {
return message.getName();
}
}
}
return null;
}
@Override
public void process(MessageTree tree) {
DependencyReport report = findOrCreateReport(tree.getDomain());
Message message = tree.getMessage();
if (message instanceof Transaction) {
processTransaction(report, tree, (Transaction) message);
} else if (message instanceof Event) {
processEvent(report, tree, (Event) message);
}
}
private void processEvent(DependencyReport report, MessageTree tree, Event event) {
String type = event.getType();
if (m_exceptions.contains(type)) {
long current = event.getTimestamp() / 1000 / 60;
int min = (int) (current % (60));
Segment segment = report.findOrCreateSegment(min);
Index index = segment.findOrCreateIndex("Exception");
index.incTotalCount();
index.incErrorCount();
}
}
private void processPigeonTransaction(DependencyReport report, MessageTree tree, Transaction t, String type) {
if ("PigeonCall".equals(type) || "Call".equals(type)) {
String target = parseServerName(t);
String callType = "PigeonCall";
if (target != null) {
updateDependencyInfo(report, t, target, callType);
DependencyReport serverReport = findOrCreateReport(target);
updateDependencyInfo(serverReport, t, tree.getDomain(), "PigeonService");
}
}
}
private void processSqlTransaction(DependencyReport report, Transaction t, String type) {
if ("SQL".equals(type)) {
String database = parseDatabase(t);
if (database != null) {
updateDependencyInfo(report, t, database, "Database");
}
}
}
private void processTransaction(DependencyReport report, MessageTree tree, Transaction t) {
String type = t.getType();
String name = t.getName();
if (m_serverFilterConfigManager.discardTransaction(type, name)) {
return;
} else {
processTransactionType(report, t, type);
processSqlTransaction(report, t, type);
processPigeonTransaction(report, tree, t, type);
List<Message> children = t.getChildren();
for (Message child : children) {
if (child instanceof Transaction) {
processTransaction(report, tree, (Transaction) child);
} else if (child instanceof Event) {
processEvent(report, tree, (Event) child);
}
}
}
}
private void processTransactionType(DependencyReport report, Transaction t, String type) {
if (m_types.contains(type) || isCache(type)) {
long current = t.getTimestamp() / 1000 / 60;
int min = (int) (current % (60));
Segment segment = report.findOrCreateSegment(min);
Index index = segment.findOrCreateIndex(type);
if (!t.getStatus().equals(Transaction.SUCCESS)) {
index.incErrorCount();
}
index.incTotalCount();
index.setSum(index.getSum() + t.getDurationInMillis());
index.setAvg(index.getSum() / index.getTotalCount());
}
if (isCache(type)) {
updateDependencyInfo(report, t, type, "Cache");
}
}
private void updateDependencyInfo(DependencyReport report, Transaction t, String target, String type) {
synchronized (report) {
long current = t.getTimestamp() / 1000 / 60;
int min = (int) (current % (60));
Segment segment = report.findOrCreateSegment(min);
Dependency dependency = segment.findOrCreateDependency(type + ":" + target);
dependency.setType(type);
dependency.setTarget(target);
if (!t.getStatus().equals(Transaction.SUCCESS)) {
dependency.incErrorCount();
}
dependency.incTotalCount();
dependency.setSum(dependency.getSum() + t.getDurationInMillis());
dependency.setAvg(dependency.getSum() / dependency.getTotalCount());
}
}
}