You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vert.x中实现Zipkin链路追踪?

Integrating Zipkin with Vert.x Applications

Great question! Integrating Zipkin with Vert.x is totally achievable using Vert.x's official OpenTracing support paired with the Zipkin OpenTracing bridge. Unlike Spring Boot's auto-config approach, Vert.x requires a bit of manual setup but it's straightforward. Here's how to do it:

1. Add Required Dependencies

First, you'll need two key dependencies: Vert.x's OpenTracing integration and the Zipkin OpenTracing bridge. If you're using Maven, add these to your pom.xml:

<!-- Vert.x OpenTracing core integration -->
<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-opentracing</artifactId>
  <version>YOUR_VERTX_VERSION</version> <!-- Match your app's Vert.x version (e.g., 4.5.1) -->
</dependency>

<!-- Zipkin OpenTracing bridge -->
<dependency>
  <groupId>io.opentracing.contrib</groupId>
  <artifactId>opentracing-zipkin</artifactId>
  <version>0.33.2</version> <!-- Use the latest stable version -->
</dependency>

For Gradle users, add these to your build.gradle:

implementation 'io.vertx:vertx-opentracing:YOUR_VERTX_VERSION'
implementation 'io.opentracing.contrib:opentracing-zipkin:0.33.2'

2. Initialize Zipkin Tracer & Bind to Vert.x

Next, you need to create a Zipkin Tracer instance and configure Vert.x to use it for tracing. This is typically done when starting your Vert.x application:

import io.opentracing.Tracer;
import io.opentracing.contrib.zipkin.ZipkinTracer;
import io.opentracing.util.GlobalTracer;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.opentracing.OpenTracingOptions;
import io.vertx.core.AbstractVerticle;

public class MainVerticle extends AbstractVerticle {

    @Override
    public void start() {
        // 1. Build the Zipkin Tracer
        Tracer zipkinTracer = ZipkinTracer.newBuilder()
                .serviceName("your-vertx-service-name") // Name of your Vert.x service (shows in Zipkin UI)
                .reportingEndpoint("http://YOUR_ZIPKIN_HOST:9411/api/v2/spans") // Your Zipkin server endpoint
                .sampler(io.opentracing.contrib.zipkin.Sampler.create(1.0)) // 100% sampling rate (adjust as needed)
                .build();

        // 2. Register tracer with GlobalTracer (optional but simplifies access elsewhere)
        GlobalTracer.registerIfAbsent(zipkinTracer);

        // 3. Configure Vert.x to use the Zipkin tracer
        VertxOptions vertxOptions = new VertxOptions()
                .setTracingOptions(new OpenTracingOptions(zipkinTracer));

        // 4. Initialize Vert.x with tracing enabled
        Vertx vertx = Vertx.vertx(vertxOptions);

        // Deploy your business verticles
        vertx.deployVerticle(new YourBusinessVerticle());
    }
}

3. Automatic Tracing for Built-in Vert.x Components

The good news is that many Vert.x components are automatically traced once you've set up the tracer:

  • Vert.x Web HTTP Servers: Incoming HTTP requests will automatically generate a root span, including details like HTTP method, path, and response status.
  • Vert.x HTTP Clients: Outgoing HTTP calls will create child spans, with trace IDs propagated to downstream services (if they support OpenTracing/Zipkin).
  • Vert.x Event Bus: Messages sent over the event bus will carry trace context, enabling distributed tracing across verticles or cluster nodes.

4. Manual Tracing for Custom Business Logic

For custom operations (like database queries, complex computations, or business workflows), you can manually create child spans to add granular tracing:

import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;
import io.vertx.core.Context;

// Inside your business verticle or handler
Context currentContext = vertx.getOrCreateContext();

// Create a child span linked to the current request's root span
Span customSpan = GlobalTracer.get().buildSpan("process-payment")
        .asChildOf(currentContext.getLocal("opentracing-span"))
        .start();

try {
    // Your custom business logic here
    processPaymentOrder(orderId);
} finally {
    // Always finish the span to record it
    customSpan.finish();
}

5. Troubleshooting Tips

  • No spans showing up in Zipkin? Double-check the reportingEndpoint URL (ensure Zipkin is running and reachable), and verify your service name is correctly set.
  • Dependency version conflicts? Make sure your vertx-opentracing version matches your Vert.x core version. For Vert.x 4.x, use vertx-opentracing 4.x; for 3.x, use 3.x.
  • Sampling issues? Adjust the sampler rate (e.g., 0.5 for 50% sampling) if you're generating too many spans, or set it to 1.0 for full sampling during debugging.

内容的提问来源于stack exchange,提问作者xmlParser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:41:08