/* 发送trace日志到总线上 */ private void sendTrace(HttpServletRequest req, TraceContext c, BufferedResponseWrapper res) { AccessBean b = new AccessBean(); b.setStamp(FilterHelpers.getRequestTime(req)); b.setCost((int) FilterHelpers.getCostTime(req)); b.setTraceId(c.getTraceId()); b.setRpcId(c.getParentRpcId()); b.setClientIp(getRemoteIp(req)); b.setServerIp(ConfigHelper.getServerInnerIP()); b.setProfile(ConfigHelper.getProcessInfo().getProfile()); b.setCode(res.getStatus()); b.setSize(res.getLength()); b.setReferer(req.getHeader("Referer")); b.setUserAgent(req.getHeader("User-Agent")); b.setCookie(req.getHeader("Cookie")); b.setUid(FilterHelpers.getUserId(req)); String url = req.getServerName(); if (req.getServerPort() != 80) { url += ':' + req.getServerPort(); } if (req.getQueryString() != null) { url += '?' + req.getQueryString(); } b.setUrl(url); Message m = new Message("JinJingAccess", c.getTraceId(), JSON.toJSONBytes(b)); RocketMqSender.getInstance().asyncSend(m); }
private void copyResponse(HttpServletResponse res, BufferedResponseWrapper wrapper) throws IOException { if (wrapper.isError()) { res.setContentType("text/html; charset=UTF-8"); res.sendError(wrapper.getStatus()); } // 发送响应内容 wrapper.flushBuffer(); if (wrapper.getLength() > 0) { if (wrapper.isGZipped()) { res.setHeader("Content-Encoding", "gzip"); } if (!res.isCommitted()) { String traceId = TraceContext.get().getTraceId(); if (!Strings.isNullOrEmpty(traceId)) { res.setHeader("x-trace-id", traceId); } res.setContentLength(wrapper.getLength()); wrapper.writeTo(res.getOutputStream()); res.flushBuffer(); } } }