Search in sources :

Example 1 with FixedSegmentNamingStrategy

use of com.amazonaws.xray.strategy.FixedSegmentNamingStrategy in project aws-xray-sdk-java by aws.

the class AWSXRayServletFilterTest method testServletUsesPassedInRecorder.

@Test
public void testServletUsesPassedInRecorder() throws IOException, ServletException {
    AWSXRayRecorder customRecorder = Mockito.spy(getMockRecorder());
    AWSXRayServletFilter servletFilter = new AWSXRayServletFilter(new FixedSegmentNamingStrategy("test"), customRecorder);
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURL()).thenReturn(new StringBuffer("test_url"));
    when(request.getMethod()).thenReturn("TEST_METHOD");
    when(request.isAsyncStarted()).thenReturn(true);
    when(request.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mockChain(request, response);
    AsyncEvent event = mock(AsyncEvent.class);
    when(event.getSuppliedRequest()).thenReturn(request);
    when(event.getSuppliedResponse()).thenReturn(response);
    servletFilter.doFilter(request, response, chain);
    Assert.assertNull(AWSXRay.getTraceEntity());
    AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
    listener.onComplete(event);
    verify(customRecorder.getEmitter(), Mockito.times(1)).sendSegment(Mockito.any());
}
Also used : FixedSegmentNamingStrategy(com.amazonaws.xray.strategy.FixedSegmentNamingStrategy) HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 2 with FixedSegmentNamingStrategy

use of com.amazonaws.xray.strategy.FixedSegmentNamingStrategy in project aws-xray-sdk-java by aws.

the class AWSXRayServletAsyncListener method init.

/**
 * @param config
 *  the filter configuration. There are various init-params which may be passed on initialization. The values in init-params will create segment naming strategies which override those passed in constructors.
 *
 * <ul>
 *
 * <li>
 * <b>fixedName</b> A String value used as the fixedName parameter for a created {@link com.amazonaws.xray.strategy.FixedSegmentNamingStrategy}. Used only if the {@code dynamicNamingFallbackName} init-param is not set.
 * </li>
 *
 * <li>
 * <b>dynamicNamingFallbackName</b> A String value used as the fallbackName parameter for a created {@link com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy}.
 * </li>
 *
 * <li>
 * <b>dynamicNamingRecognizedHosts</b> A String value used as the recognizedHosts parameter for a created {@link com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy}.
 * </li>
 *
 * </ul>
 *
 * @throws ServletException
 *  when a segment naming strategy is not provided in constructor arguments nor in init-params.
 */
@Override
public void init(FilterConfig config) throws ServletException {
    String fixedName = config.getInitParameter("fixedName");
    String dynamicNamingFallbackName = config.getInitParameter("dynamicNamingFallbackName");
    String dynamicNamingRecognizedHosts = config.getInitParameter("dynamicNamingRecognizedHosts");
    if (StringValidator.isNotNullOrBlank(dynamicNamingFallbackName)) {
        if (StringValidator.isNotNullOrBlank(dynamicNamingRecognizedHosts)) {
            segmentNamingStrategy = new DynamicSegmentNamingStrategy(dynamicNamingFallbackName, dynamicNamingRecognizedHosts);
        } else {
            segmentNamingStrategy = new DynamicSegmentNamingStrategy(dynamicNamingFallbackName);
        }
    } else if (StringValidator.isNotNullOrBlank(fixedName)) {
        segmentNamingStrategy = new FixedSegmentNamingStrategy(fixedName);
    } else if (null == segmentNamingStrategy) {
        throw new ServletException("The AWSXRayServletFilter requires either a fixedName init-param or an instance of SegmentNamingStrategy. Add an init-param tag to the AWSXRayServletFilter's declaration in web.xml, using param-name: 'fixedName'. Alternatively, pass an instance of SegmentNamingStrategy to the AWSXRayServletFilter constructor.");
    }
}
Also used : FixedSegmentNamingStrategy(com.amazonaws.xray.strategy.FixedSegmentNamingStrategy) ServletException(javax.servlet.ServletException) DynamicSegmentNamingStrategy(com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy)

Example 3 with FixedSegmentNamingStrategy

use of com.amazonaws.xray.strategy.FixedSegmentNamingStrategy in project aws-xray-sdk-java by aws.

the class AWSXRayServletFilter method init.

/**
 * @param config
 *  the filter configuration. There are various init-params which may be passed on initialization. The values in init-params
 *  will create segment naming strategies which override those passed in constructors.
 *
 * <ul>
 *
 * <li>
 * <b>fixedName</b> A String value used as the fixedName parameter for a created
 * {@link com.amazonaws.xray.strategy.FixedSegmentNamingStrategy}. Used only if the {@code dynamicNamingFallbackName}
 * init-param is not set.
 * </li>
 *
 * <li>
 * <b>dynamicNamingFallbackName</b> A String value used as the fallbackName parameter for a created
 * {@link com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy}.
 * </li>
 *
 * <li>
 * <b>dynamicNamingRecognizedHosts</b> A String value used as the recognizedHosts parameter for a created
 * {@link com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy}.
 * </li>
 *
 * </ul>
 *
 * @throws ServletException
 *  when a segment naming strategy is not provided in constructor arguments nor in init-params.
 */
@Override
public void init(FilterConfig config) throws ServletException {
    String fixedName = config.getInitParameter("fixedName");
    String dynamicNamingFallbackName = config.getInitParameter("dynamicNamingFallbackName");
    String dynamicNamingRecognizedHosts = config.getInitParameter("dynamicNamingRecognizedHosts");
    if (StringValidator.isNotNullOrBlank(dynamicNamingFallbackName)) {
        if (StringValidator.isNotNullOrBlank(dynamicNamingRecognizedHosts)) {
            segmentNamingStrategy = new DynamicSegmentNamingStrategy(dynamicNamingFallbackName, dynamicNamingRecognizedHosts);
        } else {
            segmentNamingStrategy = new DynamicSegmentNamingStrategy(dynamicNamingFallbackName);
        }
    } else if (StringValidator.isNotNullOrBlank(fixedName)) {
        segmentNamingStrategy = new FixedSegmentNamingStrategy(fixedName);
    } else if (null == segmentNamingStrategy) {
        throw new ServletException("The AWSXRayServletFilter requires either a fixedName init-param or an instance of SegmentNamingStrategy. " + "Add an init-param tag to the AWSXRayServletFilter's declaration in web.xml, using param-name: 'fixedName'. " + "Alternatively, pass an instance of SegmentNamingStrategy to the AWSXRayServletFilter constructor.");
    }
}
Also used : FixedSegmentNamingStrategy(com.amazonaws.xray.strategy.FixedSegmentNamingStrategy) ServletException(javax.servlet.ServletException) DynamicSegmentNamingStrategy(com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy)

Aggregations

FixedSegmentNamingStrategy (com.amazonaws.xray.strategy.FixedSegmentNamingStrategy)3 DynamicSegmentNamingStrategy (com.amazonaws.xray.strategy.DynamicSegmentNamingStrategy)2 ServletException (javax.servlet.ServletException)2 AWSXRayRecorder (com.amazonaws.xray.AWSXRayRecorder)1 AsyncContext (javax.servlet.AsyncContext)1 AsyncEvent (javax.servlet.AsyncEvent)1 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.Test)1