ClientSpanServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { handleRequest(req, resp); } ClientSpanServlet();   ClientSpanServlet(TracingPlugin tracingPlugin, ServletPlugin servletPlugin);   }
@Test public void testConvertWeaselBeaconToSpan_withMetadata() throws ServletException, IOException { MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.setParameter("ty", "pl"); mockHttpServletRequest.setParameter("r", "1496751574200"); mockHttpServletRequest.setParameter("u", "http: mockHttpServletRequest.setParameter("m_user", "tom.mason@example.com"); mockHttpServletRequest.setParameter("ts", "-197"); mockHttpServletRequest.setParameter("d", "518"); mockHttpServletRequest.setParameter("t_unl", "0"); mockHttpServletRequest.setParameter("t_red", "0"); mockHttpServletRequest.setParameter("t_apc", "5"); mockHttpServletRequest.setParameter("t_dns", "0"); mockHttpServletRequest.setParameter("t_tcp", "0"); mockHttpServletRequest.setParameter("t_ssl", "0"); mockHttpServletRequest.setParameter("t_req", "38"); mockHttpServletRequest.setParameter("t_rsp", "4"); mockHttpServletRequest.setParameter("t_pro", "471"); mockHttpServletRequest.setParameter("t_loa", "5"); mockHttpServletRequest.setParameter("t_fp", "151"); mockHttpServletRequest.setParameter("m_username", "test string here"); mockHttpServletRequest.setParameter("m_age", "26"); mockHttpServletRequest.setParameter("m_is_access_allowed", "1"); mockHttpServletRequest.setParameter("m_some_not_mapped_property", "this should not exist"); final HashMap<String, ClientSpanMetadataDefinition> whitelistedValues = new HashMap<>(); whitelistedValues.put("username", new ClientSpanMetadataDefinition("string")); whitelistedValues.put("age", new ClientSpanMetadataDefinition("number")); whitelistedValues.put("is_access_allowed", new ClientSpanMetadataDefinition("boolean")); whitelistedValues.put("parameter_not_sent", new ClientSpanMetadataDefinition("string")); when(servletPlugin.getWhitelistedClientSpanTags()).thenReturn(whitelistedValues); servlet.doGet(mockHttpServletRequest, new MockHttpServletResponse()); assertSoftly(softly -> { final List<MockSpan> finishedSpans = mockTracer.finishedSpans(); softly.assertThat(finishedSpans).hasSize(1); MockSpan span = finishedSpans.get(0); softly.assertThat(span.operationName()).isEqualTo("/petclinic/"); softly.assertThat(span.tags()) .doesNotContainEntry(Tags.SAMPLING_PRIORITY.getKey(), 0) .containsEntry("type", "pageload") .containsEntry("username", "test string here") .containsEntry("age", 26.) .containsEntry("is_access_allowed", true) .doesNotContainKey("parameter_not_sent") .doesNotContainKey("some_not_mapped_property"); }); }
@Test public void testConvertWeaselBeaconToSpan_withErrorBeacon() throws ServletException, IOException { MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.setParameter("k", "someKey"); mockHttpServletRequest.setParameter("s", "3776086ebf658768"); mockHttpServletRequest.setParameter("t", "3776086ebf658768"); mockHttpServletRequest.setParameter("ts", "1496753245024"); mockHttpServletRequest.setParameter("ty", "err"); mockHttpServletRequest.setParameter("pl", "e6bf60fdf2672398"); mockHttpServletRequest.setParameter("l", "http: mockHttpServletRequest.setParameter("e", "Uncaught null"); mockHttpServletRequest.setParameter("st", "at http: mockHttpServletRequest.setParameter("c", "1"); mockHttpServletRequest.setParameter("m_user", "tom.mason@example.com"); servlet.doGet(mockHttpServletRequest, new MockHttpServletResponse()); assertSoftly(softly -> { final List<MockSpan> finishedSpans = mockTracer.finishedSpans(); softly.assertThat(finishedSpans).hasSize(1); MockSpan span = finishedSpans.get(0); softly.assertThat(span.operationName()).isEqualTo("/petclinic/"); softly.assertThat(span.finishMicros() - span.startMicros()).isZero(); softly.assertThat(span.tags()) .doesNotContainEntry(Tags.SAMPLING_PRIORITY.getKey(), 0) .containsEntry("http.url", "http: .containsEntry("type", "js_error") .containsEntry("exception.stack_trace", "at http: .containsEntry("exception.message", "Uncaught null"); }); }
@Test public void testConvertWeaselBeaconToSpan_withErrorBeaconTrimsTooLongStackTraces() throws ServletException, IOException { MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.setParameter("k", "someKey"); mockHttpServletRequest.setParameter("s", "3776086ebf658768"); mockHttpServletRequest.setParameter("t", "3776086ebf658768"); mockHttpServletRequest.setParameter("ts", "1496753245024"); mockHttpServletRequest.setParameter("ty", "err"); mockHttpServletRequest.setParameter("pl", "e6bf60fdf2672398"); mockHttpServletRequest.setParameter("l", "http: mockHttpServletRequest.setParameter("e", "Uncaught null"); final StringBuilder stacktrace = new StringBuilder(); for (int i = 0; i < 100; i++) { stacktrace.append("at http: } mockHttpServletRequest.setParameter("st", stacktrace.toString()); mockHttpServletRequest.setParameter("c", "1"); mockHttpServletRequest.setParameter("m_user", "tom.mason@example.com"); servlet.doGet(mockHttpServletRequest, new MockHttpServletResponse()); assertSoftly(softly -> { final List<MockSpan> finishedSpans = mockTracer.finishedSpans(); softly.assertThat(finishedSpans).hasSize(1); MockSpan span = finishedSpans.get(0); softly.assertThat(span.operationName()).isEqualTo("/petclinic/"); softly.assertThat(span.finishMicros() - span.startMicros()).isZero(); softly.assertThat(span.tags()) .doesNotContainEntry(Tags.SAMPLING_PRIORITY.getKey(), 0) .containsEntry("http.url", "http: .containsEntry("type", "js_error") .containsKey("exception.stack_trace") .containsEntry("exception.message", "Uncaught null"); softly.assertThat(((String) span.tags().get("exception.stack_trace")).length()) .isLessThan(stacktrace.length()) .isGreaterThan(0); }); }
@Test public void testConvertWeaselBeaconToSpan_withXHRBeacon() throws ServletException, IOException { MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.setParameter("r", "1496994284184"); mockHttpServletRequest.setParameter("k", "null"); mockHttpServletRequest.setParameter("ts", "21793"); mockHttpServletRequest.setParameter("d", "2084"); mockHttpServletRequest.setParameter("ty", "xhr"); mockHttpServletRequest.setParameter("pl", "d58cddae830273d1"); mockHttpServletRequest.setParameter("l", "http: mockHttpServletRequest.setParameter("m", "GET"); mockHttpServletRequest.setParameter("u", "http: mockHttpServletRequest.setParameter("a", "1"); mockHttpServletRequest.setParameter("st", "200"); mockHttpServletRequest.setParameter("e", "undefined"); mockHttpServletRequest.setParameter("m_user", "tom.mason@example.com"); mockHttpServletRequest.setParameter("t", "2d371455215c504"); mockHttpServletRequest.setParameter("s", "2d371455215c504"); servlet.doGet(mockHttpServletRequest, new MockHttpServletResponse()); assertSoftly(softly -> { final List<MockSpan> finishedSpans = mockTracer.finishedSpans(); softly.assertThat(finishedSpans).hasSize(1); MockSpan span = finishedSpans.get(0); softly.assertThat(span.operationName()).isEqualTo("/petclinic/owners.html"); softly.assertThat(span.finishMicros() - span.startMicros()).isEqualTo(TimeUnit.MILLISECONDS.toMicros(2084L)); softly.assertThat(span.tags()) .doesNotContainEntry(Tags.SAMPLING_PRIORITY.getKey(), 0) .containsEntry("type", "ajax") .containsEntry("http.status_code", 200L) .containsEntry("method", "GET") .containsEntry("xhr.requested_url", "http: .containsEntry("xhr.requested_from", "http: .containsEntry("xhr.async", true) .containsEntry("duration_ms", 2084L) .containsEntry("id", "2d371455215c504") .containsEntry("trace_id", "2d371455215c504"); }); }
@Test public void testWeaselBeaconXhrBeacon_withSampledFlag() throws ServletException, IOException { MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.setParameter("t", "2d371455215c504"); mockHttpServletRequest.setParameter("s", "2d371455215c504"); mockHttpServletRequest.setParameter("ty", "xhr"); mockHttpServletRequest.setParameter("sp", "0"); mockHttpServletRequest.setParameter(METADATA_BACKEND_SPAN_ID, "1"); servlet.doGet(mockHttpServletRequest, new MockHttpServletResponse()); assertSoftly(softly -> { final List<MockSpan> finishedSpans = mockTracer.finishedSpans(); softly.assertThat(finishedSpans).hasSize(1); MockSpan span = finishedSpans.get(0); softly.assertThat(span.tags()) .containsEntry("id", "2d371455215c504") .containsEntry("trace_id", "2d371455215c504") .containsEntry(Tags.SAMPLING_PRIORITY.getKey(), 0); }); }
@Test public void testWeaselBeaconPageLoadBeacon_withSampledFlag() throws ServletException, IOException { MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); mockHttpServletRequest.setParameter("t", "2d371455215c504"); mockHttpServletRequest.setParameter("s", "2d371455215c504"); mockHttpServletRequest.setParameter("ty", TYPE_PAGE_LOAD); mockHttpServletRequest.setParameter("sp", "0"); mockHttpServletRequest.setParameter(METADATA_BACKEND_SPAN_SAMPLING_FLAG, "1"); servlet.doGet(mockHttpServletRequest, new MockHttpServletResponse()); assertSoftly(softly -> { final List<MockSpan> finishedSpans = mockTracer.finishedSpans(); softly.assertThat(finishedSpans).hasSize(1); MockSpan span = finishedSpans.get(0); softly.assertThat(span.tags()) .containsEntry("id", "2d371455215c504") .containsEntry("trace_id", "2d371455215c504") .containsEntry(Tags.SAMPLING_PRIORITY.getKey(), 1); }); }