Skip to content

Instantly share code, notes, and snippets.

@ttfcfc
Forked from yangl/UmpLogAspect.java
Created January 12, 2017 03:48
Show Gist options
  • Save ttfcfc/b487976b3a7c2088fd7eabb31e083dff to your computer and use it in GitHub Desktop.
Save ttfcfc/b487976b3a7c2088fd7eabb31e083dff to your computer and use it in GitHub Desktop.

Revisions

  1. @yangl yangl revised this gist Aug 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UmpLogAspect.java
    Original file line number Diff line number Diff line change
    @@ -86,7 +86,7 @@ public Object execUmpLog(ProceedingJoinPoint jp) throws Throwable {
    jsonMapper.toJson(rs),
    e);

    throw new Throwable(e);
    throw e;

    }
    if (callerInfo != null) {
  2. @yangl yangl revised this gist Nov 15, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion UmpLogAspect.java
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,8 @@
    /**
    * UmpLog切面
    *
    * spring mvc Controller 切入点必须和springmvc-servlet.xml放一起,不然不起作用!!!
    * 注意:如果把spring mvc的Controller
    * 作为切入点,下边的xml配置必须和springmvc-servlet.xml放一起,不然不起作用!!!
    *
    * @author YANGLiiN
    * @date 13-8-15 上午11:11
  3. @yangl yangl revised this gist Nov 15, 2013. No changes.
  4. @yangl yangl revised this gist Nov 15, 2013. 2 changed files with 7 additions and 3 deletions.
    8 changes: 6 additions & 2 deletions UmpLogAspect.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package com.jd.buy.giftshop.trade.web.aspect;
    package com.jd.buy.giftshop.shared.utils.aspect;

    import java.util.HashMap;
    import java.util.HashSet;
    @@ -19,6 +19,8 @@
    /**
    * UmpLog切面
    *
    * spring mvc Controller 切入点必须和springmvc-servlet.xml放一起,不然不起作用!!!
    *
    * @author YANGLiiN
    * @date 13-8-15 上午11:11
    */
    @@ -68,7 +70,9 @@ public Object execUmpLog(ProceedingJoinPoint jp) throws Throwable {
    long time = System.currentTimeMillis() - start;

    if (time > maxExecTime) {
    logger.info("***调用【{}】方法执行耗时【{}ms】***", className + "." + methodName, time);
    logger.info("***调用【{}】方法执行耗时【{}ms】***", className
    + "."
    + methodName, time);
    }

    }
    2 changes: 1 addition & 1 deletion spring-config-ump.xml
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    default-autowire="byName">


    <bean id="umpLogAspect" class="com.jd.buy.giftshop.trade.web.aspect.UmpLogAspect">
    <bean id="umpLogAspect" class="com.jd.buy.giftshop.shared.utils.aspect.UmpLogAspect">
    <!-- 调用方法最大时间(毫秒),超过则记录之 -->
    <property name="maxExecTime" value="1000"/>
    <!-- UMP总开关 -->
  5. @yangl yangl revised this gist Oct 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion spring-config-ump.xml
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@
    <entry key="com.jd.buy.giftshop.trade.web.payandship.PayAndShipAction" value="payandship"/>
    </map>
    </property>
    <!-- 方法白名单列表,以get开关的方法但又想监控的就加到该列表-->
    <!-- 方法白名单列表,以get,set开头的方法但又想监控的就加到该列表-->
    <property name="whiteMethod">
    <set>
    <value>getCurrentCartNew</value>
  6. @yangl yangl revised this gist Oct 25, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions spring-config-ump.xml
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,12 @@
    <entry key="com.jd.buy.giftshop.trade.web.payandship.PayAndShipAction" value="payandship"/>
    </map>
    </property>
    <!-- 方法白名单列表,以get开关的方法但又想监控的就加到该列表-->
    <property name="whiteMethod">
    <set>
    <value>getCurrentCartNew</value>
    </set>
    </property>
    </bean>

    <aop:config>
  7. @yangl yangl revised this gist Oct 25, 2013. 1 changed file with 29 additions and 16 deletions.
    45 changes: 29 additions & 16 deletions UmpLogAspect.java
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,18 @@
    package com.jd.buy.giftshop.trade.web.aspect;

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;

    import javax.annotation.PostConstruct;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.InitializingBean;

    import com.jd.buy.giftshop.trade.common.utils.JsonMapper;
    import com.jd.buy.giftshop.shared.utils.mapper.JsonMapper;
    import com.jd.ump.profiler.CallerInfo;
    import com.jd.ump.profiler.proxy.Profiler;

    @@ -20,7 +23,8 @@
    * @date 13-8-15 上午11:11
    */

    public class UmpLogAspect implements InitializingBean {
    // implements InitializingBean
    public class UmpLogAspect {

    private static final Logger logger = LoggerFactory.getLogger(UmpLogAspect.class);
    private JsonMapper jsonMapper = new JsonMapper();
    @@ -29,6 +33,7 @@ public class UmpLogAspect implements InitializingBean {
    private String system;
    private int maxExecTime = 1000;
    private Map<String, String> umpMaps = new HashMap<String, String>();
    private Set<String> whiteMethod = new HashSet<String>();

    public Object execUmpLog(ProceedingJoinPoint jp) throws Throwable {
    MethodSignature ms = (MethodSignature) jp.getSignature();
    @@ -41,16 +46,22 @@ public Object execUmpLog(ProceedingJoinPoint jp) throws Throwable {

    try {
    if (enable) {
    String umpLogKey = umpMaps.get(className);
    if (umpLogKey != null && !"".equals(umpLogKey)) {
    StringBuilder sb = new StringBuilder(system).append(".")
    .append(umpLogKey)
    .append(".")
    .append(methodName);
    String jproKey = sb.toString();
    callerInfo = Profiler.registerInfo(jproKey, false, true);

    if ((!methodName.startsWith("get") && !methodName.startsWith("set"))
    || whiteMethod.contains(methodName)) {
    String umpLogKey = umpMaps.get(className);
    if (umpLogKey != null && !"".equals(umpLogKey)) {
    StringBuilder sb = new StringBuilder(system).append(".")
    .append(umpLogKey)
    .append(".")
    .append(methodName);
    String jproKey = sb.toString();

    callerInfo = Profiler.registerInfo(jproKey, false, true);

    }
    }

    }
    start = System.currentTimeMillis();
    rs = jp.proceed();
    @@ -73,17 +84,15 @@ public Object execUmpLog(ProceedingJoinPoint jp) throws Throwable {
    throw new Throwable(e);

    }
    finally {
    if (callerInfo != null) {
    Profiler.registerInfoEnd(callerInfo);
    }
    if (callerInfo != null) {
    Profiler.registerInfoEnd(callerInfo);
    }

    return rs;

    }

    @Override
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
    if (enable) {
    Profiler.InitHeartBeats(system + ".heart");
    @@ -107,4 +116,8 @@ public void setSystem(String system) {
    public void setUmpMaps(Map<String, String> umpMaps) {
    this.umpMaps = umpMaps;
    }

    public void setWhiteMethod(Set<String> whiteMethod) {
    this.whiteMethod = whiteMethod;
    }
    }
  8. @yangl yangl revised this gist Aug 16, 2013. No changes.
  9. @yangl yangl renamed this gist Aug 16, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. @yangl yangl revised this gist Aug 16, 2013. No changes.
  11. @yangl yangl created this gist Aug 16, 2013.
    110 changes: 110 additions & 0 deletions UmpLogAspect.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    package com.jd.buy.giftshop.trade.web.aspect;

    import java.util.HashMap;
    import java.util.Map;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.InitializingBean;

    import com.jd.buy.giftshop.trade.common.utils.JsonMapper;
    import com.jd.ump.profiler.CallerInfo;
    import com.jd.ump.profiler.proxy.Profiler;

    /**
    * UmpLog切面
    *
    * @author YANGLiiN
    * @date 13-8-15 上午11:11
    */

    public class UmpLogAspect implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(UmpLogAspect.class);
    private JsonMapper jsonMapper = new JsonMapper();

    private boolean enable = true;
    private String system;
    private int maxExecTime = 1000;
    private Map<String, String> umpMaps = new HashMap<String, String>();

    public Object execUmpLog(ProceedingJoinPoint jp) throws Throwable {
    MethodSignature ms = (MethodSignature) jp.getSignature();
    String className = jp.getTarget().getClass().getName();
    String methodName = ms.getName();

    CallerInfo callerInfo = null;
    Object rs = null;
    long start;

    try {
    if (enable) {
    String umpLogKey = umpMaps.get(className);
    if (umpLogKey != null && !"".equals(umpLogKey)) {
    StringBuilder sb = new StringBuilder(system).append(".")
    .append(umpLogKey)
    .append(".")
    .append(methodName);
    String jproKey = sb.toString();
    callerInfo = Profiler.registerInfo(jproKey, false, true);

    }
    }
    start = System.currentTimeMillis();
    rs = jp.proceed();
    long time = System.currentTimeMillis() - start;

    if (time > maxExecTime) {
    logger.info("***调用【{}】方法执行耗时【{}ms】***", className + "." + methodName, time);
    }

    }
    catch (Throwable e) {
    if (callerInfo != null) {
    Profiler.functionError(callerInfo);
    }
    logger.error("***参数列表【{}】,执行结果【{}】,异常堆栈【{}】***",
    jsonMapper.toJson(jp.getArgs()),
    jsonMapper.toJson(rs),
    e);

    throw new Throwable(e);

    }
    finally {
    if (callerInfo != null) {
    Profiler.registerInfoEnd(callerInfo);
    }
    }

    return rs;

    }

    @Override
    public void afterPropertiesSet() throws Exception {
    if (enable) {
    Profiler.InitHeartBeats(system + ".heart");
    Profiler.registerJVMInfo(system + ".jvm");
    }

    }

    public void setEnable(boolean enable) {
    this.enable = enable;
    }

    public void setMaxExecTime(int maxExecTime) {
    this.maxExecTime = maxExecTime;
    }

    public void setSystem(String system) {
    this.system = system;
    }

    public void setUmpMaps(Map<String, String> umpMaps) {
    this.umpMaps = umpMaps;
    }
    }
    43 changes: 43 additions & 0 deletions gistfile1.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
    default-autowire="byName">


    <bean id="umpLogAspect" class="com.jd.buy.giftshop.trade.web.aspect.UmpLogAspect">
    <!-- 调用方法最大时间(毫秒),超过则记录之 -->
    <property name="maxExecTime" value="1000"/>
    <!-- UMP总开关 -->
    <property name="enable" value="true"/>
    <!-- UMP系统中注册的key,自动注册系统心跳`system`.heart及jvm监控`system`.jvm-->
    <property name="system" value="gift-trade"/>
    <property name="umpMaps">
    <map>
    <!-- 注册key组合:`system`.`value`.`methodName` -->
    <entry key="com.jd.buy.giftshop.trade.web.balance.BalanceAction" value="balance"/>
    <entry key="com.jd.buy.giftshop.trade.web.consignee.ConsigneeAction" value="consignee"/>
    <entry key="com.jd.buy.giftshop.trade.web.coupon.CouponAction" value="coupon"/>
    <entry key="com.jd.buy.giftshop.trade.web.giftcard.GiftCardAction" value="giftcard"/>
    <entry key="com.jd.buy.giftshop.trade.web.invoice.InvoiceAction" value="invoice"/>
    <entry key="com.jd.buy.giftshop.trade.web.order.OrderAction" value="order"/>
    <entry key="com.jd.buy.giftshop.trade.web.payandship.PayAndShipAction" value="payandship"/>
    </map>
    </property>
    </bean>

    <aop:config>
    <aop:aspect ref="umpLogAspect">
    <aop:pointcut id="umpLogPointcut"
    expression="execution(public * com.jd.buy.giftshop.trade.web.*.*Action.*(..))"/>
    <aop:around pointcut-ref="umpLogPointcut" method="execUmpLog"/>
    </aop:aspect>

    </aop:config>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    </beans>