Skip to content

Commit 8472f5c

Browse files
authored
Merge pull request apache#567 from zhangkewei/master
support for logback MDC
2 parents 329e53c + 6b9ef38 commit 8472f5c

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.toolkit.log.logback.v1.x.mdc;
20+
21+
import ch.qos.logback.classic.pattern.MDCConverter;
22+
import ch.qos.logback.classic.spi.ILoggingEvent;
23+
import ch.qos.logback.core.util.OptionHelper;
24+
25+
/**
26+
* @author zhangkewei
27+
*/
28+
public class LogbackMDCPatternConverter extends MDCConverter {
29+
private static final String CONVERT_KEY = "tid";
30+
31+
private boolean convert4TID = false;
32+
@Override
33+
public void start() {
34+
super.start();
35+
String[] key = OptionHelper.extractDefaultReplacement(getFirstOption());
36+
if (null != key && key.length > 0 && CONVERT_KEY.equals(key[0])) {
37+
convert4TID = true;
38+
}
39+
}
40+
@Override
41+
public String convert(ILoggingEvent iLoggingEvent) {
42+
return convert4TID ? convertTID() : super.convert(iLoggingEvent);
43+
}
44+
45+
public String convertTID() {
46+
return "TID: N/A";
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.toolkit.log.logback.v1.x.mdc;
20+
21+
import ch.qos.logback.classic.PatternLayout;
22+
23+
/**
24+
* Override "X",SuperClass run before Subclass.
25+
* @author zhangkewei
26+
*/
27+
public class TraceIdMDCPatternLogbackLayout extends PatternLayout {
28+
static {
29+
defaultConverterMap.put("X", LogbackMDCPatternConverter.class.getName());
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
package org.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc;
19+
20+
import net.bytebuddy.description.method.MethodDescription;
21+
import net.bytebuddy.matcher.ElementMatcher;
22+
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
23+
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
24+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
25+
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
26+
27+
import static net.bytebuddy.matcher.ElementMatchers.named;
28+
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
29+
30+
/**
31+
* Support MDC https://logback.qos.ch/manual/mdc.html
32+
* @author: zhangkewei
33+
*/
34+
public class MDCConverterActivation extends ClassInstanceMethodsEnhancePluginDefine {
35+
36+
@Override
37+
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
38+
return null;
39+
}
40+
41+
@Override
42+
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
43+
return new InstanceMethodsInterceptPoint[] {
44+
new InstanceMethodsInterceptPoint() {
45+
@Override
46+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
47+
return named("convertTID");
48+
}
49+
50+
@Override
51+
public String getMethodsInterceptor() {
52+
return "org.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc.PrintMDCTraceIdInterceptor";
53+
}
54+
55+
@Override public boolean isOverrideArgs() {
56+
return false;
57+
}
58+
}
59+
};
60+
}
61+
62+
@Override
63+
protected ClassMatch enhanceClass() {
64+
return byName("org.skywalking.apm.toolkit.log.logback.v1.x.mdc.LogbackMDCPatternConverter");
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
package org.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc;
19+
20+
import org.skywalking.apm.agent.core.context.ContextManager;
21+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
22+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
23+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
24+
25+
import java.lang.reflect.Method;
26+
27+
/**
28+
* @author zhangkewei
29+
*/
30+
public class PrintMDCTraceIdInterceptor implements InstanceMethodsAroundInterceptor {
31+
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
32+
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
33+
34+
}
35+
36+
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
37+
Class<?>[] argumentsTypes, Object ret) throws Throwable {
38+
return "TID:" + ContextManager.getGlobalTraceId();
39+
}
40+
41+
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
42+
Class<?>[] argumentsTypes, Throwable t) {
43+
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
toolkit-logback=org.skywalking.apm.toolkit.activation.log.logback.v1.x.LogbackPatternConverterActivation
2+
toolkit-logback=org.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc.MDCConverterActivation

0 commit comments

Comments
 (0)