MonitoredHttpRequest extends MonitoredRequest { @Override public Span createSpan() { boolean sample = true; if (servletPlugin.isHonorDoNotTrackHeader() && "1".equals(httpServletRequest.getHeader("dnt"))) { sample = false; } final Tracer tracer = tracingPlugin.getTracer(); io.opentracing.SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, new HttpServletRequestTextMapExtractAdapter(httpServletRequest)); Tracer.SpanBuilder spanBuilder = tracer.buildSpan(getRequestName()) .asChildOf(spanCtx) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER); if (widgetAndStagemonitorEndpointsAllowed) { spanBuilder = spanBuilder.withTag(WIDGET_ALLOWED_ATTRIBUTE, (String) null); } if (!sample) { spanBuilder = spanBuilder.withTag(Tags.SAMPLING_PRIORITY.getKey(), 0); } spanBuilder.withTag(SpanUtils.OPERATION_TYPE, "http"); final Span span = spanBuilder.start(); Tags.HTTP_URL.set(span, httpServletRequest.getRequestURI()); Tags.PEER_PORT.set(span, httpServletRequest.getRemotePort()); span.setTag("method", httpServletRequest.getMethod()); span.setTag("http.referring_site", getReferringSite()); if (servletPlugin.isCollectHttpHeaders()) { SpanUtils.setHttpHeaders(span, getHeaders(httpServletRequest)); } SpanContextInformation info = SpanContextInformation.forSpan(span); info.addRequestAttribute(CONNECTION_ID_ATTRIBUTE, connectionId); info.addRequestAttribute(MONITORED_HTTP_REQUEST_ATTRIBUTE, this); if (tracingPlugin.isSampled(span) && servletPlugin.isParseUserAgent() && StringUtils.isNotEmpty(userAgentHeader)) { parseUserAgentAsync(span, info); } return span; } MonitoredHttpRequest(HttpServletRequest httpServletRequest,
								StatusExposingByteCountingServletResponse responseWrapper,
								FilterChain filterChain, ConfigurationRegistry configuration, ExecutorService userAgentParsingExecutor); @Override Span createSpan(); static String getClientIp(HttpServletRequest request); String getRequestName(); static String getRequestNameByRequest(HttpServletRequest request, ServletPlugin servletPlugin); static String removeSemicolonContent(String requestUri); @Override void execute(); static final String CONNECTION_ID_ATTRIBUTE; static final String WIDGET_ALLOWED_ATTRIBUTE; static final String MONITORED_HTTP_REQUEST_ATTRIBUTE; static final String USER_AGENT_PARSED_FUTURE_ATTRIBUTE; }
@Test public void testCreateSpan() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js"); request.addParameter("foo", "bar"); request.addParameter("bla", "blubb"); request.addParameter("pwd", "secret"); request.addParameter("creditCard", "123456789"); request.addHeader("Cookie", "foobar"); request.addHeader("Accept", "application/json"); final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request); Span span = monitoredHttpRequest.createSpan(); Scope scope = tracer.scopeManager().activate(span); span.finish(); scope.close(); assertEquals(1, tracer.finishedSpans().size()); final MockSpan mockSpan = tracer.finishedSpans().get(0); assertEquals("/test.js", mockSpan.tags().get(Tags.HTTP_URL.getKey())); assertEquals("GET *.js", mockSpan.operationName()); assertEquals("GET", mockSpan.tags().get("method")); assertEquals("application/json", mockSpan.tags().get(SpanUtils.HTTP_HEADERS_PREFIX + "accept")); assertFalse(mockSpan.tags().containsKey(SpanUtils.HTTP_HEADERS_PREFIX + "cookie")); assertFalse(mockSpan.tags().containsKey(SpanUtils.HTTP_HEADERS_PREFIX + "Cookie")); assertEquals("bar", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "foo")); assertEquals("blubb", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "bla")); assertEquals("XXXX", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "pwd")); assertEquals("XXXX", mockSpan.tags().get(SpanUtils.PARAMETERS_PREFIX + "creditCard")); assertFalse(mockSpan.tags().containsKey(Tags.ERROR.getKey())); }
@Test public void testNumberFormatExceptionCreateSpan() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js"); request.addHeader("spanid", "<script>alert(1);</script>"); request.addHeader("traceid", "42"); final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request); Span span = monitoredHttpRequest.createSpan(); Scope scope = tracer.scopeManager().activate(span); span.finish(); scope.close(); assertEquals(1, tracer.finishedSpans().size()); final MockSpan mockSpan = tracer.finishedSpans().get(0); assertEquals("/test.js", mockSpan.tags().get(Tags.HTTP_URL.getKey())); assertEquals("GET *.js", mockSpan.operationName()); assertEquals("GET", mockSpan.tags().get("method")); assertFalse(mockSpan.tags().containsKey(Tags.ERROR.getKey())); }
@Test public void testReferringSite() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js"); request.addHeader("Referer", "https: final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request); Span span = monitoredHttpRequest.createSpan(); Scope scope = tracer.scopeManager().activate(span); span.finish(); scope.close(); assertEquals(1, tracer.finishedSpans().size()); final MockSpan mockSpan = tracer.finishedSpans().get(0); assertEquals("www.github.com", mockSpan.tags().get("http.referring_site")); }
@Test public void testReferringSameHostSite() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js"); request.addHeader("Referer", "https: request.setServerName("www.myapp.com"); final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request); Span span = monitoredHttpRequest.createSpan(); Scope scope = tracer.scopeManager().activate(span); span.finish(); scope.close(); assertEquals(1, tracer.finishedSpans().size()); final MockSpan mockSpan = tracer.finishedSpans().get(0); assertNull(mockSpan.tags().get("http.referring_site")); }
@Test public void testParseUserAgent() throws Exception { doReturn(true).when(servletPlugin).isParseUserAgent(); final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.js"); request.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); final MonitoredHttpRequest monitoredHttpRequest = createMonitoredHttpRequest(request); Span span = monitoredHttpRequest.createSpan(); Scope scope = tracer.scopeManager().activate(span); span.finish(); scope.close(); assertEquals(1, tracer.finishedSpans().size()); final MockSpan mockSpan = tracer.finishedSpans().get(0); assertThat(mockSpan.tags()).containsEntry("user_agent.browser", "Chrome"); }