Skip to content

Commit

Permalink
thread name enhancer
Browse files Browse the repository at this point in the history
  • Loading branch information
gunlee01 committed Oct 16, 2016
1 parent 140cbba commit cdfc17e
Show file tree
Hide file tree
Showing 28 changed files with 795 additions and 37 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

bytescope.boot/target/
*.iml

bytescope.agent/dependency-reduced-pom.xml

bytescope.boot/target/**
bytescope.agent/target/**
bytescope.runner/logs/**
bytescope.common/target/**
bytescope.runner/target/**
1 change: 1 addition & 0 deletions bytescope.agent/bytescope.agent.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="bytescope.common" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
</component>
</module>
91 changes: 86 additions & 5 deletions bytescope.agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
<artifactId>bytescope-agent</artifactId>
<version>0.1-SNAPSHOT</version>

<properties>
<scouter.http.jar>scouter.http.jar</scouter.http.jar>
<final.jar.name>${pom.build.directory}/${build.finalName}.jar</final.jar.name>
<final.jar.name.xtra>${pom.build.directory}/xtra-${build.finalName}.jar</final.jar.name.xtra>
<final.jar.name.shade>${pom.build.directory}/shade-${build.finalName}.jar</final.jar.name.shade>
</properties>


<build>
<plugins>
<plugin>
Expand All @@ -33,22 +41,95 @@
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
<excludes>
<exclude>**/scouter/xtra/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>make-ext-jar</id>
<phase>package</phase>
<configuration>
<target>
<jar destfile="${pom.build.directory}/${scouter.http.jar}">
<fileset dir="${pom.build.outputDirectory}">
<include name="scouter/xtra/http/*.class"/>
</fileset>
</jar>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>add-to-uber-jar</id>
<phase>package</phase>
<configuration>
<target>
<echo message="final.jar.name = ${final.jar.name}" />
<echo message="final.jar.name.xtra = ${final.jar.name.xtra}" />

<jar destfile="${final.jar.name.xtra}">
<fileset dir="${pom.build.directory}">
<include name="${scouter.http.jar}"/>
</fileset>
<zipfileset src="${final.jar.name}" />

<manifest>
<attribute name="Premain-Class" value="scouter.toys.bytescope.AgentMain"/>
<attribute name="Agent-Class" value="scouter.toys.bytescope.AgentMain"/>
<attribute name="Can-Redefine-Classes" value="true"/>
<attribute name="Can-Retransform-Classes" value="true"/>
</manifest>
</jar>

<copy file="${final.jar.name}" tofile="${final.jar.name.shade}" overwrite="true" verbose="true" />
<copy file="${final.jar.name.xtra}" tofile="${final.jar.name}" overwrite="true" verbose="true" />
<delete file="${pom.build.directory}/${scouter.http.jar}" />

<copy failonerror="false" file="${final.jar.name}" todir="${project.basedir}/../bytescope.runner/agent/" overwrite="true" verbose="true" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<!--<dependency>-->
<!--<groupId>com.github.scouter-project</groupId>-->
<!--<artifactId>scouter-common</artifactId>-->
<!--<version>1.1.0-SNAPSHOT</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.github.scouter-project</groupId>
<artifactId>bytescope-common</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public static Instrumentation getInstrumentation() {
// }

public static void agentmain(String args, Instrumentation inst) throws Exception {
if(loaded) {
System.out.println("[bytescope] aleady loaded");
return;
try {
if (loaded) {
System.out.println("[bytescope] aleady loaded");
return;
}
System.out.println("[bytescope] start agentmain");
innermain(args, inst);

} catch(Throwable t) {
t.printStackTrace();
}
System.out.println("[bytescope] start agentmain");
innermain(args, inst);
}

private static void innermain(String args, Instrumentation inst) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
cr.accept(cv, ClassReader.EXPAND_FRAMES);
classfileBuffer = cw.toByteArray();
cv = cw = getClassWriter(classDesc);
Logger.info(className + "\t\t[" + loader + "]");
Logger.info(className + "\t[" + loader + "]");
}
}
return classfileBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class ServletServiceMV extends LocalVariablesSorter implements Opcodes {
public ServletServiceMV(int access, String desc, MethodVisitor mv) {
super(ASM4, access, desc, mv);
}
private int statIdx;
@Override
public void visitCode() {
mv.visitVarInsn(Opcodes.ALOAD, 1);
Expand All @@ -96,7 +95,6 @@ public void visitCode() {
@Override
public void visitInsn(int opcode) {
if ((opcode >= IRETURN && opcode <= RETURN)) {
mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, DECO, END_METHOD, END_SIGNATURE, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,60 @@

import scouter.toys.bytescope.deco.context.ServletTraceContext;
import scouter.toys.bytescope.deco.context.ServletTraceContextManager;
import scouter.toys.bytescope.deco.helper.HttpTraceFactory;
import scouter.toys.bytescope.deco.helper.IHttpTrace;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author Gun Lee (gunlee01@gmail.com) on 2016. 9. 4.
*/
public class CommonDeco {
public static void startHttpService() {
ServletTraceContext contxt = ServletTraceContextManager.start();
System.out.println("[method-start]CommonDeco.startHttpService()");
private static Object lock = new Object();
private static IHttpTrace http = null;

public static void startHttpService(Object req, Object res) {
ServletTraceContext ctx = ServletTraceContextManager.getContext();
if (ctx != null) {
return;
}
ctx = ServletTraceContextManager.start();

if (http == null) {
initHttp(req);
}
http.start(ctx, req, res);

String orgThreadName = Thread.currentThread().getName();
ctx.setOrgThreadName(orgThreadName);

Date startDate = new Date(ctx.getStartTime());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startDateString = formatter.format(startDate);

Thread.currentThread().setName(orgThreadName + " [bytescope][uri]" + ctx.getServiceName() + " [start at] " + startDateString + " [" + ctx.getStartTime() + "]");
}

public static void endHttpService() {
System.out.println("[method-start]CommonDeco.endHttpService()-Normal case");
public static void endHttpService(Throwable thr) {
ServletTraceContext ctx = ServletTraceContextManager.end();
if(ctx == null) {
return;
}

Thread.currentThread().setName(ctx.getOrgThreadName());

if(thr != null) {
} else {
}
}

private static void initHttp(Object req) {
synchronized (lock) {
if (http == null) {
http = HttpTraceFactory.create(req.getClass().getClassLoader());
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
package scouter.toys.bytescope.deco.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Gun Lee (gunlee01@gmail.com) on 2016. 9. 25.
*/
public class ServletTraceContext {
private HttpServletRequest request;
private HttpServletResponse response;
private String serviceName;
private int servicHash;
private long startTime;
private String orgThreadName;

public HttpServletRequest getRequest() {
return request;
}

public void setRequest(HttpServletRequest request) {
this.request = request;
}

public HttpServletResponse getResponse() {
return response;
}

public void setResponse(HttpServletResponse response) {
this.response = response;
}

public String getServiceName() {
return serviceName;
}

public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public int getServicHash() {
return servicHash;
}

public void setServicHash(int servicHash) {
this.servicHash = servicHash;
}

public long getStartTime() {
return startTime;
}

public void setStartTime(long startTime) {
this.startTime = startTime;
}

public String getOrgThreadName() {
return orgThreadName;
}

public void setOrgThreadName(String orgThreadName) {
this.orgThreadName = orgThreadName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public static ServletTraceContext start() {
return contextTL.get();
}

public static ServletTraceContext getContext() {
return contextTL.get();
}

public static ServletTraceContext end() {
ServletTraceContext context = contextTL.get();
contextTL.set(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2015 the original author or authors.
* @https://github.com/scouter-project/scouter
*
* 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.toys.bytescope.deco.helper;

import scouter.toys.bytescope.deco.context.ServletTraceContext;
import scouterx.org.pmw.tinylog.Logger;

public class HttpTraceFactory {
private static final String HTTP_TRACE = "scouter.xtra.http.HttpTrace";

public static final IHttpTrace dummy = new IHttpTrace() {
public String getParameter(Object req, String key) {
return null;
}

public String getHeader(Object req, String key) {
return null;
}

public void start(ServletTraceContext ctx, Object req, Object res) {
}

public void end() {
}
};

public static IHttpTrace create(ClassLoader parent) {
try {
ClassLoader loader = LoaderManager.getHttpLoader(parent);
if (loader == null) {
return dummy;
}
Class c = Class.forName(HTTP_TRACE, true, loader);
return (IHttpTrace) c.newInstance();
} catch (Throwable e) {
Logger.error(e, "fail to create IHttpTrace");
return dummy;
}
}

}
Loading

0 comments on commit cdfc17e

Please sign in to comment.