Java - Brave / Zipkin:当前跨度为空

Java - Brave / Zipkin: Current Span Null

关于 Java 应用程序的小问题,请使用 Brave / Zipkin 进行跟踪。

我有一段非常简单的代码(附上代码+maven pom,准备运行重现问题)

import io.netty.handler.logging.LogLevel;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.logging.AdvancedByteBufFormat;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.brave.ZipkinSpanHandler;
import zipkin2.reporter.urlconnection.URLConnectionSender;
import brave.Tracing;
import brave.opentracing.BraveTracer;
import java.util.Map;

public class QuestionApplication {

    public static void main(String[] args) {
        final var sender        = URLConnectionSender.create("http://the-zipkin-instance.com:9411/api/v2/spans");
        final var spanReporter  = AsyncReporter.create(sender);
        final var tracing       = Tracing.newBuilder().localServiceName("thisIsTheClientApplicationWhoIsGeneratingTheFirstTraceId").addSpanHandler(ZipkinSpanHandler.create(spanReporter)).build();
        final var tracer        = BraveTracer.create(tracing).unwrap().tracer();
        final var webClient     = WebClient.create().mutate().clientConnector(new ReactorClientHttpConnector(HttpClient.create().wiretap("reactor.netty.http.client.HttpClient", LogLevel.INFO, AdvancedByteBufFormat.HEX_DUMP))).build();
        final var jsonPayload   = Map.of("key", "someKey", "value", "someValue");
        final var span = tracer.currentSpan();
        final var context = span.context();
        final var traceIdString = context.traceIdString();
        final var response      = webClient.post().uri("http://the-server.com/api/route").header("X-B3-TraceId", traceIdString).body(BodyInserters.fromValue(jsonPayload)).retrieve().bodyToMono(String.class).block();
        System.out.println(response);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github</groupId>
    <artifactId>question</artifactId>
    <version>1.1</version>

    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter2</groupId>
            <artifactId>zipkin-sender-urlconnection</artifactId>
            <version>2.16.3</version>
        </dependency>
        <dependency>
            <groupId>io.opentracing.brave</groupId>
            <artifactId>brave-opentracing</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

</project>

在这里,使用上面的代码,我在行 final var context = span.context(); 上得到一个 java.lang.NullPointerException,因为跨度为空 final var span = tracer.currentSpan();

本应用为“客户端”应用,即最先发起http调用,因此,我希望这一小段主代码最先生成trace ID,trace ID会在十几分钟内运行其他子系统。

请问这是什么问题,这个 NPE 的根本原因是什么,或者我应该说,创建要传递的跟踪 ID 的正确方法是什么,因为我是链中的第一个?

谢谢

我不知道你的 NPE 生成了什么(技术上),但考虑到 Tracing 来自 Brave,SpanHandler 注入和 Brave 包装器很奇怪。那么最有可能的是,在这两行中,您没有为创建 Spans 注入必要的依赖项。

Tracing.newBuilder().localServiceName("thisIsTheClientApplicationWhoIsGeneratingTheFirstTraceId").addSpanHandler(ZipkinSpanHandler.create(spanReporter)).build();
BraveTracer.create(tracing).unwrap().tracer();

这里我做了一个没有这些行的例子,它按你预期的那样工作。使用 parent-child 流结构,您可以通过注入和提取 headers 的跟踪来复制和分发它在其组件中:X-B3-TraceId、X-B3-ParentSpanId、X-B3-SpanId , X-B3-采样。

主要代码

package com.Whosebug.q69430794;

import brave.Span;
import brave.Tracing;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.okhttp3.OkHttpSender;

public class QuestionApplication {

    public static void main(String[] args) {
        
        var sender = OkHttpSender.newBuilder().endpoint("http://localhost:9411/api/v2/spans").build();
        var reporter = AsyncReporter.builder(sender).build();
        
        var tracing = Tracing.newBuilder().localServiceName("component-test").spanReporter(reporter).build();
        
        // Create parent span and his child --------------------
        
        // Parent
        tracing.tracer().startScopedSpan("parentSpan");
        Span span =  tracing.tracer().currentSpan();
        span.tag("key", "firstBiz");
        
        // Child
        tracing.tracer().startScopedSpanWithParent("childSpan", tracing.tracer().currentSpan().context());
        Span childSpan =  tracing.tracer().currentSpan();
        childSpan.tag("key", "secondBiz");
        childSpan.finish();
        System.out.println("id:" + childSpan.context().traceIdString());
        
        // Finish span
        span.finish();
        
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // -----------------------------------------------------
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.Whosebug.q69430794</groupId>
    <artifactId>69430794</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>69430794</name>
    <description>Question # 69430794</description>

    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave</artifactId>
            <version>5.4.2</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter2</groupId>
            <artifactId>zipkin-sender-okhttp3</artifactId>
            <version>2.7.9</version>
        </dependency>
        
    </dependencies>

</project>

我用了docker 图片:

docker pull openzipkin/zipkin

Zipkin中的踪迹