aws/aws-xray-sdk-java

Failed to begin subsegment in Spring Filters

ramkumar-mn opened this issue · 2 comments

Getting following exception while making a http call in Spring OncePerRequestFilter class.
However, the same call working fine in the controller and services classes.

Please let me know what can be done to trace a http call in the filter classes.

Attempted to find context at:
  java.lang.RuntimeException: Failed to begin subsegment named 'app.com: segment cannot be found.
  at com.amazonaws.xray.strategy.LogErrorContextMissingStrategy.contextMissing(LogErrorContextMissingStrategy.java:36)
  at com.amazonaws.xray.contexts.ThreadLocalSegmentContext.beginSubsegment(ThreadLocalSegmentContext.java:40)
  at com.amazonaws.xray.AWSXRayRecorder.beginSubsegment(AWSXRayRecorder.java:614)
  at com.amazonaws.xray.proxies.apache.http.TracedHttpClient.execute(TracedHttpClient.java:178)
  at com.amazonaws.xray.proxies.apache.http.TracedHttpClient.execute(TracedHttpClient.java:42)
  at feign.httpclient.ApacheHttpClient.execute(ApacheHttpClient.java:83)
  at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119)
  at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
  at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
  at com.sun.proxy.$Proxy122.validate(Unknown Source)
  at com.sample.app.ValildateFilter.doFilterInternal(ValildateFilter.java:45)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)

Hi @ramkumar-mn,

This error means you are making an instrumented HTTP request before you've begun a segment, meaning a subsegment is created to represent the HTTP request but has no segment to parent to. If you've done our Spring instrumentation, it's done via our own servlet filter, so I'm guessing this problematic HTTP request is happening before our filter is started (and hence there is not yet a parent segment in the context). There are a few solutions:

  1. Position the X-Ray servlet filter earlier in the request handling chain so the segment is started before the HTTP call is made
  2. Do not instrument the Apache client used to make the HTTP client, so it does not attempt to create a subsegment
  3. Set the AWS_XRAY_CONTEXT_MISSING environment variable to IGNORE_ERROR, in which case this problem will still occur but nothing will be logged.

Thanks @willarmiros. It helped.