在Spring应用程序中配置Jaeger [英] Configuring Jaeger in Spring application

查看:124
本文介绍了在Spring应用程序中配置Jaeger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spring应用程序中配置Jaeger.我不知何故找不到合适的方法来做到这一点.几乎所有与Spring-Jaeger相关的文档都是针对Spring Boot的,其中大多数属性都是自动配置的.这是我的方法. Maven依赖项:

I would like to configure Jaeger in my Spring application. Somehow I cannot find a proper way to do this. Almost all Spring-Jaeger-related documentation is for Spring Boot where most of the properties are auto configured. Here's my approach. Maven dependency:

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

Jaeger的Spring配置:

Spring config for 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在docker的本地端口6831上运行.

Jaeger is running locally in docker on port 6831.

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

一旦我的应用程序启动,我注意到该应用程序的运行速度大大降低,我认为这是因为LoggingReporter将大量指标记录到控制台中.

Once my application starts, I noticed that application slows down considerably, I assume that is because of metrics logged heavily to console by LoggingReporter.

但是,我的Spring应用程序未显示在Jaeger UI中.首先,我想跟踪我的REST端点.有人可以向我指出正确的方向,为什么我的应用程序从UI中丢失以及我如何正确配置Jaeger? Spring + Jaeger可能有一个不依赖于过时的Jaeger的示例项目吗?

However, My Spring app does not show up in Jaeger UI. In the beginning I would like to trace my REST endpoints. Can someone point me in the right direction why my app is missing from UI and how I configure Jaeger properly? Is there perhaps a sample project with Spring+Jaeger that does not rely on outdated Jaeger?

推荐答案

如果其他人想在Spring项目中设置Jaeger,我会这样做:

If anyone else would like to set up Jaeger in spring project, here's what I did:

将依赖项添加到pom:

Add dependencies to 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:

Set up you web.xml to register new tracing filter tracingFilter to intercept 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>

在spring mvc中注册jaeger跟踪器:

Register jaeger tracer in spring 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>

设置我们在web.xml中描述的 tracingFilter bean:

Set up the tracingFilter bean we described in 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弹簧配置:

Finally define jaeger tracer spring configuration:

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);
        }
    }
}

这篇关于在Spring应用程序中配置Jaeger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆