MetricAnalyzer.java
10.3 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.dianping.cat.consumer.metric;
import java.util.Date;
import java.util.HashMap;
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 org.unidal.lookup.util.StringUtils;
import com.dianping.cat.Cat;
import com.dianping.cat.Constants;
import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.configuration.NetworkInterfaceManager;
import com.dianping.cat.consumer.config.ProductLineConfig;
import com.dianping.cat.consumer.config.ProductLineConfigManager;
import com.dianping.cat.consumer.dal.BusinessReport;
import com.dianping.cat.consumer.dal.BusinessReportDao;
import com.dianping.cat.consumer.metric.model.entity.MetricItem;
import com.dianping.cat.consumer.metric.model.entity.MetricReport;
import com.dianping.cat.consumer.metric.model.entity.Segment;
import com.dianping.cat.consumer.metric.model.transform.DefaultNativeBuilder;
import com.dianping.cat.consumer.metric.model.transform.DefaultSaxParser;
import com.dianping.cat.consumer.metric.model.transform.DefaultXmlBuilder;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Metric;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.report.ReportBucket;
import com.dianping.cat.report.ReportBucketManager;
import com.dianping.cat.report.ReportManager;
import com.dianping.cat.task.TaskManager;
import com.dianping.cat.task.TaskManager.TaskProlicy;
public class MetricAnalyzer extends AbstractMessageAnalyzer<MetricReport> implements LogEnabled {
public static final String ID = "metric";
@Inject
private ReportBucketManager m_bucketManager;
@Inject
private BusinessReportDao m_businessReportDao;
@Inject
private MetricConfigManager m_configManager;
@Inject
private ProductLineConfigManager m_productLineConfigManager;
@Inject
private TaskManager m_taskManager;
private Map<String, MetricReport> m_reports = new HashMap<String, MetricReport>();
private static final String METRIC = "Metric";
@Override
public synchronized void doCheckpoint(boolean atEnd) {
storeReports(atEnd);
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
private MetricReport findOrCreateReport(String product) {
MetricReport report = m_reports.get(product);
if (report == null) {
report = new MetricReport(product);
report.setStartTime(new Date(m_startTime));
report.setEndTime(new Date(m_startTime + MINUTE * 60 - 1));
m_reports.put(product, report);
}
return report;
}
public MetricReport getReport(String group) {
MetricReport report = m_reports.get(group);
if (report == null) {
report = new MetricReport(group);
report.setStartTime(new Date(m_startTime));
report.setEndTime(new Date(m_startTime + MINUTE * 60 - 1));
}
return report;
}
@Override
public ReportManager<?> getReportManager() {
return null;
}
protected void loadReports() {
ReportBucket reportBucket = null;
try {
reportBucket = m_bucketManager.getReportBucket(m_startTime, MetricAnalyzer.ID, m_index);
for (String id : reportBucket.getIds()) {
String xml = reportBucket.findById(id);
MetricReport report = DefaultSaxParser.parse(xml);
m_reports.put(report.getProduct(), report);
}
} catch (Exception e) {
m_logger.error(String.format("Error when loading metric reports of %s!", new Date(m_startTime)), e);
} finally {
if (reportBucket != null) {
m_bucketManager.closeBucket(reportBucket);
}
}
}
private ConfigItem parseValue(String status, String data) {
ConfigItem config = new ConfigItem();
if ("C".equals(status)) {
if (StringUtils.isEmpty(data)) {
data = "1";
}
int count = (int) Double.parseDouble(data);
config.setCount(count);
config.setValue((double) count);
config.setShowCount(true);
} else if ("T".equals(status)) {
double duration = Double.parseDouble(data);
config.setCount(1);
config.setValue(duration);
config.setShowAvg(true);
} else if ("S".equals(status)) {
double sum = Double.parseDouble(data);
config.setCount(1);
config.setValue(sum);
config.setShowSum(true);
} else if ("S,C".equals(status)) {
String[] datas = data.split(",");
config.setCount(Integer.parseInt(datas[0]));
config.setValue(Double.parseDouble(datas[1]));
config.setShowSum(true);
} else {
return null;
}
return config;
}
@Override
public void process(MessageTree tree) {
String domain = tree.getDomain();
String group = m_productLineConfigManager.queryProductLineByDomain(domain);
MetricReport report = null;
if (group != null) {
report = findOrCreateReport(group);
}
Message message = tree.getMessage();
if (message instanceof Transaction) {
processTransaction(report, tree, (Transaction) message);
} else if (message instanceof Metric) {
processMetric(report, tree, (Metric) message);
}
}
private int processMetric(MetricReport report, MessageTree tree, Metric metric) {
String group = metric.getType();
String metricName = metric.getName();
String domain = tree.getDomain();
String data = (String) metric.getData();
String status = metric.getStatus();
ConfigItem config = parseValue(status, data);
if (StringUtils.isNotEmpty(group)) {
boolean result = m_productLineConfigManager.insertIfNotExsit(group, domain);
if (!result) {
m_logger.error(String.format("error when insert product line info, productline %s, domain %s", group,
domain));
}
report = findOrCreateReport(group);
}
if (config != null && report != null) {
long current = metric.getTimestamp() / 1000 / 60;
int min = (int) (current % (60));
String key = m_configManager.buildMetricKey(domain, METRIC, metricName);
MetricItem metricItem = report.findOrCreateMetricItem(key);
metricItem.addDomain(domain).setType(status);
updateMetric(metricItem, min, config.getCount(), config.getValue());
config.setTitle(metricName);
ProductLineConfig productLineConfig = m_productLineConfigManager.queryProductLine(report.getProduct());
if (ProductLineConfig.METRIC.equals(productLineConfig)) {
boolean result = m_configManager.insertMetricIfNotExist(domain, METRIC, metricName, config);
if (!result) {
m_logger.error(String.format("error when insert metric config info, domain %s, metricName %s", domain,
metricName));
}
}
}
return 0;
}
private int processTransaction(MetricReport report, MessageTree tree, Transaction t) {
int count = 0;
List<Message> children = t.getChildren();
for (Message child : children) {
if (child instanceof Transaction) {
count += processTransaction(report, tree, (Transaction) child);
} else if (child instanceof Metric) {
count += processMetric(report, tree, (Metric) child);
}
}
return count;
}
public void setBucketManager(ReportBucketManager bucketManager) {
m_bucketManager = bucketManager;
}
public void setBusinessReportDao(BusinessReportDao businessReportDao) {
m_businessReportDao = businessReportDao;
}
public void setConfigManager(MetricConfigManager configManager) {
m_configManager = configManager;
}
public void setProductLineConfigManager(ProductLineConfigManager productLineConfigManager) {
m_productLineConfigManager = productLineConfigManager;
}
public void setTaskManager(TaskManager taskManager) {
m_taskManager = taskManager;
}
protected void storeReports(boolean atEnd) {
ReportBucket reportBucket = null;
Transaction t = Cat.getProducer().newTransaction("Checkpoint", ID);
t.setStatus(Message.SUCCESS);
try {
reportBucket = m_bucketManager.getReportBucket(m_startTime, MetricAnalyzer.ID, m_index);
for (MetricReport report : m_reports.values()) {
try {
String xml = new DefaultXmlBuilder(true).buildXml(report);
String product = report.getProduct();
reportBucket.storeById(product, xml);
} catch (Exception e) {
t.setStatus(e);
Cat.logError(e);
}
}
if (atEnd && !isLocalMode()) {
Date period = new Date(m_startTime);
String ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
int binary = 1;
for (MetricReport report : m_reports.values()) {
try {
BusinessReport r = m_businessReportDao.createLocal();
String product = report.getProduct();
r.setName(ID);
r.setProductLine(product);
r.setPeriod(period);
r.setIp(ip);
r.setType(binary);
r.setContent(DefaultNativeBuilder.build(report));
r.setCreationDate(new Date());
m_businessReportDao.insert(r);
} catch (Throwable e) {
m_logger.error(report.toString());
t.setStatus(e);
Cat.getProducer().logError(e);
}
}
m_taskManager.createTask(period, Constants.CAT, ID, TaskProlicy.DAILY);
}
} catch (Exception e) {
Cat.getProducer().logError(e);
t.setStatus(e);
m_logger.error(String.format("Error when storing metric reports of %s!", new Date(m_startTime)), e);
} finally {
t.complete();
if (reportBucket != null) {
m_bucketManager.closeBucket(reportBucket);
}
}
}
private void updateMetric(MetricItem metricItem, int minute, int count, double sum) {
Segment seg = metricItem.findOrCreateSegment(minute);
seg.setCount(seg.getCount() + count);
seg.setSum(seg.getSum() + sum);
seg.setAvg(seg.getSum() / seg.getCount());
}
public static class ConfigItem {
private int m_count;
private double m_value;
private boolean m_showCount = false;
private boolean m_showAvg = false;
private boolean m_showSum = false;
private String m_title;
public int getCount() {
return m_count;
}
public String getTitle() {
return m_title;
}
public double getValue() {
return m_value;
}
public boolean isShowAvg() {
return m_showAvg;
}
public boolean isShowCount() {
return m_showCount;
}
public boolean isShowSum() {
return m_showSum;
}
public ConfigItem setCount(int count) {
m_count = count;
return this;
}
public ConfigItem setShowAvg(boolean showAvg) {
m_showAvg = showAvg;
return this;
}
public ConfigItem setShowCount(boolean showCount) {
m_showCount = showCount;
return this;
}
public ConfigItem setShowSum(boolean showSum) {
m_showSum = showSum;
return this;
}
public void setTitle(String title) {
m_title = title;
}
public ConfigItem setValue(double value) {
m_value = value;
return this;
}
}
}