EventAnalyzer.java
5.01 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
package com.dianping.cat.consumer.event;
import java.util.List;
import java.util.Map;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Constants;
import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.config.server.ServerFilterConfigManager;
import com.dianping.cat.consumer.event.model.entity.EventName;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.entity.EventType;
import com.dianping.cat.consumer.event.model.entity.Range;
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 EventAnalyzer extends AbstractMessageAnalyzer<EventReport> implements LogEnabled {
public static final String ID = "event";
@Inject
private EventDelegate m_delegate;
@Inject(ID)
private ReportManager<EventReport> m_reportManager;
@Inject
private ServerFilterConfigManager m_serverFilterConfigManager;
private EventTpsStatisticsComputer m_computer = new EventTpsStatisticsComputer();
@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 int getAnanlyzerCount() {
return 2;
}
@Override
public EventReport getReport(String domain) {
if (!Constants.ALL.equals(domain)) {
long period = getStartTime();
long timestamp = System.currentTimeMillis();
long remainder = timestamp % 3600000;
long current = timestamp - remainder;
EventReport report = m_reportManager.getHourlyReport(period, domain, false);
report.getDomainNames().addAll(m_reportManager.getDomains(getStartTime()));
if (period == current) {
report.accept(m_computer.setDuration(remainder / 1000));
} else if (period < current) {
report.accept(m_computer.setDuration(3600));
}
return report;
} else {
Map<String, EventReport> reports = m_reportManager.getHourlyReports(getStartTime());
return m_delegate.createAggregatedReport(reports);
}
}
@Override
public ReportManager<EventReport> 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();
if (m_serverFilterConfigManager.validateDomain(domain)) {
EventReport report = m_reportManager.getHourlyReport(getStartTime(), domain, true);
Message message = tree.getMessage();
String ip = tree.getIpAddress();
if (message instanceof Transaction) {
processTransaction(report, tree, (Transaction) message, ip);
} else if (message instanceof Event) {
processEvent(report, tree, (Event) message, ip);
}
}
}
private void processEvent(EventReport report, MessageTree tree, Event event, String ip) {
int count = 1;
EventType type = report.findOrCreateMachine(ip).findOrCreateType(event.getType());
EventName name = type.findOrCreateName(event.getName());
String messageId = tree.getMessageId();
report.addIp(tree.getIpAddress());
type.incTotalCount(count);
name.incTotalCount(count);
if (event.isSuccess()) {
if (type.getSuccessMessageUrl() == null) {
type.setSuccessMessageUrl(messageId);
}
if (name.getSuccessMessageUrl() == null) {
name.setSuccessMessageUrl(messageId);
}
} else {
type.incFailCount(count);
name.incFailCount(count);
if (type.getFailMessageUrl() == null) {
type.setFailMessageUrl(messageId);
}
if (name.getFailMessageUrl() == null) {
name.setFailMessageUrl(messageId);
}
}
type.setFailPercent(type.getFailCount() * 100.0 / type.getTotalCount());
name.setFailPercent(name.getFailCount() * 100.0 / name.getTotalCount());
processEventGrpah(name, event, count);
}
private void processEventGrpah(EventName name, Event t, int count) {
long current = t.getTimestamp() / 1000 / 60;
int min = (int) (current % (60));
Range range = name.findOrCreateRange(min);
range.incCount(count);
if (!t.isSuccess()) {
range.incFails(count);
}
}
private void processTransaction(EventReport report, MessageTree tree, Transaction t, String ip) {
List<Message> children = t.getChildren();
for (Message child : children) {
if (child instanceof Transaction) {
processTransaction(report, tree, (Transaction) child, ip);
} else if (child instanceof Event) {
processEvent(report, tree, (Event) child, ip);
}
}
}
public void setReportManager(ReportManager<EventReport> reportManager) {
m_reportManager = reportManager;
}
}