微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3
前言
> 微服务全链路跟踪:grpc集成jaeger中介绍了springboot集成jaeger,在现实使用时很多情况下需要根据业务id来搜索全链路,这里就需要注入tab来方便搜索,
方案
1、直接手动注入
@Controller
@RequestMapping("/demo")
@Api(tags = "demo")
@Slf4j
public class DemoController extends BaseController {
@Autowired
private Tracer tracer;
/**
*
*/
@GetMapping("/getTaskByid")
public ApiResponse getByid(@RequestParam("id") Long id)
{
if (tracer!=null&&tracer.activeSpan()!=null) {
tracer.activeSpan().setTag(“id”, id);
}
return ApiResponse.success(service.selectById(id));
}
}
2、通过注解或者aop自动注入 下面主要介绍注解的方式
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JaegerLog {
String tagName() default "id";
/**
* 操作对象ID
* @return
*/
String objectIdKey() default "";
}
@Slf4j
@Aspect
@Order()
public class JaegerLogAspect {
@Autowired
private Tracer tracer;
@Around("@annotation(jaegerLog)")
@SneakyThrows
public Object around(ProceedingJoinPoint point, JaegerLog jaegerLog) {
Object object = null;
Map<string, object> paramsMap = Maps.newHashMap();
try {
paramsMap .put("args", point.getArgs());
object = point.proceed();
paramsMap .put("response", object);
} catch (BusinessException e) {
throw new Exception(e);
}catch (Exception e) {
throw new Exception(e);
}finally {
try{
if (tracer!=null&&tracer.activeSpan()!=null&&StringUtils.isNotBlank(jaegerLog.objectIdKey())) {
String keyValue = getKeyValue(jaegerLog.objectIdKey(), params);
tracer.activeSpan().setTag(jaegerLog.tagName(), keyValue);
}
}catch (Exception e){
log.error(e.getMessage());
}
}
return object ;
}
private String getKeyValue(String key, Map<string, object> params) {
try {
Object value= JSONPath.eval(params, key);
if (mm != null) {
return value.toString();
}
} catch (Exception e) {
log.error("JSONPath.eval:", e);
}
return null;
}
}
@Controller
@RequestMapping("/demo")
@Api(tags = "demo")
@Slf4j
public class DemoController extends BaseController {
/**
*
*/
@GetMapping("/getByid")
@JaegerLog(objectIdKey = "args[0]")
public ApiResponse getByid(@RequestParam("id") Long id)
{
return ApiResponse.success(service.selectById(id));
}
/**
*
*/
@PostMapping("/add")
@JaegerLog(objectIdKey = "args[0].id")
public ApiResponse add(@RequestBody Map<string,string> parms)
{
return ApiResponse.success(service.add(parms));
}
}
这样就可以自定义参数id来进行jaeger tag注入。 后面就可以根据注入的id来查询全链路结果,如下图: </string,string></string,></string,>
来源:oschina
链接:https://my.oschina.net/lipengxs/blog/4267577