其他分享
首页 > 其他分享> > 极简!一个注解就能创建Jaeger的Span

极简!一个注解就能创建Jaeger的Span

作者:互联网

欢迎访问我的GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;

本篇概览

在这里插入图片描述

在这里插入图片描述

  1. 新建web工程,里面有controller层,调用service层的服务Biz,服务Biz再调用另一个服务ChildBiz
  2. 创建两个注解MySpanMyChildSpan
  3. 创建AOP类SpanAspect,负责处理所有被注解MySpanMyChildSpan修饰的方法;
  4. 把注解MySpanMyChildSpan用在普通的服务Biz和ChildBiz上
  5. 启动服务,验证功能;

源码下载

名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议

在这里插入图片描述

在这里插入图片描述

编码

<?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">
    <parent>
        <artifactId>spring-cloud-tutorials</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jaeger-annonation-demo</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                    </layers>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
spring:
  application:
    name: jaeger-annonation-demo
opentracing:
  jaeger:
    enabled: true
    udp-sender:
      host: 127.0.0.1
      port: 6831
server:
  port: 18080
package com.bolingcavalry.annonation;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AnnonationDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(AnnonationDemoApplication.class, args);
    }
}
package com.bolingcavalry.annonation.service;

public interface ChildBiz {
    void mockChild();
}
package com.bolingcavalry.annonation.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ChildBizImpl implements ChildBiz {
    @Override
    public void mockChild() {
        log.info("mockChild");
    }
}
package com.bolingcavalry.annonation.service;

public interface Biz {
    void mock();
}
package com.bolingcavalry.annonation.service.impl;

import com.bolingcavalry.annonation.service.Biz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class BizImpl implements Biz {

    final ChildBiz childBiz;

    public BizImpl(ChildBiz childBiz) {
        this.childBiz = childBiz;
    }

    @Override
    public void mock() {
      log.info("mock");
      childBiz.mockChild();
    }
}

定义注解

package com.bolingcavalry.annonation.aop;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface MySpan {
    String spanName() default "";
}
package com.bolingcavalry.annonation.aop;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface MyChildSpan {
    String spanName() default "";
}

AOP和注解开发

package com.bolingcavalry.annonation.aop;

import io.opentracing.Span;
import io.opentracing.Tracer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class SpanAspect {

    private Tracer tracer;

    public SpanAspect(Tracer tracer) {
        this.tracer = tracer;
    }

    /**
     * 取得真实方法的相关信息
     * @param proceedingJoinPoint
     * @return
     */
    private static String getOperationDesc(ProceedingJoinPoint proceedingJoinPoint) {
        Signature signature = proceedingJoinPoint.getSignature();
        // 提取类名
        return StringUtils.substringAfterLast(signature.getDeclaringTypeName(), ".")
                + ":"
                + signature.getName();
    }

    @Around("@annotation(mySpan)")
    public Object traceSpan(ProceedingJoinPoint proceedingJoinPoint, MySpan mySpan) throws Throwable {

        // 类名:方法名
        String operationDesc = getOperationDesc(proceedingJoinPoint);

        // 看方法的注解中有没有设置name
        String name = mySpan.spanName();

        // 如果没有设置name,就给span一个默认name
        if (StringUtils.isEmpty(name)) {
            name = "span-aspect-" + operationDesc;
        }

        // 创建一个span,在创建的时候就添加一个tag
        Span span = tracer.buildSpan(name).start();

        // span日志
        span.log("span log of " + operationDesc);

        // 增加一个tag
        span.setTag("operation-desc", operationDesc);

        // span结束
        span.finish();

        return proceedingJoinPoint.proceed();
    }

    @Around("@annotation(myChildSpan)")
    public Object traceChildSpan(ProceedingJoinPoint proceedingJoinPoint, MyChildSpan myChildSpan) throws Throwable {
        // 类名:方法名
        String operationDesc = getOperationDesc(proceedingJoinPoint);

        // 看方法的注解中有没有设置name
        String name = myChildSpan.spanName();

        // 如果没有设置name,就给span一个默认name
        if (StringUtils.isEmpty(name)) {
            name = "child-span-aspect-" + operationDesc;
        }

        // 从上下文中取得已存在的span
        Span parentSpan = tracer.activeSpan();

        if (null==parentSpan) {
            log.error("can not get span from context");
        } else {
            Span span = tracer.buildSpan(name).asChildOf(parentSpan).start();

            // span日志
            span.log("child span log of " + operationDesc);

            // 增加一个tag
            span.setTag("child-operation-desc", operationDesc);

            // span结束
            span.finish();
        }

        return proceedingJoinPoint.proceed();
    }
}

使用注解

在这里插入图片描述

在这里插入图片描述

验证

docker run -d \
--name jaeger \
-e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 jaegertracing/all-in-one:1.26

在这里插入图片描述

在这里插入图片描述

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos

标签:极简,Span,name,span,Jaeger,注解,import,com,public
来源: https://www.cnblogs.com/bolingcavalry/p/15717913.html