Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HandlerInterceptorSpanDecorator Not run #93

Closed
etsangsplk opened this issue Feb 19, 2019 · 1 comment
Closed

HandlerInterceptorSpanDecorator Not run #93

etsangsplk opened this issue Feb 19, 2019 · 1 comment

Comments

@etsangsplk
Copy link

I am using https://github.com/spring-projects/spring-petclinic as a simple test example to test out how to use the library. A jaeger Tracer is configured and created. I only added PetClinicApplicationConfiguration that implements WebMvcConfigurer tow wireup a tracer and and TracingHandlerInterceptor. When I run the app, I can see the log message "TracingHandlerInterceptor added". But when I bring up the web and click on the front end . I am seeing the any of the HandlerInterceptorSpanDecorator being run. No log messages are printed from inside the Span decorator. .Below are the two files:

PetClinicApplicationConfiguraton.java

package org.springframework.samples.petclinic;

import org.springframework.context.annotation.Configuration;
import org.springframework.samples.tracing.TracerConfiguration;
import org.springframework.samples.petclinic.PetClinicApplication;
import org.springframework.samples.tracing.TracingHandlerDecorator;

import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import io.opentracing.contrib.spring.web.interceptor.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class PetClinicApplicationConfiguration implements WebMvcConfigurer {
    private static Logger LOGGER = LogManager.getLogger();

    @Bean
    public Tracer tracer() {
        return (new TracerConfiguration()).getTracer(PetClinicApplication.SERVICE_NAME);
    }

    @Bean
    public List<HandlerInterceptorSpanDecorator> spanDecorators() {
        return Arrays.asList(new TracingHandlerDecorator());
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TracingHandlerInterceptor(GlobalTracer.get(), spanDecorators()));
        LOGGER.info("TracingHandlerInterceptor added");
    }

}

package org.springframework.samples.tracing;

import io.opentracing.Span;
import io.opentracing.contrib.spring.web.interceptor.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.bson.types.ObjectId;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

public class TracingHandlerDecorator implements HandlerInterceptorSpanDecorator {
private static Logger LOGGER = LogManager.getLogger();

@Override
public void onPreHandle(HttpServletRequest httpServletRequest,
                        Object handler,
                        Span span) {
    String metaData = HandlerUtils.methodName(handler);
    LOGGER.info("*************tracinghandledecorator***********");
    LOGGER.info("*******Global tracer*********** ={}", io.opentracing.util.GlobalTracer.get());
    if (metaData != null) {
        span.setOperationName(metaData);
        //SplunkUri uri = new SplunkUri(httpServletRequest.getRequestURI());
        //String tenantId = uri.getTenant();
        String requestId = getRequestId(httpServletRequest);
        span.setTag("requestId", requestId);
        //span.setTag("tenant", tenantId);
    }
}

@Override
public void onAfterCompletion(HttpServletRequest httpServletRequest,
                              HttpServletResponse httpServletResponse,
                              Object handler,
                              Exception ex,
                              Span span) {
    LOGGER.info("*************tracinghandledecorator ONAFTERCOMPLETION ***********");
}

@Override
public void onAfterConcurrentHandlingStarted(HttpServletRequest httpServletRequest,
                                             HttpServletResponse httpServletResponse,
                                             Object handler,
                                             Span span) {
    LOGGER.info("*************tracinghandledecorator AFTERCONCURRENTHANDLEING  STARTED***********");
}

private String getRequestId(HttpServletRequest request) {
    return Optional
        .ofNullable(request.getHeader("X-Request-Id"))
        .orElseGet(() -> ObjectId.get().toString());
}

/**
 * Helper class for deriving tags/logs from handler object.
 */
static class HandlerUtils {
    private HandlerUtils() {}

    /**
     * Class name of a handler serving request.
     */
    public static final String HANDLER_CLASS_NAME = "handler.class_simple_name";
    /**
     * Method name of handler serving request.
     */
    public static final String HANDLER_METHOD_NAME = "handler.method_name";
    /**
     * Spring handler object.
     */
    public static final String HANDLER = "handler";

    public static String methodName(Object handler) {
        return handler instanceof HandlerMethod
            ? ((HandlerMethod) handler).getMethod().getName() : null;
    }
}

}

@etsangsplk
Copy link
Author

Closing. FilterRegistrationBean has not been initialized properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant