Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
gunlee01 committed Sep 27, 2017
2 parents c8aef28 + 64997ec commit d584bf2
Show file tree
Hide file tree
Showing 223 changed files with 14,136 additions and 413 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.github.scouter-project</groupId>
<artifactId>scouter-parent</artifactId>
<version>1.7.3.2</version>
<version>1.7.4.SNAPSHOT</version>
<packaging>pom</packaging>

<name>SCOUTER APM</name>
Expand All @@ -15,6 +15,7 @@

<modules>
<module>scouter.common</module>
<module>scouter.webapp</module>
<module>scouter.server.boot</module>
<module>scouter.server</module>
<module>scouter.agent.host</module>
Expand All @@ -27,10 +28,15 @@
<scouter.agent.java.assembly.name>scouter-java-agent-assembly</scouter.agent.java.assembly.name>
<scouter.agent.batch.assembly.name>scouter-batch-agent-assembly</scouter.agent.batch.assembly.name>
<scouter.server.assembly.name>scouter-server-assembly</scouter.server.assembly.name>
<scouter.webapp.assembly.name>scouter-webapp-assembly</scouter.webapp.assembly.name>

<scouter.whole.packaging.prepare.dir>${project.basedir}/../target/whole-pack-working</scouter.whole.packaging.prepare.dir>
<scouter.product.name>scouter-all-${project.version}</scouter.product.name>

<jetty.version>9.4.6.v20170531</jetty.version>
<jersey.version>2.25.1</jersey.version>
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
</properties>

<repositories>
Expand Down
2 changes: 1 addition & 1 deletion scouter.agent.batch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.github.scouter-project</groupId>
<artifactId>scouter-parent</artifactId>
<version>1.7.3.2</version>
<version>1.7.4.SNAPSHOT</version>
</parent>

<artifactId>scouter-agent-batch</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scouter.agent.host/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.github.scouter-project</groupId>
<artifactId>scouter-parent</artifactId>
<version>1.7.3.2</version>
<version>1.7.4.SNAPSHOT</version>
</parent>

<artifactId>scouter-agent-host</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions scouter.agent.java/plugin/counter.plug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[counter]
//public void counter(scouter.lang.pack.PerfCounterPack $pack)
2 changes: 1 addition & 1 deletion scouter.agent.java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.github.scouter-project</groupId>
<artifactId>scouter-parent</artifactId>
<version>1.7.3.2</version>
<version>1.7.4.SNAPSHOT</version>
</parent>

<artifactId>scouter-agent-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import scouter.agent.Configure;
import scouter.agent.Logger;
import scouter.agent.asm.jdbc.PsUpdateCountMV;
import scouter.agent.asm.jdbc.PsCloseMV;
import scouter.agent.asm.jdbc.StExecuteMV;
import scouter.agent.asm.jdbc.StInitMV;
import scouter.agent.asm.util.HookingSet;

import java.util.HashSet;
Expand Down Expand Up @@ -78,12 +80,18 @@ public void visit(int version, int access, String name, String signature, String
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
if ("<init>".equals(name)) {
return new StInitMV(access, desc, mv);
}

if (StExecuteMV.isTarget(name)) {
if (desc.startsWith("(Ljava/lang/String;)")) {
return new StExecuteMV(access, desc, mv, owner, name);
}
} else if ("getUpdateCount".equals(name) && "()I".equals(desc)) {
return new PsUpdateCountMV(mv);
} else if ("close".equals(name) && "()V".equals(desc)) {
return new PsCloseMV(mv);
}
return mv;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2015 Scouter Project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package scouter.agent.asm.jdbc;

import scouter.agent.trace.TraceSQL;
import scouter.org.objectweb.asm.MethodVisitor;
import scouter.org.objectweb.asm.Opcodes;
import scouter.org.objectweb.asm.commons.LocalVariablesSorter;

public class StInitMV extends LocalVariablesSorter implements Opcodes {

private final static String TRACESQL = TraceSQL.class.getName().replace('.', '/');
private final static String METHOD_INIT = "stmtInit";
private final static String SIGNATURE_INIT = "(Ljava/lang/Object;)V";

public StInitMV(int access, String desc, MethodVisitor mv) {
super(ASM5,access, desc, mv);
}

@Override
public void visitInsn(int opcode) {
if (opcode >= IRETURN && opcode <= RETURN) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, METHOD_INIT, SIGNATURE_INIT, false);
}
mv.visitInsn(opcode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package scouter.agent.counter.task;

import scouter.agent.counter.CounterBasket;
import scouter.agent.counter.anotation.Counter;
import scouter.agent.plugin.PluginCounter;

public class PluginTask {

@Counter
public void pluginCounter(CounterBasket pw) {
PluginCounter.counter(pw);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public Pack threadDetail(Pack param) {
if(thread != 0L) {
p = ThreadUtil.getThreadDetail(thread);
ctx = TraceContextManager.getContext(thread);

if (ctx != null) {
p.put("Service Txid", new TextValue(Hexa32.toString32(ctx.txid)));
p.put("Service Name", new TextValue(AgentCommonConstant.removeSpringRequestMappingPostfixFlag(ctx.serviceName)));
long etime = System.currentTimeMillis() - ctx.startTime;
p.put("Service Elapsed", new DecimalValue(etime));
String sql = ctx.sqltext;
if (sql != null) {
p.put("SQL", sql);
}
String subcall = ctx.apicall_name;
if (subcall != null) {
p.put("Subcall", subcall);
}
}

} else {
p = new MapPack();
ctx = TraceContextManager.getDeferredContext(txid);
Expand All @@ -64,6 +80,7 @@ public Pack threadDetail(Pack param) {
p.put("Service Name", new TextValue(AgentCommonConstant.removeSpringRequestMappingPostfixFlag(ctx.serviceName)));
long etime = System.currentTimeMillis() - ctx.startTime;
p.put("Service Elapsed", new DecimalValue(etime));

} else {
p.put("Thread Name", new TextValue("[No Thread] End"));
p.put("State", new TextValue("end"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scouter.agent.plugin;

import scouter.lang.pack.PerfCounterPack;

abstract public class AbstractCounter extends AbstractPlugin {

abstract public void counter(PerfCounterPack pack);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package scouter.agent.plugin;

import scouter.agent.JavaAgent;
import scouter.agent.Logger;
import scouter.agent.netio.data.DataProxy;
import scouter.agent.trace.AlertProxy;
Expand All @@ -15,6 +16,7 @@

import java.lang.reflect.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -63,6 +65,34 @@ public static Object invokeMethod(Object o, String methodName, Class[] argTypes,
return m.invoke(o, args);
}

static Map<String, Integer> tryCountMap = Collections.synchronizedMap(new HashMap<String, Integer>(10));

public static void clearTryCountMap() {
tryCountMap.clear();
}

public static Object invokeStaticMethod(String className, String methodName) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Integer tryCount = tryCountMap.get(className);
if (tryCount != null && tryCount.intValue() > 20) return null;
Method m = (Method) reflCache.get(className);
if (m == null) {
Class[] loadedClasses = JavaAgent.getInstrumentation().getAllLoadedClasses();
for (Class c : loadedClasses) {
if (c.getName().equals(className)) {
m = c.getMethod(methodName);
reflCache.put(className, m);
tryCountMap.remove(className);
break;
}
}
}
if (m == null) {
tryCountMap.put(className, new Integer(tryCount.intValue() + 1));
return null;
}
return m.invoke(null, new Object[]{});
}

public static Object newInstance(String className) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
return newInstance(className, Thread.currentThread().getContextClassLoader());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scouter.agent.plugin;

import scouter.agent.counter.CounterBasket;
import scouter.lang.TimeTypeEnum;

public class PluginCounter {

static AbstractCounter plugIn;

static {
PluginLoader.getInstance();
}

public static void counter(CounterBasket cw) {

if (plugIn != null) {
try {
plugIn.counter(cw.getPack(TimeTypeEnum.REALTIME));
} catch (Throwable th) {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import scouter.agent.trace.HookReturn;
import scouter.agent.trace.TraceSQL;
import scouter.javassist.*;
import scouter.lang.pack.PerfCounterPack;
import scouter.util.FileUtil;
import scouter.util.Hexa32;
import scouter.util.StringUtil;
Expand Down Expand Up @@ -121,6 +122,15 @@ private void reloadIfModified(File root) {
PluginHttpCallTrace.plugIn = createIHttpCall(script);
}
}
script = new File(root, "counter.plug");
if (script.canRead() == false) {
PluginCounter.plugIn = null;
} else {
if (PluginCounter.plugIn == null
|| PluginCounter.plugIn.lastModified != script.lastModified()) {
PluginCounter.plugIn = createCounter(script);
}
}
}
private long IHttpServiceCompile;
private AbstractHttpService createHttpService(File script) {
Expand All @@ -130,7 +140,7 @@ private AbstractHttpService createHttpService(File script) {
try {
HashMap<String, StringBuffer> bodyTable = loadFileText(script);
String superName = AbstractHttpService.class.getName();
String className = "scouter.agent.plugin.impl.HttpServiceImpl";
String className = "scouter.agent.plugIn.impl.HttpServiceImpl";
String METHOD_START = "start";
String METHOD_END = "end";
String METHOD_REJECT = "reject";
Expand Down Expand Up @@ -240,7 +250,7 @@ private AbstractAppService createAppService(File script) {
try {
HashMap<String, StringBuffer> bodyTable = loadFileText(script);
String superName = AbstractAppService.class.getName();
String className = "scouter.agent.plugin.impl.ServiceTraceImpl";
String className = "scouter.agent.plugIn.impl.ServiceTraceImpl";
String START = "start";
String START_SIG = "(" + nativeName(WrContext.class) + nativeName(HookArgs.class) + ")V";
String START_P1 = WrContext.class.getName();
Expand Down Expand Up @@ -324,7 +334,7 @@ private AbstractCapture createICaptureTrace(File script) {
try {
HashMap<String, StringBuffer> bodyTable = loadFileText(script);
String superName = AbstractCapture.class.getName();
String className = "scouter.agent.plugin.impl.CaptureImpl";
String className = "scouter.agent.plugIn.impl.CaptureImpl";
String ARG = "capArgs";
String ARG_SIG = "(" + nativeName(WrContext.class) + nativeName(HookArgs.class) + ")V";
String ARG_P1 = WrContext.class.getName();
Expand Down Expand Up @@ -437,7 +447,7 @@ private AbstractJdbcPool createIJdbcPool(File script) {
try {
HashMap<String, StringBuffer> bodyTable = loadFileText(script);
String superName = AbstractJdbcPool.class.getName();
String className = "scouter.agent.plugin.impl.JdbcPoolImpl";
String className = "scouter.agent.plugIn.impl.JdbcPoolImpl";
String URL = "url";
String URL_SIG = "(" + nativeName(WrContext.class) + nativeName(String.class) + nativeName(Object.class)
+ ")" + nativeName(String.class);
Expand Down Expand Up @@ -498,7 +508,7 @@ private AbstractHttpCall createIHttpCall(File script) {
try {
HashMap<String, StringBuffer> bodyTable = loadFileText(script);
String superName = AbstractHttpCall.class.getName();
String className = "scouter.agent.plugin.impl.IHttCallTraceImpl";
String className = "scouter.agent.plugIn.impl.IHttCallTraceImpl";
String CALL = "call";
String CALL_SIG = "(" + nativeName(WrContext.class) + nativeName(WrHttpCallRequest.class) + ")V";
String CALL_P1 = WrContext.class.getName();
Expand Down Expand Up @@ -550,6 +560,58 @@ private AbstractHttpCall createIHttpCall(File script) {
}
return null;
}
private long ICounterCompile;
private AbstractCounter createCounter(File script) {
if (ICounterCompile == script.lastModified())
return null;
ICounterCompile = script.lastModified();
try {
HashMap<String, StringBuffer> bodyTable = loadFileText(script);
String superName = AbstractCounter.class.getName();
String className = "scouter.agent.plugIn.impl.CounterImpl";
String METHOD_COUNTER = "counter";
String METHOD_SIGNATURE = "(" + nativeName(PerfCounterPack.class) +")V";
String METHOD_P1 = PerfCounterPack.class.getName();
if (bodyTable.containsKey(METHOD_COUNTER) == false)
throw new CannotCompileException("no method body: " + METHOD_COUNTER);
ClassPool cp = ClassPool.getDefault();
String jar = FileUtil.getJarFileName(PluginLoader.class);
if (jar != null) {
cp.appendClassPath(jar);
}
CtClass cc = cp.get(superName);
CtClass impl = null;
CtMethod method_counter = null;
try {
impl = cp.get(className);
impl.defrost();
method_counter = impl.getMethod(METHOD_COUNTER, METHOD_SIGNATURE);
} catch (NotFoundException e) {
impl = cp.makeClass(className, cc);
StringBuffer sb = new StringBuffer();
sb.append("public void ").append(METHOD_COUNTER).append("(").append(METHOD_P1).append(" p1){}");
method_counter = CtNewMethod.make(sb.toString(), impl);
impl.addMethod(method_counter);
}
StringBuffer body = new StringBuffer();
body.append("{");
body.append(METHOD_P1).append(" $pack=$1;");
body.append(bodyTable.get(METHOD_COUNTER));
body.append("\n}");
method_counter.setBody(body.toString());
Class c = impl.toClass(new URLClassLoader(new URL[0], this.getClass().getClassLoader()), null);
AbstractCounter plugin = (AbstractCounter) c.newInstance();
plugin.lastModified = script.lastModified();
Logger.println("PLUG-IN : " + AbstractCounter.class.getName() + " " + script.getName() + " loaded #"
+ Hexa32.toString32(plugin.hashCode()));
return plugin;
} catch (CannotCompileException ee) {
Logger.println("PLUG-IN : " + ee.getMessage());
} catch (Throwable e) {
Logger.println("A161", e);
}
return null;
}
private String nativeName(Class class1) {
return "L" + class1.getName().replace('.', '/') + ";";
}
Expand Down
Loading

0 comments on commit d584bf2

Please sign in to comment.