在使用链路追踪控制台追踪应用的链路数据之前,需要通过客户端将应用数据上报至链路追踪。本文介绍如何通过Open Telemetry SDK进行应用埋点,并通过Jaeger Export上报链路数据。

前提条件

  1. 登录链路追踪控制台,在左侧导航栏,单击集群配置
  2. 集群配置页面上单击接入点信息页签,在集群信息区域打开显示Token开关。
  3. 客户端采集工具区域单击需要使用的链路数据采集客户端。
  4. 在下方表格的相关信息列中,单击接入点信息末尾的复制图标。

Tracing Analysis Endpoint Section

说明:如果应用部署于阿里云生产环境,则选择私网接入点,否则选择公网接入点。对于Zipkin,一般情况下请使用v2版接入点,v1版接入点仅限对Zipkin十分了解的高阶用户使用。

操作步骤

  1. 打开pom.xml文件,添加Jar包依赖。
    <io.opentelemetry.version>0.12.0</io.opentelemetry.version>
    ...
    <dependency>
                <groupId>io.opentelemetry</groupId>
                <artifactId>opentelemetry-api</artifactId>
                <version>${io.opentelemetry.version}</version>
            </dependency>
    
            <dependency>
                <groupId>io.opentelemetry</groupId>
                <artifactId>opentelemetry-sdk-metrics</artifactId>
                <version>${io.opentelemetry.version}</version>
            </dependency>
    
            <dependency>
                <groupId>io.opentelemetry</groupId>
                <artifactId>opentelemetry-sdk-trace</artifactId>
                <version>${io.opentelemetry.version}</version>
            </dependency>
    
            <dependency>
                <groupId>io.opentelemetry</groupId>
                <artifactId>opentelemetry-sdk</artifactId>
                <version>${io.opentelemetry.version}</version>
            </dependency>
    
            <dependency>
                <groupId>io.opentelemetry</groupId>
                <artifactId>opentelemetry-exporter-jaeger</artifactId>
                <version>${io.opentelemetry.version}</version>
            </dependency>
  2. 通过Jaeger Export上报数据。
    使用initTracer()参数初始化Tracer对象,使用myWonderfulUseCase()参数通过Tracer对象生成Span来进行代码埋点。
    import io.grpc.*;
    import io.opentelemetry.api.trace.Span;
    import io.opentelemetry.api.trace.Tracer;
    import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
    import io.opentelemetry.sdk.OpenTelemetrySdk;
    import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
    
    public class OpenTelemetryExample {
        // OTel API
        private Tracer tracer = null;
    
    
        private void initTracer(String name, int port, String auth) {
            // Create a channel towards Jaeger end point
            ManagedChannel jaegerChannel = ManagedChannelBuilder.forAddress(name, port).intercept(new ClientInterceptor() {
                @Override
                public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
                    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
                            channel.newCall(methodDescriptor, callOptions)) {
                        @Override
                        public void start(Listener<RespT> responseListener, final Metadata headers) {
                            Metadata.Key<String> headerKey = Metadata.Key.of("Authentication", Metadata.ASCII_STRING_MARSHALLER);
                            headers.put(headerKey, auth);
                            super.start(responseListener, headers);
                        }
                    };
                }
            }).usePlaintext().build();
    
            // Export traces to Jaeger
            JaegerGrpcSpanExporter jaegerExporter =
                    JaegerGrpcSpanExporter.builder()
                            .setServiceName("otel-jaeger-example")
                            .setChannel(jaegerChannel)
                            .setDeadlineMs(30000)
                            .build();
    
            // Set to process the spans by the Jaeger Exporter
            OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().build();
            openTelemetry
                    .getTracerManagement()
                    .addSpanProcessor(SimpleSpanProcessor.builder(jaegerExporter).build());
    
    
            openTelemetry.getTracerManagement();
            tracer = openTelemetry.getTracer("io.opentelemetry.example.JaegerExample");
    
        }
    
        private void myWonderfulUseCase() {
            // Generate a span
            Span span = this.tracer.spanBuilder("Start my wonderful use case").startSpan();
            span.addEvent("Event 0");
            // execute my use case - here we simulate a wait
            doWork();
            span.addEvent("Event 1");
            span.end();
        }
    
        private void doWork() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    
        public static void main(String[] args) {
    
            OpenTelemetryExample example = new OpenTelemetryExample();
            // please replease token from "http://tracing.console.aliyun.com/"
            example.initTracer("tracing-analysis-dc-bj.aliyuncs.com", 1883, "123@0yourToken_123@0yourToken");
            example.myWonderfulUseCase();
            // wait some seconds
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }
            System.out.println("Bye");
        }
    }
  3. 下载[Demo]工程,并将工程中的Token信息替换为前提条件中获取的Token。
  4. 执行以下命令运行Demo。
    mvn clean compile exec:java -Dexec.mainClass=com.demo.OpenTelemetryExample

查看数据

登录ARMS控制台后,在链路追踪应用列表页面选择新创建的应用(本示例中的应用为“otel-jaeger-example“),查看链路数据。

更多信息

不是您要找的文档?鼠标悬浮在这里试一试。