MatrixAnalyzer.java
4.02 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
package com.dianping.cat.consumer.matrix;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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.consumer.matrix.model.entity.Matrix;
import com.dianping.cat.consumer.matrix.model.entity.MatrixReport;
import com.dianping.cat.consumer.matrix.model.entity.Ratio;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.report.ReportManager;
import com.dianping.cat.report.DefaultReportManager.StoragePolicy;
public class MatrixAnalyzer extends AbstractMessageAnalyzer<MatrixReport> implements LogEnabled {
public static final String ID = "matrix";
@Inject(ID)
private ReportManager<MatrixReport> m_reportManager;
@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;
}
@Override
public MatrixReport getReport(String domain) {
MatrixReport report = m_reportManager.getHourlyReport(getStartTime(), domain, false);
report.getDomainNames().addAll(m_reportManager.getDomains(getStartTime()));
return report;
}
@Override
public ReportManager<MatrixReport> getReportManager() {
return m_reportManager;
}
@Override
protected void loadReports() {
m_reportManager.loadHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
@Override
public void process(MessageTree tree) {
String domain = tree.getDomain();
MatrixReport report = m_reportManager.getHourlyReport(getStartTime(), domain, true);
Message message = tree.getMessage();
if (message instanceof Transaction) {
String messageType = message.getType();
if (messageType.equals("URL") || messageType.equals("Service") || messageType.equals("PigeonService")) {
Matrix matrix = report.findOrCreateMatrix(message.getName());
matrix.setType(message.getType());
matrix.setName(message.getName());
long duration = ((Transaction) message).getDurationInMicros();
matrix.incCount();
matrix.setTotalTime(matrix.getTotalTime() + duration);
Map<String, Ratio> ratios = new HashMap<String, Ratio>();
ratios.put("Call", new Ratio());
ratios.put("SQL", new Ratio());
ratios.put("Cache", new Ratio());
processTransaction(tree, (Transaction) message, ratios);
for (Entry<String, Ratio> entry : ratios.entrySet()) {
String type = entry.getKey();
Ratio ratio = entry.getValue();
int count = ratio.getTotalCount();
long time = ratio.getTotalTime();
Ratio real = matrix.findOrCreateRatio(type);
if (real.getMin() > count || real.getMin() == 0) {
real.setMin(count);
}
if (real.getMax() < count) {
real.setMax(count);
real.setUrl(tree.getMessageId());
}
real.setTotalCount(real.getTotalCount() + count);
real.setTotalTime(real.getTotalTime() + time);
}
if (matrix.getUrl() == null) {
matrix.setUrl(tree.getMessageId());
}
}
}
}
private void processTransaction(MessageTree tree, Transaction t, Map<String, Ratio> ratios) {
List<Message> children = t.getChildren();
String type = t.getType();
Ratio ratio = null;
if (m_serverConfigManager.isRpcClient(type)) {
ratio = ratios.get("Call");
} else if (type.equals("SQL")) {
ratio = ratios.get("SQL");
} else if (type.startsWith("Cache.")) {
ratio = ratios.get("Cache");
}
if (ratio != null) {
ratio.incTotalCount();
ratio.setTotalTime(ratio.getTotalTime() + t.getDurationInMicros());
}
for (Message child : children) {
if (child instanceof Transaction) {
processTransaction(tree, (Transaction) child, ratios);
}
}
}
}