PageAnalyzeHandler.java
6.88 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
package com.yoho.trace.online.handler;
import com.google.common.collect.Lists;
import com.yoho.trace.anaylzer.model.ApiTraceResult;
import com.yoho.trace.anaylzer.model.SpanInfo;
import com.yoho.trace.sleuth.Span;
import com.yoho.trace.sleuth.Spans;
import com.yoho.trace.store.HBasePool;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.PairFlatMapFunction;
import org.apache.spark.api.java.function.VoidFunction;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Tuple2;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
/**
* @Author: lingjie.meng
* @Descroption:
* @Date: craete on 下午3:48 in 2017/11/10
* @ModifyBy:
*/
public class PageAnalyzeHandler implements TraceHandler, Serializable {
private static Logger logger = LoggerFactory.getLogger(TraceAnalyzeHandler.class);
private static final String SPLIT_STR = "-";
private static final String FROM_PAGE = "yoho.fromPage";
@Override
public void handle(final JavaDStream<Spans> kafkaMsgDStream) {
// 只取包含yoho.fromPage的span
JavaDStream<SpanInfo> spanInfoStream = kafkaMsgDStream.flatMap(new FlatMapFunction<Spans, SpanInfo>() {
@Override
public Iterator<SpanInfo> call(Spans spans) throws Exception {
List<SpanInfo> result = Lists.newArrayList();
List<Span> list = spans.getSpans();
Iterator<Span> ite = list.iterator();
SpanInfo spanInfo;
String pageId;
while (ite.hasNext()) {
Span span = ite.next();
//只取包含pageID的span
pageId = span.tags().get(FROM_PAGE);
if (!StringUtils.isEmpty(pageId) && !StringUtils.isEmpty(span.getName())) {
spanInfo = new SpanInfo();
spanInfo.setPageId(pageId);
spanInfo.setName(span.getName());
spanInfo.setBegin(span.getBegin());
spanInfo.setEnd(span.getEnd());
spanInfo.setTraceid(Span.idToHex(span.getTraceId()));
spanInfo.setSpanid(Span.idToHex(span.getSpanId()));
spanInfo.setService(spans.getHost().getServiceName());
spanInfo.setIp(spans.getHost().getAddress());
result.add(spanInfo);
}
}
return result.iterator();
}
});
// key:pageId:apiname, value ApiTraceResult
JavaPairDStream<String, ApiTraceResult> pageIdSpanInfoJavaPairDStream = spanInfoStream
.mapPartitionsToPair(new PairFlatMapFunction<Iterator<SpanInfo>, String, ApiTraceResult>() {
@Override
public Iterator<Tuple2<String, ApiTraceResult>> call(Iterator<SpanInfo> ite) throws Exception {
List<Tuple2<String, ApiTraceResult>> list = Lists.newArrayList();
while (ite.hasNext()) {
SpanInfo spanInfo = ite.next();
ApiTraceResult result = new ApiTraceResult();
result.setDuration(spanInfo.getEnd() - spanInfo.getBegin());
list.add(new Tuple2<>(spanInfo.getPageId() + SPLIT_STR + spanInfo.getName().replace("http:/", ""), result));
}
return list.iterator();
}
});
// key pageId:apiname, value List<ApiTraceResult>
JavaPairDStream<String, Iterable<ApiTraceResult>> pageIdGroupPairStream = pageIdSpanInfoJavaPairDStream.groupByKey();
// 给每个页面的数据汇总,key pageId:apiname, value ApiTraceResult
JavaPairDStream<String, ApiTraceResult> pageResultStream = pageIdGroupPairStream.flatMapToPair(new PairFlatMapFunction<Tuple2<String, Iterable<ApiTraceResult>>, String, ApiTraceResult>() {
@Override
public Iterator<Tuple2<String, ApiTraceResult>> call(Tuple2<String, Iterable<ApiTraceResult>> tuple2) throws Exception {
Iterator<ApiTraceResult> iterator = tuple2._2().iterator();
int calTimes = 0;
long duration = 0;
while (iterator.hasNext()) {
ApiTraceResult apiTraceResult = iterator.next();
duration = (calTimes++ * duration + apiTraceResult.getDuration()) / calTimes;
}
ApiTraceResult result = new ApiTraceResult();
result.setCallTimes(calTimes);
result.setDuration(duration);
return Arrays.asList(new Tuple2<>(tuple2._1, result)).iterator();
}
});
//入库
pageResultStream.foreachRDD(new VoidFunction<JavaPairRDD<String, ApiTraceResult>>() {
@Override
public void call(JavaPairRDD<String, ApiTraceResult> rdd) throws Exception {
rdd.foreachPartition(new VoidFunction<Iterator<Tuple2<String, ApiTraceResult>>>() {
@Override
public void call(Iterator<Tuple2<String, ApiTraceResult>> tuple2s) throws Exception {
long now = Calendar.getInstance().getTimeInMillis();
try (HTable resultTable1 = (HTable) HBasePool.getConnection().getTable(TableName.valueOf("trace_page_analyze_minutes"));
HTable resultTable2 = (HTable) HBasePool.getConnection().getTable(TableName.valueOf("trace_api_source_analyze_minutes"));
HTable resultTable3 = (HTable) HBasePool.getConnection().getTable(TableName.valueOf("page_statistics"))) {
List<Put> puts1 = Lists.newArrayList();
List<Put> puts2 = Lists.newArrayList();
List<Put> puts3 = Lists.newArrayList();
while (tuple2s.hasNext()) {
Tuple2<String, ApiTraceResult> tuple2 = tuple2s.next();
String rowKey1 = tuple2._1.split(SPLIT_STR)[0] + "-" + now + "-" + tuple2._1.split(SPLIT_STR)[1];
Put put1 = new Put(Bytes.toBytes(rowKey1));
put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes("times"), Bytes.toBytes(tuple2._2.getCallTimes()));
put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes("duration"), Bytes.toBytes(tuple2._2.getDuration()));
puts1.add(put1);
logger.info("put data to trace_page_analyze_minutes, {}", put1);
String rowKey2 = tuple2._1.split(SPLIT_STR)[1] + "-" + now + "-" + tuple2._1.split(SPLIT_STR)[0];
Put put2 = new Put(Bytes.toBytes(rowKey2));
put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes("times"), Bytes.toBytes(tuple2._2.getCallTimes()));
puts2.add(put2);
logger.info("put data to trace_api_source_analyze_minutes, {}", put2);
String rowKey3 = "page-" + now;
Put put3 = new Put(Bytes.toBytes(rowKey3));
put3.addColumn(Bytes.toBytes("data"), Bytes.toBytes("page"), Bytes.toBytes(tuple2._1.split(SPLIT_STR)[0]));
put3.addColumn(Bytes.toBytes("data"), Bytes.toBytes("api"), Bytes.toBytes(tuple2._1.split(SPLIT_STR)[1]));
puts3.add(put3);
logger.info("put data to page_statistics, {}", put3);
}
resultTable1.put(puts1);
resultTable2.put(puts2);
resultTable3.put(puts3);
} catch (Exception e) {
logger.error("store page result failed, e is {} ", e);
}
}
});
}
});
}
}