监测mysql查询时间
@Autowired
private Tracer tracer; //用于访问Spring Cloud Sleuth跟踪信息 public Organization getOrg (String organizationId) { Span newSpan = tracer.createSpan("getOrgDBCall");//创建一个新的自定义跨度,名为getOrgDBCall logger.debug("In the organizationService.getOrg() call"); try { return orgRepository.findById(organizationId); } finally{ newSpan.tag("peer.service", "mysql");//将标签信息添加到跨度中,提供了将要被Zipkin捕获的服务名称 newSpan.logEvent(org.springframework.cloud.sleuth.Span.CLIENT_RECV);//记录事件,告诉Spring Cloud Sleuth捕获调用完成的时间 tracer.close(newSpan);//关闭跟踪,否则报错 } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 监测redis查询时间private Organization checkRedisCache(String organizationId) {
Span newSpan = tracer.createSpan("readLicensingDataFromRedis"); try { return orgRedisRepo.findOrganization(organizationId); } catch (Exception ex){ return null; } finally { newSpan.tag("peer.service", "redis"); newSpan.logEvent(org.springframework.cloud.sleuth.Span.CLIENT_RECV); tracer.close(newSpan); } }@Aspect@Component@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = false)public class TracerServiceAspect { @Pointcut("execution(public * com.xxx.cloud.*.service.impl.*.*(..))") public void tracerLog() { } // 全局的trace对象 @Autowired Tracer tracer; // 针对所有Controller层的方法的切面 @Around("tracerLog()") public Object doSurround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { Span currentSpan = tracer.getCurrentSpan(); String className = proceedingJoinPoint.getSignature().getDeclaringTypeName(); String methodName = proceedingJoinPoint.getSignature().getName(); Span newSpan = null; if (currentSpan != null) { newSpan = tracer.createSpan(methodName, currentSpan); } else { newSpan = tracer.createSpan(methodName); } newSpan.tag("className", className); newSpan.tag("methodName", methodName); Object result = null; try { // 方法的执行结果 result = proceedingJoinPoint.proceed(); } catch (Exception e) { newSpan.tag("error", e.getMessage()); } finally { newSpan.logEvent(org.springframework.cloud.sleuth.Span.CLIENT_RECV);// 记录事件,告诉Spring Cloud Sleuth捕获调用完成的时间 tracer.close(newSpan);// 关闭跟踪,否则报错 } return result; }}