Настройка приложения Jaeger in Spring - PullRequest
0 голосов
/ 20 марта 2019

Я бы хотел настроить Jaeger в своем приложении Spring. Почему-то я не могу найти правильный способ сделать это. Почти вся связанная с Spring-Jaeger документация относится к Spring Boot, где большинство свойств настраиваются автоматически. Вот мой подход. Зависимость Maven:

    <dependency>
        <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-spring-jaeger-cloud-starter</artifactId>
        <version>1.0.3</version>
    </dependency>

Конфигурация Spring для Jaeger:

@Configuration
public class JagerConfiguration {


    @Bean
    public io.opentracing.Tracer jaegerTracer() {
        Map<String, String> tags = new HashMap<>();
        tags.put(Constants.TRACER_HOSTNAME_TAG_KEY, "localhost");

        CompositeReporter reporter = new CompositeReporter(new LoggingReporter(), remoteReporter());


        return new Builder("myTestSpringApp")
                .withSampler(new ConstSampler(true))
                .withMetricsFactory(new InMemoryMetricsFactory())
                .withReporter(remoteReporter())
                .withTags(tags)
                .build();
    }

    @Bean
    public RemoteReporter remoteReporter() {
        return new RemoteReporter.Builder().withSender(new UdpSender("localhost", 6831, 0)).build();
    }
}

Jaeger работает локально в докере через порт 6831.

docker run -d -p6831:6831/udp -p16686:16686 jaegertracing/all-in-one:latest

Как только мое приложение запустилось, я заметил, что приложение значительно замедляется, я полагаю, это из-за того, что метрики интенсивно записываются на консоль LoggingReporter.

Однако приложение My Spring не отображается в пользовательском интерфейсе Jaeger. В начале я хотел бы отследить мои конечные точки REST. Может ли кто-нибудь указать мне правильное направление, почему мое приложение отсутствует в пользовательском интерфейсе и как правильно настроить Jaeger? Возможно, есть пример проекта со Spring + Jaeger, который не опирается на устаревший Jaeger?

1 Ответ

0 голосов
/ 13 июня 2019

Если кто-то еще хочет настроить Jaeger в весеннем проекте, вот что я сделал:

Добавление зависимостей в pom:

<properties>
    <opentracing.spring.web.version>0.3.4</opentracing.spring.web.version>
    <opentracing.jdbc.version>0.0.12</opentracing.jdbc.version>
    <opentracing.spring.configuration.starter.version>0.1.0</opentracing.spring.configuration.starter.version>
    <opentracing.spring.jaeger.starter.version>0.2.2</opentracing.spring.jaeger.starter.version>
</properties>

<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-spring-web-starter</artifactId>
    <version>${opentracing.spring.web.version}</version>
</dependency>
<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-jdbc</artifactId>
    <version>${opentracing.jdbc.version}</version>
</dependency>
<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-spring-tracer-configuration-starter</artifactId>
    <version>${opentracing.spring.configuration.starter.version}</version>
</dependency>
<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-spring-jaeger-starter</artifactId>
    <version>${opentracing.spring.jaeger.starter.version}</version>
</dependency>

Настройка вашего web.xml для регистрации новоготрассировочный фильтр tracingFilter для перехвата REST API:

<filter>
    <filter-name>tracingFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>tracingFilter</filter-name>
    <url-pattern>/api/*</url-pattern>
</filter-mapping>

Зарегистрируйте jaeger tracer весной mvc:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="io.opentracing.contrib.spring.web.interceptor.TracingHandlerInterceptor">
            <constructor-arg ref="jaegerTracer" />
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

Настройте bean-компонент tracingFilter мы описали в web.xml:

import io.opentracing.Tracer;
import io.opentracing.contrib.web.servlet.filter.TracingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class JaegerFilterConfiguration {

    @Bean
    public TracingFilter tracingFilter(Tracer tracer) {
        return new TracingFilter(tracer);
    }
}

Окончательно определим конфигурацию пружины jaeger tracer:

import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.metrics.NoopMetricsFactory;
import io.jaegertracing.internal.reporters.RemoteReporter;
import io.jaegertracing.internal.reporters.RemoteReporter.Builder;
import io.jaegertracing.internal.samplers.ProbabilisticSampler;
import io.jaegertracing.thrift.internal.senders.UdpSender;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.event.ContextRefreshedEvent;


@Configuration
public class JaegerConfiguration implements ApplicationListener<ContextRefreshedEvent> {


    private static final int JAEGER_PORT = 6831;
    private static final String JAEGER_HOST = "localhost";
    private static final String JAEGER_SERVICE_NAME = "my-awesome-jaeger";
    private static final double SAMPLING_RATE = 0.5;
    @Autowired
    private Tracer tracer;

    @Bean
    @Primary
    public Tracer jaegerTracer(RemoteReporter remoteReporter) {
        return new JaegerTracer.Builder(JAEGER_SERVICE_NAME)
                .withReporter(remoteReporter)
                .withMetricsFactory(new NoopMetricsFactory()).withSampler(new ProbabilisticSampler(SAMPLING_RATE))
                .build();
    }

    @Bean
    public RemoteReporter remoteReporter() {
        return new Builder().withSender(new UdpSender(JAEGER_HOST, JAEGER_PORT, 0)).build();
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!GlobalTracer.isRegistered()) {
            GlobalTracer.register(tracer);
        }
    }
}
...