CatFilter.java
8.55 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
package com.dianping.cat.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.unidal.helper.Joiners;
import org.unidal.helper.Joiners.IBuilder;
import com.dianping.cat.Cat;
import com.dianping.cat.CatConstants;
import com.dianping.cat.configuration.client.entity.Server;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.internal.DefaultMessageManager;
import com.dianping.cat.message.internal.DefaultTransaction;
public class CatFilter implements Filter {
private List<Handler> m_handlers = new ArrayList<Handler>();
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
Context ctx = new Context((HttpServletRequest) request, (HttpServletResponse) response, chain, m_handlers);
ctx.handle();
}
protected String getOriginalUrl(ServletRequest request) {
return ((HttpServletRequest) request).getRequestURI();
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
m_handlers.add(CatHandler.ENVIRONMENT);
m_handlers.add(CatHandler.LOG_SPAN);
m_handlers.add(CatHandler.LOG_CLIENT_PAYLOAD);
m_handlers.add(CatHandler.ID_SETUP);
}
private static enum CatHandler implements Handler {
ENVIRONMENT {
@Override
public void handle(Context ctx) throws IOException, ServletException {
HttpServletRequest req = ctx.getRequest();
boolean top = !Cat.getManager().hasContext();
ctx.setTop(top);
if (top) {
ctx.setType(CatConstants.TYPE_URL);
setTraceMode(req);
} else {
ctx.setType(CatConstants.TYPE_URL_FORWARD);
}
ctx.handle();
}
protected void setTraceMode(HttpServletRequest req) {
String traceMode = "X-CAT-TRACE-MODE";
String headMode = req.getHeader(traceMode);
if ("true".equals(headMode)) {
Cat.getManager().setTraceMode(true);
}
}
},
ID_SETUP {
private String m_servers;
private String getCatServer() {
if (m_servers == null) {
DefaultMessageManager manager = (DefaultMessageManager) Cat.getManager();
List<Server> servers = manager.getConfigManager().getServers();
m_servers = Joiners.by(',').join(servers, new IBuilder<Server>() {
@Override
public String asString(Server server) {
String ip = server.getIp();
Integer httpPort = server.getHttpPort();
return ip + ":" + httpPort;
}
});
}
return m_servers;
}
@Override
public void handle(Context ctx) throws IOException, ServletException {
boolean isTraceMode = Cat.getManager().isTraceMode();
HttpServletResponse res = ctx.getResponse();
if (isTraceMode) {
String id = Cat.getCurrentMessageId();
res.setHeader("X-CAT-ROOT-ID", id);
res.setHeader("X-CAT-SERVER", getCatServer());
}
ctx.handle();
}
},
LOG_CLIENT_PAYLOAD {
@Override
public void handle(Context ctx) throws IOException, ServletException {
HttpServletRequest req = ctx.getRequest();
String type = ctx.getType();
if (ctx.isTop()) {
logRequestClientInfo(req, type);
logRequestPayload(req, type);
} else {
logRequestPayload(req, type);
}
ctx.handle();
}
protected void logRequestClientInfo(HttpServletRequest req, String type) {
StringBuilder sb = new StringBuilder(1024);
String ip = "";
String ipForwarded = req.getHeader("x-forwarded-for");
if (ipForwarded == null) {
ip = req.getRemoteAddr();
} else {
ip = ipForwarded;
}
sb.append("IPS=").append(ip);
sb.append("&VirtualIP=").append(req.getRemoteAddr());
sb.append("&Server=").append(req.getServerName());
sb.append("&Referer=").append(req.getHeader("referer"));
sb.append("&Agent=").append(req.getHeader("user-agent"));
Cat.logEvent(type, type + ".Server", Message.SUCCESS, sb.toString());
}
protected void logRequestPayload(HttpServletRequest req, String type) {
StringBuilder sb = new StringBuilder(256);
sb.append(req.getScheme().toUpperCase()).append('/');
sb.append(req.getMethod()).append(' ').append(req.getRequestURI());
String qs = req.getQueryString();
if (qs != null) {
sb.append('?').append(qs);
}
Cat.logEvent(type, type + ".Method", Message.SUCCESS, sb.toString());
}
},
LOG_SPAN {
public static final char SPLIT = '/';
private void customizeStatus(Transaction t, HttpServletRequest req) {
Object catStatus = req.getAttribute(CatConstants.CAT_STATE);
if (catStatus != null) {
t.setStatus(catStatus.toString());
} else {
t.setStatus(Message.SUCCESS);
}
}
private void customizeUri(Transaction t, HttpServletRequest req) {
if (t instanceof DefaultTransaction) {
Object catPageType = req.getAttribute(CatConstants.CAT_PAGE_TYPE);
if (catPageType instanceof String) {
((DefaultTransaction) t).setType(catPageType.toString());
}
Object catPageUri = req.getAttribute(CatConstants.CAT_PAGE_URI);
if (catPageUri instanceof String) {
((DefaultTransaction) t).setName(catPageUri.toString());
}
}
}
private String getRequestURI(HttpServletRequest req) {
String url = req.getRequestURI();
int length = url.length();
StringBuilder sb = new StringBuilder(length);
for (int index = 0; index < length;) {
char c = url.charAt(index);
if (c == SPLIT && index < length - 1) {
sb.append(c);
StringBuilder nextSection = new StringBuilder();
boolean isNumber = false;
boolean first = true;
for (int j = index + 1; j < length; j++) {
char next = url.charAt(j);
if ((first || isNumber == true) && next != SPLIT) {
isNumber = isNumber(next);
first = false;
}
if (next == SPLIT) {
if (isNumber) {
sb.append("{num}");
} else {
sb.append(nextSection.toString());
}
index = j;
break;
} else if (j == length - 1) {
if (isNumber) {
sb.append("{num}");
} else {
nextSection.append(next);
sb.append(nextSection.toString());
}
index = j + 1;
break;
} else {
nextSection.append(next);
}
}
} else {
sb.append(c);
index++;
}
}
return sb.toString();
}
@Override
public void handle(Context ctx) throws IOException, ServletException {
HttpServletRequest req = ctx.getRequest();
Transaction t = Cat.newTransaction(ctx.getType(), getRequestURI(req));
try {
ctx.handle();
customizeStatus(t, req);
} catch (ServletException e) {
t.setStatus(e);
Cat.logError(e);
throw e;
} catch (IOException e) {
t.setStatus(e);
Cat.logError(e);
throw e;
} catch (Throwable e) {
t.setStatus(e);
Cat.logError(e);
throw new RuntimeException(e);
} finally {
customizeUri(t, req);
t.complete();
}
}
private boolean isNumber(char c) {
return (c >= '0' && c <= '9') || c == '.' || c == '-' || c == ',';
}
};
}
protected static class Context {
private FilterChain m_chain;
private List<Handler> m_handlers;
private int m_index;
private HttpServletRequest m_request;
private HttpServletResponse m_response;
private boolean m_top;
private String m_type;
public Context(HttpServletRequest request, HttpServletResponse response, FilterChain chain, List<Handler> handlers) {
m_request = request;
m_response = response;
m_chain = chain;
m_handlers = handlers;
}
public HttpServletRequest getRequest() {
return m_request;
}
public HttpServletResponse getResponse() {
return m_response;
}
public String getType() {
return m_type;
}
public void handle() throws IOException, ServletException {
if (m_index < m_handlers.size()) {
Handler handler = m_handlers.get(m_index++);
handler.handle(this);
} else {
m_chain.doFilter(m_request, m_response);
}
}
public boolean isTop() {
return m_top;
}
public void setTop(boolean top) {
m_top = top;
}
public void setType(String type) {
m_type = type;
}
}
protected static interface Handler {
public void handle(Context ctx) throws IOException, ServletException;
}
}