Solon

opentracing-solon-plugin

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>opentracing-solon-plugin</artifactId>
</dependency>

1、描述

分布式扩展插件。基于 opentracing 适配的 solon cloud 插件。可与支持 opentracing 规范的服务,一同提供链路跟踪支持。

2、配置示例

solon.app:
  name: "demoapp"
  group: "demo"

solon.cloud.opentracing:
  server: "udp://localhost:6831"
  trace:
    enable: true                     #是否启用(默认:true)
    exclude: "/healthz,/_run/check/" #排除路径,多个以,号隔开

3、代码应用

  • 启用和配置跟踪器实现
public class App {
    public static void main(String[] args) {
        Solon.start(App.class, args);
    }
}

@Configuration
public class Config {
    //构建跟踪服务。添加依赖:io.jaegertracing:jaeger-client:1.7.0
    @Bean
    public Tracer tracer() throws TTransportException {
        CloudProps cloudProps = OpentracingProps.instance;
        
        //为了可自由配置,这行代码重要
        if(cloudProps.getTraceEnable() == false 
            || Utils.isEmpty(cloudProps.getServer())){
            return null;
        }

        URI serverUri = URI.create(cloudProps.getServer());
        Sender sender = new UdpSender(serverUri.getHost(), serverUri.getPort(), 0);

        final CompositeReporter compositeReporter = new CompositeReporter(
                new RemoteReporter.Builder().withSender(sender).build(),
                new LoggingReporter()
        );

        final Metrics metrics = new Metrics(new NoopMetricsFactory());

        return new JaegerTracer.Builder(Solon.cfg().appName())
                .withReporter(compositeReporter)
                .withMetrics(metrics)
                .withExpandExceptionLogs()
                .withSampler(new ConstSampler(true)).build();
    }
}
  • 应用代码
// --  可以当它不存在得用
@Controller
public class TestController {
    @NamiClient
    UserService userService;
    
    @Inject
    OrderService orderService;

    @Mapping("/")
    public String hello(String name) {
        name = userService.getUser(name);

        return orderService.orderCreate(name, "1");
    }
}

//-- 通过注解增加业务链节点 ( @Tracing )
@Service
public class OrderService {
    @Tracing(name = "创建订单", tags = "订单=${orderId}")
    public String orderCreate(String userName, String orderId) {
        //手动添加 tag
        Spans.active().setTag("用户", userName));
        
        return orderId;
    }
}

4、@Tracking 注意事项

  • 控制器或最终转为 Handler 的类可以不加(已由 Filter 全局处理了),加了会产生新的 Span

  • 修改当前 Span 的操作名

@Controller
public class TestController {

    @Mapping("/")
    public String hello(String name) {
        Spans.active().setOperationName("Hello");  //修改当前操作名
    
        return "Hello " + name;
    }
}
  • 添加在空接口上,一般会无效(比如:Mapper)。除非其底层有适配
  • 需加在代理的类上,不然拦截器不会生效。如:@Service,@Dao

5、演示项目

demo9071-opentracing_jeager