2

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

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

Jaeger 的 Spring 配置:

@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 端口上本地运行。

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

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

但是,我的 Spring 应用程序不会显示在 Jaeger UI 中。一开始我想跟踪我的 REST 端点。有人可以指出正确的方向,为什么 UI 中缺少我的应用程序以及如何正确配置 Jaeger?是否有不依赖过时 Jaeger 的 Spring+Jaeger 示例项目?

4

2 回答 2

5

如果其他人想在 spring 项目中设置 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 以注册新的跟踪过滤器trackingFilter以拦截 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 tracer:

<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 中描述的trackingFilter bean:

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 spring 配置:

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);
        }
    }
}
于 2019-06-13T13:23:02.267 回答
0

我有以下 gradle 依赖项工作,

    implementation "io.opentracing.contrib:opentracing-spring-cloud-starter:0.1.13"
    implementation "io.jaegertracing:jaeger-client:0.31.0"

在跟踪 bean 配置之后,

  @Bean
    public io.opentracing.Tracer initTracer() {
        Configuration.SamplerConfiguration samplerConfig = new Configuration.SamplerConfiguration().withType("const").withParam(1);
        return Configuration.fromEnv("sprint-service").withSampler(samplerConfig).getTracer();
    }

然后跨度可以记录为

Span sprintSpan = tracer.buildSpan("my-span-name").withTag("player", player).start();

// some business functionality

sprintSpan.finish();

这是您可以参考的工作示例https://github.com/krushnatkhawale/jaeger-with-spring-boot-web-app

于 2020-07-24T23:41:06.740 回答