io.opentracing.contrib.web.servlet.filter.TracingFilter Java Examples

The following examples show how to use io.opentracing.contrib.web.servlet.filter.TracingFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServerTracingAutoConfiguration.java    From java-spring-web with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(TracingFilter.class)
public WebMvcConfigurer tracingHandlerInterceptor(final Tracer tracer) {
    log.info("Creating " + WebMvcConfigurer.class.getSimpleName() + " bean with " +
            TracingHandlerInterceptor.class);

    return new WebMvcConfigurer() {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            List<HandlerInterceptorSpanDecorator> decorators = interceptorSpanDecorator.getIfAvailable();
            if (CollectionUtils.isEmpty(decorators)) {
                decorators = Arrays.asList(HandlerInterceptorSpanDecorator.STANDARD_LOGS,
                        HandlerInterceptorSpanDecorator.HANDLER_METHOD_OPERATION_NAME);
            }

            registry.addInterceptor(new TracingHandlerInterceptor(tracer, decorators));
        }
    };
}
 
Example #2
Source File: OpenTracingInitializer.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();

    String skipPatternAttribute = servletContext.getInitParameter(TracingFilter.SKIP_PATTERN);
    if (null != skipPatternAttribute && !skipPatternAttribute.isEmpty()) {
        servletContext.setAttribute(TracingFilter.SKIP_PATTERN, Pattern.compile(skipPatternAttribute));
    }

    logger.info("Registering Tracing Filter");
    Dynamic filterRegistration = servletContext
        .addFilter("tracingFilter", new TracingFilter());
    filterRegistration.setAsyncSupported(true);
    filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
}
 
Example #3
Source File: StandardSpanDecoratorTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Bean
@SuppressWarnings({"rawtypes", "unchecked"}) // generic as of Spring Boot 2
public FilterRegistrationBean tracingFilter(
        final Tracer tracer,
        final List<ServletFilterSpanDecorator> decorators) {

    final TracingFilter filter = new TracingFilter(tracer, decorators, null);
    final FilterRegistrationBean bean = new FilterRegistrationBean(filter);
    bean.setAsyncSupported(true);

    return bean;
}
 
Example #4
Source File: OpenTracingServletExtensionAutoConfigurationTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Bean
@SuppressWarnings({"rawtypes", "unchecked"}) // generic as of Spring Boot 2
public FilterRegistrationBean tracingFilter(
        final Tracer tracer,
        final List<ServletFilterSpanDecorator> decorators) {

    final TracingFilter filter = new TracingFilter(tracer, decorators, null);
    final FilterRegistrationBean bean = new FilterRegistrationBean(filter);
    bean.setAsyncSupported(true);

    return bean;
}
 
Example #5
Source File: OpenTracingWebExtensionAutoConfigurationTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Bean
@SuppressWarnings({"rawtypes", "unchecked"}) // generic as of Spring Boot 2
public FilterRegistrationBean tracingFilter(
        final Tracer tracer) {

    final TracingFilter filter = new TracingFilter(tracer, emptyList(), null);
    final FilterRegistrationBean bean = new FilterRegistrationBean(filter);
    bean.setAsyncSupported(true);

    return bean;
}
 
Example #6
Source File: StandardSpanDecoratorTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Bean
@SuppressWarnings({"rawtypes", "unchecked"}) // generic as of Spring Boot 2
public FilterRegistrationBean tracingFilter(
        final Tracer tracer) {

    final TracingFilter filter = new TracingFilter(tracer, emptyList(), null);
    final FilterRegistrationBean bean = new FilterRegistrationBean(filter);
    bean.setAsyncSupported(true);

    return bean;
}
 
Example #7
Source File: JettyExtension.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
JettyExtension(final Filter filter, final Tracer tracer, final Flow flow) {
    final ServletContextHandler handler = new ServletContextHandler();
    handler.setContextPath("/");
    handler.addServlet(new ServletHolder(new TraceServlet(flow)), "/trace");

    handler.addFilter(new FilterHolder(new TracingFilter(tracer)), "/trace", EnumSet.allOf(DispatcherType.class));

    // /untraced is intentionally NOT traced!
    handler.addFilter(new FilterHolder(filter), "/trace", EnumSet.allOf(DispatcherType.class));

    server.setHandler(handler);
}
 
Example #8
Source File: OpenTracingInstaller.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void process() throws Exception {
    logger.info("Determining whether to install OpenTracing integration or not.");
    if (archive.getName().endsWith(".war")) {
        logger.logf(Logger.Level.INFO, "Installing the OpenTracing integration for the deployment %s", archive.getName());
        WARArchive webArchive = archive.as(WARArchive.class);
        WebXmlAsset webXml = webArchive.findWebXmlAsset();

        logger.logf(Logger.Level.INFO, "Adding the listener org.wildfly.swarm.opentracing.deployment.OpenTracingInitializer");
        webXml.addListener("org.wildfly.swarm.opentracing.deployment.OpenTracingInitializer");

        setContextParamIfNotNull(webXml, TracingFilter.SKIP_PATTERN, fraction.getServletSkipPattern());
    }
}
 
Example #9
Source File: ContextAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static Object getAddFilterMethod(final ServletContext context) throws IllegalAccessException, InvocationTargetException, ServletException {
  if (servletContextToFilter.containsKey(context)) {
    if (logger.isLoggable(Level.FINER))
      logger.finer(">< ContextAgentIntercept#addFilter(" + AgentRuleUtil.getSimpleNameId(context) + "): hasFilter(context) == true");

    return null;
  }

  final Method addFilterMethod = getFilterMethod(context);
  if (addFilterMethod == null) {
    if (logger.isLoggable(Level.FINER))
      logger.finer(">< ContextAgentIntercept#addFilter(" + AgentRuleUtil.getSimpleNameId(context) + "): ServletContext#addFilter(String,Filter) is missing");

    return null;
  }

  final TracingFilter tracingFilter = getFilter(context, false);
  // If the tracingFilter instance is a TracingProxyFilter, then it was
  // created with ServletFilterAgentIntercept#getProxyFilter. This should
  // never happen, because ServletContext#addFilter happens first in the
  // servlet lifecycle.
  if (tracingFilter instanceof TracingProxyFilter) {
    if (logger.isLoggable(Level.FINER))
      logger.finer(">< ContextAgentIntercept#addFilter(" + AgentRuleUtil.getSimpleNameId(context) + "): tracingFilter instanceof TracingProxyFilter");

    return null;
  }

  if (logger.isLoggable(Level.FINER))
    logger.finer(">> ContextAgentIntercept#addFilter(" + AgentRuleUtil.getSimpleNameId(context) + "): ServletContext#addFilter(\"" + TRACING_FILTER_NAME + "\"," + AgentRuleUtil.getSimpleNameId(tracingFilter) + ")");

  return addFilterMethod.invoke(context, TRACING_FILTER_NAME, tracingFilter);
}
 
Example #10
Source File: TracingConfiguration.java    From hola with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    FilterRegistration.Dynamic filterRegistration = sce.getServletContext()
            .addFilter("BraveServletFilter", new TracingFilter(tracer));
    // Explicit mapping to avoid trace on readiness probe
    filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/api/hola", "/api/hola-chaining");
}
 
Example #11
Source File: TracingHandlerInterceptorTest.java    From java-spring-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoSpan() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getAttribute(TracingFilter.SERVER_SPAN_CONTEXT)).thenReturn(null);

    TracingHandlerInterceptor interceptor = new TracingHandlerInterceptor(null);
    assertTrue(interceptor.preHandle(request, null, null));
    interceptor.afterConcurrentHandlingStarted(request, null, null);
    interceptor.afterCompletion(request, null, null, null);
}
 
Example #12
Source File: ServletAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void serviceEnter(final Object thiz, final Object req, final Object res) {
  try {
    final ServletContext context = getServletContext((HttpServlet)thiz);
    if (context == null)
      return;

    final TracingFilter tracingFilter = getFilter(context, true);

    // If the tracingFilter instance is not a TracingProxyFilter, then it was
    // created with ServletContext#addFilter. Therefore, the intercept of the
    // Filter#doFilter method is not necessary.
    if (!(tracingFilter instanceof TracingProxyFilter))
      return;

    // If `servletRequestToState` contains the request key, then this request
    // has been handled by doFilter
    final HttpServletRequest request = (HttpServletRequest)req;
    if (request.getAttribute(TracingFilter.SERVER_SPAN_CONTEXT) != null)
      return;

    if (LocalSpanContext.get(COMPONENT_NAME) != null)
      return;

    if (!Configuration.isTraced(request))
      return;

    final Tracer tracer = GlobalTracer.get();
    final Span span = TracingFilterUtil.buildSpan(request, tracer, spanDecorators);
    LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span));
    if (logger.isLoggable(Level.FINER))
      logger.finer("<< ServletAgentIntercept#service(" + AgentRuleUtil.getSimpleNameId(req) + "," + AgentRuleUtil.getSimpleNameId(res) + "," + AgentRuleUtil.getSimpleNameId(context) + ")");
  }
  catch (final Exception e) {
    logger.log(Level.WARNING, e.getMessage(), e);
  }
}
 
Example #13
Source File: ServletFilterAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static TracingFilter getFilter(final ServletContext context, final boolean proxy) throws ServletException {
  Objects.requireNonNull(context);
  TracingFilter filter = servletContextToFilter.get(context);
  if (filter != null)
    return filter;

  synchronized (servletContextToFilter) {
    filter = servletContextToFilter.get(context);
    if (filter != null)
      return filter;

    servletContextToFilter.put(context, filter = proxy ? new TracingProxyFilter(GlobalTracer.get(), context) : new TracingFilter(GlobalTracer.get(), Configuration.spanDecorators, Configuration.skipPattern));
    return filter;
  }
}
 
Example #14
Source File: SpringMVCConfiguration.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    sce.getServletContext().setAttribute(TracingFilter.SPAN_DECORATORS,
            Collections.singletonList(ServletFilterSpanDecorator.STANDARD_TAGS));
    sce.getServletContext().setAttribute(TracingFilter.SKIP_PATTERN, Pattern.compile("/health"));
}
 
Example #15
Source File: TracingFilterProvider.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Inject
public TracingFilterProvider(Tracer tracer) {
  filter = new TracingFilter(tracer);
}
 
Example #16
Source File: TracingFilterProvider.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public TracingFilter get() {
  return filter;
}
 
Example #17
Source File: TracingWebModule.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void configureServlets() {
  // tracing
  bind(TracingFilter.class).toProvider(TracingFilterProvider.class).in(Singleton.class);
  filter("/*").through(TracingFilter.class);
}
 
Example #18
Source File: TracingHandlerInterceptorTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testIsNotTraced() {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getAttribute(TracingFilter.SERVER_SPAN_CONTEXT)).thenReturn(null);
    assertFalse(TracingHandlerInterceptor.isTraced(request));
}
 
Example #19
Source File: TracingHandlerInterceptorTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testIsTraced() {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getAttribute(TracingFilter.SERVER_SPAN_CONTEXT)).thenReturn(Mockito.mock(SpanContext.class));
    assertTrue(TracingHandlerInterceptor.isTraced(request));
}
 
Example #20
Source File: TracingHandlerInterceptor.java    From java-spring-web with Apache License 2.0 2 votes vote down vote up
/**
 * This method determines whether the HTTP request is being traced.
 *
 * @param httpServletRequest The HTTP request
 * @return Whether the request is being traced
 */
static boolean isTraced(HttpServletRequest httpServletRequest) {
    // exclude pattern, span is not started in filter
    return httpServletRequest.getAttribute(TracingFilter.SERVER_SPAN_CONTEXT) instanceof SpanContext;
}