Extract logback plugin

This commit is contained in:
Jongho Moon
2015-09-15 12:34:04 +09:00
parent 890f03f44e
commit ac89dc0235
12 changed files with 240 additions and 37 deletions
+2
View File
@@ -40,6 +40,7 @@
<artifactId>pinpoint-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@@ -55,6 +56,7 @@
<artifactId>spring-webmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
@@ -25,7 +25,7 @@ import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
@RunWith(PinpointPluginTestSuite.class)
@Dependency({"log4j:log4j:[1.1.3],[1.2.17,)"})
@Dependency({"log4j:log4j:[1.2.16,)"})
public class Log4jIT {
@Test
@@ -0,0 +1,40 @@
/**
z * Copyright 2014 NAVER Corp.
*
* 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 com.navercorp.pinpoint.plugin.logback;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
@RunWith(PinpointPluginTestSuite.class)
@Dependency({"ch.qos.logback:logback-classic:[1.0.13],[1.1.3,)", "org.slf4j:slf4j-api:1.7.12"})
public class LogbackIT {
@Test
public void test() throws Exception {
Logger logger = LoggerFactory.getLogger(getClass());
logger.error("maru");
Assert.assertNotNull(MDC.get("PtxId"));
Assert.assertNotNull(MDC.get("PspanId"));
}
}
@@ -15,8 +15,6 @@
*/
package com.navercorp.pinpoint.plugin.log4j;
import static com.navercorp.pinpoint.common.trace.HistogramSchema.*;
import java.security.ProtectionDomain;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
@@ -27,15 +25,10 @@ import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginInstrumentContext;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.PinpointClassFileTransformer;
import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.common.trace.ServiceType;
public class Log4jPlugin implements ProfilerPlugin {
private final PLogger logger = PLoggerFactory.getLogger(getClass());
public static final ServiceType GSON_SERVICE_TYPE = ServiceType.of(5010, "GSON", NORMAL_SCHEMA);
public static final AnnotationKey GSON_ANNOTATION_KEY_JSON_LENGTH = new AnnotationKey(9000, "gson.json.length");
@Override
public void setup(ProfilerPluginSetupContext context) {
context.addClassFileTransformer("org.apache.log4j.spi.LoggingEvent", new PinpointClassFileTransformer() {
@@ -49,11 +42,11 @@ public class Log4jPlugin implements ProfilerPlugin {
return null;
}
if (!mdcClass.hasMethod("put", new String[]{"java.lang.String", "java.lang.Object"})) {
if (!mdcClass.hasMethod("put", "java.lang.String", "java.lang.Object")) {
logger.warn("modify fail. Because put method does not exist at org.apache.log4j.MDC class.");
return null;
}
if (!mdcClass.hasMethod("remove", new String[]{"java.lang.String"})) {
if (!mdcClass.hasMethod("remove", "java.lang.String")) {
logger.warn("modify fail. Because remove method does not exist at org.apache.log4j.MDC class.");
return null;
}
+5
View File
@@ -0,0 +1,5 @@
/target/
/.settings/
/.classpath
/.project
/*.iml
+5
View File
@@ -0,0 +1,5 @@
RMRqrdbgbKFhbaVnDxHUdDQvrOQXxIBklnvcmahheubVC
mh2KM35CLkwUHS4DH7QVhxy52J5hnWbyEm6Cyd3KkF<mV
RmmnSVOqOMnOnMMrmMqwXomoroNrqPNRrPSsWwtUxXuUU
sRONqpnmqmUUnqonmstsmmmmmUUnqonmstsmmmmmUUGfk
mlfkqUUnmmmm
+27
View File
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pom</artifactId>
<relativePath>../..</relativePath>
<version>1.5.0-SNAPSHOT</version>
</parent>
<artifactId>pinpoint-logback-plugin</artifactId>
<name>pinpoint-logback-plugin</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-bootstrap-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,68 @@
/**
* Copyright 2014 NAVER Corp.
*
* 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 com.navercorp.pinpoint.plugin.logback;
import static com.navercorp.pinpoint.common.trace.HistogramSchema.*;
import java.security.ProtectionDomain;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginInstrumentContext;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.PinpointClassFileTransformer;
import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.common.trace.ServiceType;
public class LogbackPlugin implements ProfilerPlugin {
private final PLogger logger = PLoggerFactory.getLogger(getClass());
public static final ServiceType GSON_SERVICE_TYPE = ServiceType.of(5010, "GSON", NORMAL_SCHEMA);
public static final AnnotationKey GSON_ANNOTATION_KEY_JSON_LENGTH = new AnnotationKey(9000, "gson.json.length");
@Override
public void setup(ProfilerPluginSetupContext context) {
context.addClassFileTransformer("ch.qos.logback.classic.spi.LoggingEvent", new PinpointClassFileTransformer() {
@Override
public byte[] transform(ProfilerPluginInstrumentContext pluginContext, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass mdcClass = pluginContext.getInstrumentClass(loader, "org.slf4j.MDC", null);
if (mdcClass == null) {
logger.warn("modify fail. Because org.slf4j.MDC does not exist.");
return null;
}
if (!mdcClass.hasMethod("put", "java.lang.String", "java.lang.String")) {
logger.warn("modify fail. Because put method does not exist at org.slf4j.MDC class.");
return null;
}
if (!mdcClass.hasMethod("remove", "java.lang.String")) {
logger.warn("modify fail. Because remove method does not exist at org.slf4j.MDC class.");
return null;
}
InstrumentClass target = pluginContext.getInstrumentClass(loader, className, classfileBuffer);
target.addInterceptor("com.navercorp.pinpoint.plugin.logback.interceptor.LoggingEventOfLogbackInterceptor");
return target.toBytecode();
}
});
}
}
@@ -13,45 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.profiler.modifier.log.logback.interceptor;
package com.navercorp.pinpoint.plugin.logback.interceptor;
import org.slf4j.MDC;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.SimpleAroundInterceptor;
import com.navercorp.pinpoint.bootstrap.interceptor.TargetClassLoader;
import com.navercorp.pinpoint.bootstrap.interceptor.TraceContextSupport;
import com.navercorp.pinpoint.profiler.modifier.log.MdcKey;
import com.navercorp.pinpoint.bootstrap.interceptor.BeforeInterceptor0;
import com.navercorp.pinpoint.bootstrap.plugin.annotation.TargetConstructor;
import com.navercorp.pinpoint.bootstrap.plugin.annotation.Targets;
/**
* @author minwoo.jung
*/
public class LoggingEventOfLogbackInterceptor implements SimpleAroundInterceptor, TraceContextSupport, TargetClassLoader {
@Targets(constructors={
@TargetConstructor({}),
@TargetConstructor({"java.lang.String", "ch.qos.logback.classic.Logger", "ch.qos.logback.classic.Level", "java.lang.String", "java.lang.Throwable", "java.lang.Object[]"})
})
public class LoggingEventOfLogbackInterceptor implements BeforeInterceptor0 {
private static final String TRANSACTION_ID = "PtxId";
private static final String SPAN_ID = "PspanId";
private TraceContext traceContext;
private final TraceContext traceContext;
@Override
public void before(Object target, Object[] args) {
Trace trace = traceContext.currentTraceObject();
if (trace == null) {
MDC.remove(MdcKey.TRANSACTION_ID);
MDC.remove(MdcKey.SPAN_ID);
return;
} else {
MDC.put(MdcKey.TRANSACTION_ID, trace.getTraceId().getTransactionId());
MDC.put(MdcKey.SPAN_ID, String.valueOf(trace.getTraceId().getSpanId()));
}
}
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
}
@Override
public void setTraceContext(TraceContext traceContext) {
public LoggingEventOfLogbackInterceptor(TraceContext traceContext) {
this.traceContext = traceContext;
}
@Override
public void before(Object target) {
Trace trace = traceContext.currentTraceObject();
if (trace == null) {
MDC.remove(TRANSACTION_ID);
MDC.remove(SPAN_ID);
return;
} else {
MDC.put(TRANSACTION_ID, trace.getTraceId().getTransactionId());
MDC.put(SPAN_ID, String.valueOf(trace.getTraceId().getSpanId()));
}
}
}
@@ -0,0 +1 @@
com.navercorp.pinpoint.plugin.logback.LogbackPlugin
+6
View File
@@ -37,6 +37,7 @@
<module>mybatis</module>
<module>okhttp</module>
<module>log4j</module>
<module>logback</module>
</modules>
<dependencies>
@@ -165,6 +166,11 @@
<artifactId>pinpoint-log4j-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-logback-plugin</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,57 @@
/*
* Copyright 2014 NAVER Corp.
*
* 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 com.navercorp.pinpoint.profiler.modifier.log.logback.interceptor;
import org.slf4j.MDC;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.SimpleAroundInterceptor;
import com.navercorp.pinpoint.bootstrap.interceptor.TargetClassLoader;
import com.navercorp.pinpoint.bootstrap.interceptor.TraceContextSupport;
import com.navercorp.pinpoint.profiler.modifier.log.MdcKey;
/**
* @author minwoo.jung
*/
public class LoggingEventOfLogbackInterceptor implements SimpleAroundInterceptor, TraceContextSupport, TargetClassLoader {
private TraceContext traceContext;
@Override
public void before(Object target, Object[] args) {
Trace trace = traceContext.currentTraceObject();
if (trace == null) {
MDC.remove(MdcKey.TRANSACTION_ID);
MDC.remove(MdcKey.SPAN_ID);
return;
} else {
MDC.put(MdcKey.TRANSACTION_ID, trace.getTraceId().getTransactionId());
MDC.put(MdcKey.SPAN_ID, String.valueOf(trace.getTraceId().getSpanId()));
}
}
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
}
@Override
public void setTraceContext(TraceContext traceContext) {
this.traceContext = traceContext;
}
}