Search in sources :

Example 6 with ModuleEventWatcher

use of com.shulie.instrument.simulator.api.resource.ModuleEventWatcher in project LinkAgent by shulieTech.

the class WatchModule method info.

@Command(value = "info", description = "watch 方法")
public CommandResponse info(final Map<String, String> args) {
    String classPattern = getParameter(args, "class");
    String methodPattern = getParameter(args, "method");
    String express = getParameter(args, "express");
    String condition = getParameter(args, "condition");
    final int limits = getIntParameter(args, "limits", 20);
    final int wait = getIntParameter(args, "wait", -1);
    if (StringUtil.isEmpty(classPattern)) {
        return CommandResponse.failure("class must not be empty.");
    }
    if (StringUtil.isEmpty(methodPattern)) {
        methodPattern = "*";
    }
    EventWatcher watcher = null;
    try {
        if (limits > 5000) {
            return CommandResponse.failure("limits 最大不能超过5000");
        }
        final CountDownLatch latch = new CountDownLatch(1);
        Queue<Object> watchViews = new ConcurrentLinkedQueue<Object>();
        watcher = new EventWatchBuilder(moduleEventWatcher).onClass(classPattern).includeSubClasses().onBehavior(methodPattern).withInvoke().withCall().onListener(Listeners.of(WatchListener.class, new Object[] { latch, watchViews, condition, express, wait != -1 ? -1 : limits })).onClass().onWatch();
        if (wait > 0) {
            latch.await(wait, TimeUnit.SECONDS);
        } else if (limits > 0) {
            latch.await();
        }
        return CommandResponse.success(watchViews);
    } catch (Throwable e) {
        logger.error("SIMULATOR: watch module err! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
        return CommandResponse.failure(e);
    } finally {
        if (watcher != null) {
            try {
                watcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: watch module unwatched failed! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
            }
        }
    }
}
Also used : EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) ModuleEventWatcher(com.shulie.instrument.simulator.api.resource.ModuleEventWatcher) EventWatchBuilder(com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 7 with ModuleEventWatcher

use of com.shulie.instrument.simulator.api.resource.ModuleEventWatcher in project LinkAgent by shulieTech.

the class TraceModule method trace.

@Command(value = "info", description = "方法追踪")
public CommandResponse trace(final Map<String, String> param) {
    final String classPatternStr = param.get("classPatterns");
    if (StringUtil.isEmpty(classPatternStr)) {
        return CommandResponse.failure("classPatterns must not be empty.");
    }
    final String[] classPatterns = classPatternStr.split(",");
    /**
     * 如果 wait 和 count 都没有填写,则默认统计20条
     */
    final int wait = ParameterUtils.getInt(param, "wait", 5000);
    /**
     * 最多层数
     */
    final int level = ParameterUtils.getInt(param, "level", 0);
    /**
     * 条数限定
     */
    final int limits = ParameterUtils.getInt(param, "limits", 100);
    Set<EventWatcher> childrenWatchers = new ConcurrentHashSet<EventWatcher>();
    List<EventWatcher> watchers = new ArrayList<EventWatcher>();
    try {
        if (wait > 10 * 60 * 1000) {
            return CommandResponse.failure("wait 最大等待时间不能超过10分钟");
        }
        if (limits > 5000) {
            return CommandResponse.failure("limits 最大不能超过5000");
        }
        /**
         * 多少毫秒以下停止
         */
        final int stopInMills = ParameterUtils.getInt(param, "stop", -1);
        Map<String, Queue<TraceView>> traceViews = new ConcurrentHashMap<String, Queue<TraceView>>();
        Set<String> traceMethods = new ConcurrentHashSet<String>();
        Set<Class<?>> classes = findClasses(classPatterns);
        Set<Class<?>> instrumentClasses = new HashSet<Class<?>>();
        boolean foundInterface = false;
        boolean foundEnum = false;
        boolean foundAnnotation = false;
        for (Class clazz : classes) {
            if (clazz.isInterface()) {
                Set<Class<?>> implClasses = findImplClasses(clazz);
                instrumentClasses.addAll(implClasses);
            } else if (!clazz.isEnum() && !clazz.isAnnotation()) {
                instrumentClasses.addAll(getProxyInterfaceImplClasses(clazz));
            } else if (clazz.isEnum()) {
                foundEnum = true;
            } else if (clazz.isAnnotation()) {
                foundAnnotation = true;
            }
        }
        if (instrumentClasses.isEmpty()) {
            String errorMsg = "can't found class:" + classPatternStr;
            if (foundInterface) {
                errorMsg = "can't found impl class with interface:" + classPatternStr;
            } else if (foundEnum) {
                errorMsg = "can't trace class because of it is a enum:" + classPatternStr;
            } else if (foundAnnotation) {
                errorMsg = "can't trace class because of it is a annotation:" + classPatternStr;
            }
            return CommandResponse.failure(errorMsg);
        }
        final CountDownLatch latch = new CountDownLatch(1);
        for (Class clazz : instrumentClasses) {
            EventWatcher watcher = new EventWatchBuilder(moduleEventWatcher).onClass(clazz.getName()).includeSubClasses().onAnyBehavior().withInvoke().withCall().onListener(Listeners.of(TraceListener.class, new Object[] { classPatterns, latch, traceViews, traceMethods, childrenWatchers, level, limits, stopInMills, wait })).onClass().onWatch();
            watchers.add(watcher);
        }
        if (wait > 0) {
            latch.await(wait, TimeUnit.MILLISECONDS);
        } else if (limits > 0) {
            latch.await();
        }
        return CommandResponse.success(traceViews);
    } catch (Throwable e) {
        logger.error("SIMULATOR: trace module err! class={},limits={}, wait={}", limits, wait, e);
        return CommandResponse.failure(e);
    } finally {
        for (EventWatcher watcher : watchers) {
            try {
                watcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: trace module unwatched failed! class={},limits={}, wait={}", limits, wait, e);
            }
        }
        for (EventWatcher eventWatcher : childrenWatchers) {
            try {
                eventWatcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: trace module unwatched failed! class={},limits={}, wait={}", limits, wait, e);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) ModuleEventWatcher(com.shulie.instrument.simulator.api.resource.ModuleEventWatcher) CountDownLatch(java.util.concurrent.CountDownLatch) TraceView(com.shulie.instrument.simulator.module.model.trace2.TraceView) ConcurrentHashSet(com.shulie.instrument.simulator.module.stack.trace.util.ConcurrentHashSet) EventWatchBuilder(com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Queue(java.util.Queue) HashSet(java.util.HashSet) ConcurrentHashSet(com.shulie.instrument.simulator.module.stack.trace.util.ConcurrentHashSet) Command(com.shulie.instrument.simulator.api.annotation.Command)

Aggregations

ModuleEventWatcher (com.shulie.instrument.simulator.api.resource.ModuleEventWatcher)7 EventWatchBuilder (com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder)6 EventWatcher (com.shulie.instrument.simulator.api.listener.ext.EventWatcher)6 Command (com.shulie.instrument.simulator.api.annotation.Command)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ClassDescriptor (com.shulie.instrument.simulator.api.filter.ClassDescriptor)1 ExtFilter (com.shulie.instrument.simulator.api.filter.ExtFilter)1 MethodDescriptor (com.shulie.instrument.simulator.api.filter.MethodDescriptor)1 TraceView (com.shulie.instrument.simulator.module.model.trace2.TraceView)1 ConcurrentHashSet (com.shulie.instrument.simulator.module.stack.trace.util.ConcurrentHashSet)1 ConcurrentHashSet (com.shulie.instrument.simulator.module.util.ConcurrentHashSet)1 HashSet (java.util.HashSet)1 Queue (java.util.Queue)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1