/** * Attempts to pull a valid ID for the user making the request. * * @return The HTTP Header value of the user ID if it exists, null otherwise. The request's headers will be inspected for the user ID using the given list of userIdHeaderKeys * in list order - the first one found that is not null/empty will be returned. */ public static String getUserIdFromHttpServletRequest(HttpServletRequest servletRequest, List<String> userIdHeaderKeys) { if (servletRequest == null) return null; return HttpRequestTracingUtils.getUserIdFromRequestWithHeaders(new RequestWithHeadersServletAdapter(servletRequest), userIdHeaderKeys); }
@Test public void getHeader_delegates_to_servlet_request() { // given String headerName = "someHeader"; String headerValue = UUID.randomUUID().toString(); doReturn(headerValue).when(requestMock).getHeader(headerName); // when String actual = adapter.getHeader(headerName); // then assertThat(actual).isEqualTo(headerValue); verify(requestMock).getHeader(headerName); }
@Test public void getAttribute_delegates_to_servlet_request() { // given String attributeKey = "someAttr"; Object attributeValue = UUID.randomUUID(); doReturn(attributeValue).when(requestMock).getAttribute(attributeKey); // when Object actual = adapter.getAttribute(attributeKey); // then assertThat(actual).isEqualTo(attributeValue); verify(requestMock).getAttribute(attributeKey); }
/** * @param servletRequest The incoming request that may have {@link Span} information embedded in the headers. If this argument is null then this method will return null. * @param userIdHeaderKeys This list of header keys will be used to search the request headers for a user ID to set on the returned span. The user ID header keys * will be searched in list order, and the first non-empty user ID header value found will be used as the {@link Span#getUserId()}. * You can safely pass in null or an empty list for this argument if there is no user ID to extract; if you pass in null then the returned * span's {@link Span#getUserId()} will be null. * * @return The {@link Span} stored in the given request's trace headers (e.g. {@link com.nike.wingtips.TraceHeaders#TRACE_ID}, {@link com.nike.wingtips.TraceHeaders#TRACE_SAMPLED}, * {@link com.nike.wingtips.TraceHeaders#PARENT_SPAN_ID}, etc), or null if the request is null or doesn't contain the necessary headers. * <br/> * NOTE: {@link com.nike.wingtips.TraceHeaders#TRACE_ID} is the minimum header needed to return a non-null {@link Span}. If {@link com.nike.wingtips.TraceHeaders#SPAN_ID} * is missing then a new span ID will be generated using {@link com.nike.wingtips.TraceAndSpanIdGenerator#generateId()}. * If {@link com.nike.wingtips.TraceHeaders#TRACE_SAMPLED} is missing then the returned span will be sampleable. If {@link com.nike.wingtips.TraceHeaders#SPAN_NAME} * is missing then {@link HttpRequestTracingUtils#UNSPECIFIED_SPAN_NAME} will be used as the span name. */ public static Span fromHttpServletRequest(HttpServletRequest servletRequest, List<String> userIdHeaderKeys) { if (servletRequest == null) return null; return HttpRequestTracingUtils.fromRequestWithHeaders(new RequestWithHeadersServletAdapter(servletRequest), userIdHeaderKeys); }
@Before public void setupMethod() { requestMock = mock(HttpServletRequest.class); adapter = new RequestWithHeadersServletAdapter(requestMock); }
@Test(expected = IllegalArgumentException.class) public void constructor_throws_illegal_argument_exception_if_passed_null_arg() { // expect new RequestWithHeadersServletAdapter(null); fail("Expected IllegalArgumentException but no exception was thrown"); }