Search in sources :

Example 1 with GlobalTracer

use of io.opentracing.util.GlobalTracer in project wildfly-swarm by wildfly-swarm.

the class OpenTracingInitializer method contextInitialized.

@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, "*");
    String skipParameter = servletContext.getInitParameter("skipOpenTracingResolver");
    if (skipParameter != null && Boolean.parseBoolean(skipParameter)) {
        logger.info("OpenTracing automatic resolution is being explicitly skipped.");
        return;
    }
    if (GlobalTracer.isRegistered()) {
        logger.info("A Tracer is already registered at the GlobalTracer. Skipping resolution via TraceResolver.");
        return;
    }
    Tracer tracer = TracerResolver.resolveTracer();
    if (null == tracer) {
        logger.info("Could not get a valid OpenTracing Tracer from the classpath. Skipping.");
        return;
    }
    logger.info(String.format("Registering %s as the OpenTracing Tracer", tracer.getClass().getName()));
    GlobalTracer.register(tracer);
}
Also used : TracingFilter(io.opentracing.contrib.web.servlet.filter.TracingFilter) Dynamic(javax.servlet.FilterRegistration.Dynamic) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) ServletContext(javax.servlet.ServletContext)

Example 2 with GlobalTracer

use of io.opentracing.util.GlobalTracer in project java-spring-web by opentracing-contrib.

the class TracerAutoConfiguration method getTracer.

/**
 * This method provides tracer if user did not specify any tracer bean.
 * <p>
 * The order of getting the tracer is:
 * <ol>
 *     <li>Tracer registered in {@link GlobalTracer#register(Tracer)}</li>
 *     <li>Tracer resolved from {@link TracerResolver#resolve()}</li>
 *     <li>Default tracer, which is {@link io.opentracing.NoopTracer}</li>
 * </ol>
 * @return tracer
 */
@Bean
@ConditionalOnMissingBean(Tracer.class)
public Tracer getTracer() {
    Tracer tracer;
    if (GlobalTracer.isRegistered()) {
        log.warn("GlobalTracer is already registered. For consistency it is best practice to provide " + "a Tracer bean instead of manually registering it with the GlobalTracer");
        tracer = GlobalTracer.get();
    } else {
        tracer = TracerResolver.resolveTracer();
        if (tracer == null) {
            // WARNING: Don't return GlobalTracer.get() as this will result in a
            // stack overflow if the returned tracer is subsequently wrapped by a
            // BeanPostProcessor. The post processed tracer would then be registered
            // with the {@link GlobalTracer) (via the {@link TracerRegisterAutoConfiguration})
            // resulting in the wrapper both wrapping the GlobalTracer, as well as being
            // the tracer used by the GlobalTracer.
            tracer = NoopTracerFactory.create();
        }
    }
    log.warn("Tracer bean is not configured! Switching to " + tracer);
    return tracer;
}
Also used : Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

Tracer (io.opentracing.Tracer)2 GlobalTracer (io.opentracing.util.GlobalTracer)2 TracingFilter (io.opentracing.contrib.web.servlet.filter.TracingFilter)1 Dynamic (javax.servlet.FilterRegistration.Dynamic)1 ServletContext (javax.servlet.ServletContext)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1