Search in sources :

Example 66 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project micrometer by micrometer-metrics.

the class ScheduledMethodMetrics method timeScheduledOperation.

@Around("execution (@org.springframework.scheduling.annotation.Scheduled  * *.*(..))")
public Object timeScheduledOperation(ProceedingJoinPoint pjp) throws Throwable {
    Method method = ((MethodSignature) pjp.getSignature()).getMethod();
    String signature = pjp.getSignature().toShortString();
    if (method.getDeclaringClass().isInterface()) {
        try {
            method = pjp.getTarget().getClass().getDeclaredMethod(pjp.getSignature().getName(), method.getParameterTypes());
        } catch (final SecurityException | NoSuchMethodException e) {
            logger.warn("Unable to perform metrics timing on " + signature, e);
            return pjp.proceed();
        }
    }
    Timer shortTaskTimer = null;
    LongTaskTimer longTaskTimer = null;
    for (Timed timed : TimedUtils.findTimedAnnotations(method)) {
        if (timed.longTask())
            longTaskTimer = LongTaskTimer.builder(timed.value()).tags(timed.extraTags()).description("Timer of @Scheduled long task").register(registry);
        else {
            Timer.Builder timerBuilder = Timer.builder(timed.value()).tags(timed.extraTags()).description("Timer of @Scheduled task");
            if (timed.percentiles().length > 0) {
                timerBuilder = timerBuilder.publishPercentiles(timed.percentiles());
            }
            shortTaskTimer = timerBuilder.register(registry);
        }
    }
    if (shortTaskTimer != null && longTaskTimer != null) {
        final Timer finalTimer = shortTaskTimer;
        // noinspection NullableProblems
        return recordThrowable(longTaskTimer, () -> recordThrowable(finalTimer, pjp::proceed));
    } else if (shortTaskTimer != null) {
        // noinspection NullableProblems
        return recordThrowable(shortTaskTimer, pjp::proceed);
    } else if (longTaskTimer != null) {
        // noinspection NullableProblems
        return recordThrowable(longTaskTimer, pjp::proceed);
    }
    return pjp.proceed();
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Timer(io.micrometer.core.instrument.Timer) Timed(io.micrometer.core.annotation.Timed) Method(java.lang.reflect.Method) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Around(org.aspectj.lang.annotation.Around)

Example 67 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project nextprot-api by calipho-sib.

the class InstrumentationAspect method logDao.

@Around("execution(* org.nextprot.api.*.dao.*.*(..))")
public Object logDao(ProceedingJoinPoint pjp) throws Throwable {
    if (enableInstrumentation) {
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Annotation[][] annotations = methodSignature.getMethod().getParameterAnnotations();
        Object[] arguments = pjp.getArgs();
        StringBuilder sb = new StringBuilder();
        sb.append("type=DAO;");
        addMethodParameters(sb, methodSignature);
        addArgumentsParameters(sb, arguments, annotations);
        sb.append("DAOId=");
        sb.append(daoIdCounter.incrementAndGet());
        sb.append(";");
        Long sId = serviceRequestId.get();
        if (sId != null) {
            sb.append("serviceRequestId=");
            sb.append(sId);
            sb.append(";");
        }
        Long cId = controllerRequestId.get();
        if (cId != null) {
            sb.append("controllerRequestId=");
            sb.append(cId);
            sb.append(";");
        }
        long start = System.currentTimeMillis();
        try {
            // LOGGER.info("aspect=before;" + sb);
            Object result = pjp.proceed();
            addTimeElapsed(sb, System.currentTimeMillis() - start);
            addResultParameters(sb, result);
            LOGGER.info("aspect=after;" + sb);
            return result;
        } catch (Exception e) {
            addTimeElapsed(sb, System.currentTimeMillis() - start);
            addExceptionParameters(sb, e);
            LOGGER.info("aspect=after;" + sb);
            throw e;
        }
    } else {
        return pjp.proceed();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) AtomicLong(java.util.concurrent.atomic.AtomicLong) Around(org.aspectj.lang.annotation.Around)

Example 68 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project nextprot-api by calipho-sib.

the class InstrumentationAspect method logServiceInformaton.

@Around("execution(* org.nextprot.api.*.controller.*.*(..))")
public Object logServiceInformaton(ProceedingJoinPoint pjp) throws Throwable {
    if (enableInstrumentation) {
        controllerRequestId.set(controllerRequestIdCounter.incrementAndGet());
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Annotation[][] annotations = methodSignature.getMethod().getParameterAnnotations();
        Object[] arguments = pjp.getArgs();
        RequestInfo request = RequestInfoFactory.createRequestInfo(methodSignature.getDeclaringType().getSimpleName() + "#" + methodSignature.getName());
        request.putAll(extractSecurityInfo());
        request.putAll(extractHttpInfo());
        clientRequestManager.startMonitoringClientRequest(request);
        StringBuilder sb = new StringBuilder();
        sb.append("type=Controller;");
        addMethodParameters(sb, methodSignature);
        addArgumentsParameters(sb, arguments, annotations);
        sb.append("controllerRequestId=");
        sb.append(controllerRequestIdCounter.get());
        sb.append(";");
        for (String key : request.keySet()) {
            sb.append(key);
            sb.append("=");
            sb.append(request.get(key));
            sb.append(";");
        }
        long start = System.currentTimeMillis();
        // Proceed to method invocation
        try {
            // LOGGER.info("aspect=before;" + sb);
            Object result = pjp.proceed();
            addTimeElapsed(sb, System.currentTimeMillis() - start);
            addResultParameters(sb, result);
            LOGGER.info("aspect=after;" + sb);
            clientRequestManager.stopMonitoringCurrentRequestInfo();
            controllerRequestId.remove();
            return result;
        } catch (Exception e) {
            addTimeElapsed(sb, System.currentTimeMillis() - start);
            addExceptionParameters(sb, e);
            LOGGER.info("aspect=after;" + sb);
            clientRequestManager.stopMonitoringCurrentRequestInfo(e);
            controllerRequestId.remove();
            throw e;
        }
    } else {
        return pjp.proceed();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) RequestInfo(org.nextprot.api.core.aop.requests.RequestInfo) Around(org.aspectj.lang.annotation.Around)

Example 69 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project rxlib by RockyLOMO.

the class FeignInterceptor method onProcess.

@Override
protected Object onProcess(ProceedingJoinPoint joinPoint, StringBuilder msg) throws Throwable {
    Signature signature = joinPoint.getSignature();
    if (!(signature instanceof MethodSignature)) {
        return joinPoint.proceed();
    }
    Method method = ((MethodSignature) signature).getMethod();
    RequestMapping apiMapping = method.getAnnotation(RequestMapping.class);
    if (apiMapping == null) {
        return joinPoint.proceed();
    }
    String url = "";
    FeignClient feignClient = null;
    for (Class<?> pi : joinPoint.getTarget().getClass().getInterfaces()) {
        if ((feignClient = pi.getAnnotation(FeignClient.class)) != null) {
            break;
        }
    }
    if (feignClient != null) {
        url += feignClient.url();
    }
    RequestMapping baseMapping = method.getDeclaringClass().getAnnotation(RequestMapping.class);
    Function<RequestMapping, String> pf = p -> String.join(",", !ArrayUtils.isEmpty(p.value()) ? p.value() : p.path());
    if (baseMapping != null) {
        url += pf.apply(baseMapping);
    }
    url += pf.apply(apiMapping);
    String httpMethod = ArrayUtils.isEmpty(apiMapping.method()) ? "POST" : String.join(",", Arrays.stream(apiMapping.method()).map(p -> p.name()).collect(Collectors.toList()));
    msg.appendLine().appendLine("%s\t\t%s", httpMethod, resolveUrl(url, signature));
    return super.onProcess(joinPoint, msg);
}
Also used : Arrays(java.util.Arrays) Signature(org.aspectj.lang.Signature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Tuple(org.rx.bean.Tuple) StringBuilder(org.rx.util.StringBuilder) FeignClient(org.springframework.cloud.netflix.feign.FeignClient) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LogInterceptor(org.rx.util.LogInterceptor) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) FeignClient(org.springframework.cloud.netflix.feign.FeignClient) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 70 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project rxlib by RockyLOMO.

the class ValidateUtil method onProcess.

/**
 * Annotation expression只对method有效
 *
 * @param joinPoint
 * @param msg
 * @return
 * @throws Throwable
 */
@Override
protected Object onProcess(ProceedingJoinPoint joinPoint, StringBuilder msg) throws Throwable {
    Class targetType = joinPoint.getTarget().getClass();
    Signature signature = joinPoint.getSignature();
    Executable member;
    if (signature instanceof ConstructorSignature) {
        member = ((ConstructorSignature) signature).getConstructor();
    } else {
        member = ((MethodSignature) signature).getMethod();
    }
    msg.setPrefix(String.format("[Valid] %s.%s ", targetType.getSimpleName(), signature.getName()));
    EnableValid attr = member.getAnnotation(EnableValid.class);
    if (attr == null) {
        attr = (EnableValid) targetType.getAnnotation(EnableValid.class);
        if (attr == null) {
            msg.appendLine("skip validate..");
            return joinPoint.proceed();
        }
    }
    int flags = attr.value();
    boolean validateValues = hasFlags(flags, EnableValid.ParameterValues);
    if (hasFlags(flags, EnableValid.Method)) {
        if (signature instanceof ConstructorSignature) {
            ConstructorSignature cs = (ConstructorSignature) signature;
            validateConstructor(cs.getConstructor(), joinPoint.getArgs(), validateValues);
            return super.onProcess(joinPoint, msg);
        }
        MethodSignature ms = (MethodSignature) signature;
        return validateMethod(ms.getMethod(), joinPoint.getTarget(), joinPoint.getArgs(), validateValues, p -> super.onProcess(joinPoint, msg));
    }
    if (validateValues) {
        for (Object parameterValue : joinPoint.getArgs()) {
            validateBean(parameterValue);
        }
    }
    msg.appendLine("validate ok..").setPrefix(null);
    return super.onProcess(joinPoint, msg);
}
Also used : ConstructorSignature(org.aspectj.lang.reflect.ConstructorSignature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) ConstructorSignature(org.aspectj.lang.reflect.ConstructorSignature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Executable(java.lang.reflect.Executable) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Aggregations

MethodSignature (org.aspectj.lang.reflect.MethodSignature)116 Method (java.lang.reflect.Method)95 Around (org.aspectj.lang.annotation.Around)43 JoinPoint (org.aspectj.lang.JoinPoint)30 AccessDeniedException (org.springframework.security.access.AccessDeniedException)26 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)25 Test (org.junit.Test)25 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)24 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)22 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)21 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)14 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)13 Signature (org.aspectj.lang.Signature)13 Annotation (java.lang.annotation.Annotation)10 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 List (java.util.List)2 Tick (mysh.util.Tick)2 Ignite (org.apache.ignite.Ignite)2