未验证 提交 16b51d55 编写于 作者: H hailin0 提交者: GitHub

Support print SkyWalking context to logs (#6795)

上级 2d2ded44
......@@ -13,7 +13,8 @@ Release Notes.
* Add an agent plugin to support elasticsearch7.
* Add `jsonrpc4j` agent plugin.
* Add Seata in the component definition. Seata plugin hosts on Seata project.
* Extended Kafka plugin to properly trace consumers that have topic partitions directly assigned
* Extended Kafka plugin to properly trace consumers that have topic partitions directly assigned.
* Support print SkyWalking context to logs.
#### OAP-Backend
* BugFix: filter invalid Envoy access logs whose socket address is empty.
......@@ -30,6 +31,7 @@ Release Notes.
#### Documentation
* Polish k8s monitoring otel-collector configuration example.
* Print SkyWalking context to logs configuration example.
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/84?closed=1)
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.log.log4j.v1.x;
import org.apache.log4j.helpers.PatternConverter;
import org.apache.log4j.spi.LoggingEvent;
/**
* Default implementation outputs "SW_CTX: N/A". But, if in SkyWalking agent active mode, output will become the real
* SkyWalking context.
* <p>
*/
public class SkyWalkingContextPatternConverter extends PatternConverter {
@Override
protected String convert(LoggingEvent loggingEvent) {
return "SW_CTX: N/A";
}
}
......@@ -22,7 +22,7 @@ import org.apache.log4j.helpers.PatternConverter;
import org.apache.log4j.spi.LoggingEvent;
/**
* Default implementation outputs "TID: N/A". But, if in sky-walking agent active mode, output will become the real
* Default implementation outputs "TID: N/A". But, if in SkyWalking agent active mode, output will become the real
* ids.
* <p>
*/
......
......@@ -22,7 +22,7 @@ import org.apache.log4j.PatternLayout;
import org.apache.log4j.helpers.PatternParser;
/**
* The log4j extend pattern. By using this pattern, if sky-walking agent is also active, {@link
* The log4j extend pattern. By using this pattern, if SkyWalking agent is also active, {@link
* PatternParser#finalizeConverter(char)} method will be override dynamic. <p>
*/
public class TraceIdPatternLayout extends PatternLayout {
......
......@@ -21,10 +21,12 @@ package org.apache.skywalking.apm.toolkit.log.log4j.v1.x;
import org.apache.log4j.helpers.PatternParser;
/**
* Base on '%T', use {@link TraceIdPatternConverter} to convert the '%t' to traceId.
* Base on '%T', use {@link TraceIdPatternConverter} to convert the '%T' to traceId or '%T{SW_CTX}' to SkyWalking context.
* <p>
*/
public class TraceIdPatternParser extends PatternParser {
private static final String SKYWALKING_CONTEXT_OPTION = "SW_CTX";
public TraceIdPatternParser(String pattern) {
super(pattern);
}
......@@ -32,7 +34,12 @@ public class TraceIdPatternParser extends PatternParser {
@Override
protected void finalizeConverter(char c) {
if ('T' == c) {
addConverter(new TraceIdPatternConverter());
String option = super.extractOption();
if (option != null && option.equals(SKYWALKING_CONTEXT_OPTION)) {
addConverter(new SkyWalkingContextPatternConverter());
} else {
addConverter(new TraceIdPatternConverter());
}
} else {
super.finalizeConverter(c);
}
......
......@@ -20,7 +20,7 @@ package org.apache.skywalking.apm.toolkit.log.log4j.v2.x;
public class Log4j2OutputAppender {
/**
* As default, append "TID: N/A" to the output message, if sky-walking agent in active mode, append the real traceId
* As default, append "TID: N/A" to the output message, if SkyWalking agent in active mode, append the real traceId
* in the recent Context, if existed, or empty String.
*
* @param toAppendTo origin output message.
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.log.log4j.v2.x;
public class Log4j2SkyWalkingContextOutputAppender {
/**
* As default, append "SW_CTX: N/A" to the output message, if SkyWalking agent in active mode, append the real SkyWalking context
* in the recent Context, if existed, or empty String.
*
* @param toAppendTo origin output message.
*/
public static void append(StringBuilder toAppendTo) {
toAppendTo.append("SW_CTX: N/A");
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.log.log4j.v2.x;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
/**
* {@link SkyWalkingContextConverter} is a log4j2 plugin, by annotation as {@link Plugin}. It convert the pattern key: sw_ctx.
* Use '%sw_ctx' in log4j2's config. '%sw_ctx' will output as SW_CTX:xxxx
* <p>
*/
@Plugin(name = "SkyWalkingContextConverter", category = "Converter")
@ConverterKeys({"sw_ctx"})
public class SkyWalkingContextConverter extends LogEventPatternConverter {
/**
* Constructs an instance of LoggingEventPatternConverter.
*
* @param name name of converter.
* @param style CSS style for output.
*/
protected SkyWalkingContextConverter(String name, String style) {
super(name, style);
}
public static SkyWalkingContextConverter newInstance(String[] options) {
return new SkyWalkingContextConverter("sw_ctx", "sw_ctx");
}
@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
Log4j2SkyWalkingContextOutputAppender.append(toAppendTo);
}
}
......@@ -23,7 +23,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
public class LogbackPatternConverter extends ClassicConverter {
/**
* As default, return "TID: N/A" to the output message, if sky-walking agent in active mode, return the real traceId
* As default, return "TID: N/A" to the output message, if SkyWalking agent in active mode, return the real traceId
* in the recent Context, if existed.
*
* @param iLoggingEvent the event
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.log.logback.v1.x;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
public class LogbackSkyWalkingContextPatternConverter extends ClassicConverter {
/**
* As default, return "SW_CTX: N/A" to the output message, if SkyWalking agent in active mode, return the real SkyWalking context
* in the recent Context, if existed.
*
* @param iLoggingEvent the event
* @return the SkyWalking context: N/A, or the real SkyWalking context.
*/
@Override
public String convert(ILoggingEvent iLoggingEvent) {
return "SW_CTX: N/A";
}
}
......@@ -22,11 +22,12 @@ import ch.qos.logback.classic.PatternLayout;
/**
* Based on the logback-compoenent convert register mechanism, register {@link LogbackPatternConverter} as a new
* convert, match to "tid". You can use "%tid" in logback config file, "Pattern" section.
* convert, match to "tid" and "sw_ctx". You can use "%tid" or "sw_ctx" in logback config file, "Pattern" section.
* <p>
*/
public class TraceIdPatternLogbackLayout extends PatternLayout {
static {
defaultConverterMap.put("tid", LogbackPatternConverter.class.getName());
defaultConverterMap.put("sw_ctx", LogbackSkyWalkingContextPatternConverter.class.getName());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.fasterxml.jackson.core.JsonGenerator;
import net.logstash.logback.composite.AbstractFieldJsonProvider;
import net.logstash.logback.composite.FieldNamesAware;
import net.logstash.logback.composite.JsonWritingUtils;
import net.logstash.logback.fieldnames.LogstashFieldNames;
import java.io.IOException;
import java.util.Map;
public class SkyWalkingContextJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> implements FieldNamesAware<LogstashFieldNames> {
public static final String SKYWALKING_CONTEXT = "SW_CTX";
@Override
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
String skyWalkingContext = getSkyWalkingContext(event);
JsonWritingUtils.writeStringField(generator, getFieldName(), skyWalkingContext);
}
@Override
public void setFieldNames(LogstashFieldNames fieldNames) {
setFieldName(SKYWALKING_CONTEXT);
}
public String getSkyWalkingContext(ILoggingEvent event) {
Map<String, String> map = event.getLoggerContextVO().getPropertyMap();
return map.get(SKYWALKING_CONTEXT);
}
}
......@@ -23,25 +23,41 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.util.OptionHelper;
public class LogbackMDCPatternConverter extends MDCConverter {
private static final String CONVERT_KEY = "tid";
private static final String CONVERT_TRACE_ID_KEY = "tid";
private static final String CONVERT_SKYWALKING_CONTEXT_KEY = "sw_ctx";
private boolean convert4TID = false;
private boolean convert4SWCTX = false;
@Override
public void start() {
super.start();
String[] key = OptionHelper.extractDefaultReplacement(getFirstOption());
if (null != key && key.length > 0 && CONVERT_KEY.equals(key[0])) {
convert4TID = true;
if (null != key && key.length > 0) {
String variableName = key[0];
if (CONVERT_TRACE_ID_KEY.equals(variableName)) {
convert4TID = true;
} else if (CONVERT_SKYWALKING_CONTEXT_KEY.equals(variableName)) {
convert4SWCTX = true;
}
}
}
@Override
public String convert(ILoggingEvent iLoggingEvent) {
return convert4TID ? convertTID(iLoggingEvent) : super.convert(iLoggingEvent);
if (convert4TID) {
return convertTID(iLoggingEvent);
} else if (convert4SWCTX) {
return convertSkyWalkingContext(iLoggingEvent);
}
return super.convert(iLoggingEvent);
}
public String convertTID(ILoggingEvent iLoggingEvent) {
return "TID: N/A";
}
public String convertSkyWalkingContext(ILoggingEvent iLoggingEvent) {
return "SW_CTX: N/A";
}
}
......@@ -21,10 +21,11 @@ package org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc;
import ch.qos.logback.classic.PatternLayout;
/**
* Override "X",SuperClass run before Subclass.
* Override "X" and "mdc",SuperClass run before Subclass.
*/
public class TraceIdMDCPatternLogbackLayout extends PatternLayout {
static {
defaultConverterMap.put("X", LogbackMDCPatternConverter.class.getName());
defaultConverterMap.put("mdc", LogbackMDCPatternConverter.class.getName());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.log4j.v1.x;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
public class PrintSkyWalkingContextInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
return "SW_CTX:" + new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(),
ContextManager.getSpanId())
.toString();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.log4j.v1.x;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* Active the toolkit class "SkyWalkingContextPatternConverter". Should not dependency or import any class in
* "skywalking-toolkit-log4j-1.x" module. Activation's classloader is diff from "SkyWalkingContextPatternConverter", using direct
* will trigger classloader issue.
*/
public class SkyWalkingContextPatternConverterActivation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.log.log4j.v1.x.SkyWalkingContextPatternConverter";
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.log4j.v1.x.PrintSkyWalkingContextInterceptor";
public static final String ENHANCE_METHOD = "convert";
/**
* @return the target class, which needs active.
*/
@Override
protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
/**
* @return null, no need to intercept constructor of enhance class.
*/
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return null;
}
/**
* @return the collection of {@link InstanceMethodsInterceptPoint}, represent the intercepted methods and their
* interceptors.
*/
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
}
......@@ -15,4 +15,5 @@
# limitations under the License.
toolkit-log4j=org.apache.skywalking.apm.toolkit.activation.log.log4j.v1.x.TraceIdPatternConverterActivation
toolkit-log4j=org.apache.skywalking.apm.toolkit.activation.log.log4j.v1.x.SkyWalkingContextPatternConverterActivation
toolkit-log4j=org.apache.skywalking.apm.toolkit.activation.log.log4j.v1.x.log.GRPCLogAppenderActivation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
public class SkyWalkingContextConverterActivation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.SkyWalkingContextConverterMethodInterceptor";
private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.log.log4j.v2.x.SkyWalkingContextConverter";
private static final String ENHANCE_METHOD = "format";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
public class SkyWalkingContextConverterMethodInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
String skyWalkingContextStr = "";
//Async Thread, where ContextManager is not active
if (!ContextManager.isActive() && allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext == null) {
skyWalkingContextStr = "N/A";
} else {
skyWalkingContextStr = skyWalkingContext.toString();
}
} else {
skyWalkingContextStr = new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(),
ContextManager.getSpanId())
.toString();
}
((StringBuilder) allArguments[1]).append("SW_CTX: ").append(skyWalkingContextStr);
result.defineReturnValue(null);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
......@@ -22,6 +22,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
......@@ -34,9 +35,11 @@ public class TraceIdConverterMethodInterceptor implements InstanceMethodsAroundI
//Async Thread, where ContextManager is not active
if (!ContextManager.isActive() && allArguments[0] instanceof EnhancedInstance) {
tid = (String) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (tid == null) {
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext == null) {
tid = "N/A";
} else {
tid = skyWalkingContext.getTraceId();
}
} else {
tid = ContextManager.getGlobalTraceId();
......
......@@ -22,6 +22,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
......@@ -31,8 +32,11 @@ public class AsyncAppenderMethodInterceptor implements InstanceMethodsAroundInte
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
if (allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(), ContextManager.getSpanId());
EnhancedInstance instances = (EnhancedInstance) allArguments[0];
instances.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
instances.setSkyWalkingDynamicField(skyWalkingContext);
}
}
......
......@@ -22,11 +22,12 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
/**
* <p>Pass the global trace Id into the _sw field of Lo4jLogEvent instance after enhancing</p>
* <p>Pass the global trace context into the _sw field of Lo4jLogEvent instance after enhancing</p>
*/
public class AsyncLoggerConfigMethodInterceptor implements InstanceMethodsAroundInterceptor {
......@@ -35,8 +36,11 @@ public class AsyncLoggerConfigMethodInterceptor implements InstanceMethodsAround
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
if (allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(), ContextManager.getSpanId());
EnhancedInstance instances = (EnhancedInstance) allArguments[0];
instances.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
instances.setSkyWalkingDynamicField(skyWalkingContext);
}
}
......
......@@ -21,6 +21,7 @@ package org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
/**
* Log4jLogEvent implements LogEvent, which is a message in the Disruptor Array of the AsyncLoggerConfigDisruptor.class,
......@@ -32,6 +33,8 @@ public class Log4jLogEventConstructorInterceptor implements InstanceConstructorI
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
objInst.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
SkyWalkingContext skyWalkingContext = new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(), ContextManager.getSpanId());
objInst.setSkyWalkingDynamicField(skyWalkingContext);
}
}
......@@ -22,11 +22,12 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
/**
* <p>Pass the global trace Id into the _sw field of RingBufferLogEvent instance after enhancing</p>
* <p>Pass the global trace context into the _sw field of RingBufferLogEvent instance after enhancing</p>
*/
public class RingBufferLogEventMethodInterceptor implements InstanceMethodsAroundInterceptor {
......@@ -34,7 +35,9 @@ public class RingBufferLogEventMethodInterceptor implements InstanceMethodsAroun
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
objInst.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
SkyWalkingContext skyWalkingContext = new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(), ContextManager.getSpanId());
objInst.setSkyWalkingDynamicField(skyWalkingContext);
}
@Override
......
......@@ -15,6 +15,7 @@
# limitations under the License.
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.TraceIdConverterActivation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.SkyWalkingContextConverterActivation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.AsyncLoggerConfigInstrumentation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.Log4jLogEventInstrumentation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.RingBufferLogEventInstrumentation
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* Active the toolkit class "org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackSkyWalkingContextPatternConverter". Should not
* dependency or import any class in "skywalking-toolkit-logback-1.x" module. Activation's classloader is diff from
* "org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackSkyWalkingContextPatternConverter", using direct will trigger classloader
* issue.
* <p>
*/
public class LogbackSkyWalkingContextPatternConverterActivation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.PrintSkyWalkingContextInterceptor";
public static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackSkyWalkingContextPatternConverter";
public static final String ENHANCE_METHOD = "convert";
/**
* @return the target class, which needs active.
*/
@Override
protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
/**
* @return null, no need to intercept constructor of enhance class.
*/
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return null;
}
/**
* @return the collection of {@link StaticMethodsInterceptPoint}, represent the intercepted methods and their
* interceptors.
*/
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD).and(takesArgumentWithType(0, "ch.qos.logback.classic.spi.ILoggingEvent"));
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
public class PrintSkyWalkingContextInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (!ContextManager.isActive()) {
if (allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext != null) {
return "SW_CTX:" + skyWalkingContext.toString();
}
}
}
return "SW_CTX:" + new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(),
ContextManager.getSpanId())
.toString();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
......@@ -22,6 +22,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
......@@ -38,9 +39,9 @@ public class PrintTraceIdInterceptor implements InstanceMethodsAroundInterceptor
Object ret) throws Throwable {
if (!ContextManager.isActive()) {
if (allArguments[0] instanceof EnhancedInstance) {
String tid = (String) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (tid != null) {
return "TID:" + tid;
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext != null) {
return "TID:" + skyWalkingContext.getTraceId();
}
}
}
......
......@@ -22,6 +22,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
......@@ -31,8 +32,11 @@ public class AsyncAppenderBaseMethodInterceptor implements InstanceMethodsAround
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
if (allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(), ContextManager.getSpanId());
EnhancedInstance instances = (EnhancedInstance) allArguments[0];
instances.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
instances.setSkyWalkingDynamicField(skyWalkingContext);
}
}
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
public class SkyWalkingContextJsonProviderActivation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash.SkyWalkingContextJsonProviderInterceptor";
public static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.SkyWalkingContextJsonProvider";
public static final String ENHANCE_METHOD = "getSkyWalkingContext";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return null;
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
public class SkyWalkingContextJsonProviderInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (ret != null && !"N/A".equals(ret)) {
return ret;
}
if (!ContextManager.isActive() && allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext != null) {
return skyWalkingContext.toString();
}
}
return new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(),
ContextManager.getSpanId())
.toString();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
......@@ -20,10 +20,13 @@ package org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash;
import ch.qos.logback.classic.spi.ILoggingEvent;
import java.lang.reflect.Method;
import java.util.Map;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
public class TcpSocketAppenderInterceptor implements InstanceMethodsAroundInterceptor {
......@@ -33,7 +36,10 @@ public class TcpSocketAppenderInterceptor implements InstanceMethodsAroundInterc
ILoggingEvent event = (ILoggingEvent) allArguments[0];
if (event != null && event.getLoggerContextVO() != null && event.getLoggerContextVO()
.getPropertyMap() != null) {
event.getLoggerContextVO().getPropertyMap().put("TID", ContextManager.getGlobalTraceId());
Map<String, String> propertyMap = event.getLoggerContextVO().getPropertyMap();
propertyMap.put("TID", ContextManager.getGlobalTraceId());
propertyMap.put("SW_CTX", new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(), ContextManager.getSpanId()).toString());
}
}
......
......@@ -22,6 +22,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
......@@ -39,9 +40,9 @@ public class TraceIdJsonProviderInterceptor implements InstanceMethodsAroundInte
return ret;
}
if (!ContextManager.isActive() && allArguments[0] instanceof EnhancedInstance) {
String tid = (String) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (tid != null) {
return tid;
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext != null) {
return skyWalkingContext.getTraceId();
}
}
return ContextManager.getGlobalTraceId();
......
......@@ -34,9 +34,11 @@ import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentType
*/
public class MDCConverterActivation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc.PrintMDCTraceIdInterceptor";
public static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.LogbackMDCPatternConverter";
public static final String ENHANCE_METHOD = "convertTID";
public static final String ENHANCE_TID_METHOD = "convertTID";
public static final String INTERCEPT_TID_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc.PrintMDCTraceIdInterceptor";
public static final String ENHANCE_SKYWALKING_CONTEXT_METHOD = "convertSkyWalkingContext";
public static final String INTERCEPT_SKYWALKING_CONTEXT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc.PrintMDCSkyWalkingContextInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
......@@ -49,12 +51,28 @@ public class MDCConverterActivation extends ClassInstanceMethodsEnhancePluginDef
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD).and(takesArgumentWithType(0, "ch.qos.logback.classic.spi.ILoggingEvent"));
return named(ENHANCE_TID_METHOD).and(takesArgumentWithType(0, "ch.qos.logback.classic.spi.ILoggingEvent"));
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
return INTERCEPT_TID_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_SKYWALKING_CONTEXT_METHOD).and(takesArgumentWithType(0, "ch.qos.logback.classic.spi.ILoggingEvent"));
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_SKYWALKING_CONTEXT_CLASS;
}
@Override
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
public class PrintMDCSkyWalkingContextInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (!ContextManager.isActive()) {
if (allArguments[0] instanceof EnhancedInstance) {
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext != null) {
return "SW_CTX:" + skyWalkingContext.toString();
}
}
}
return "SW_CTX:" + new SkyWalkingContext(ContextManager.getGlobalTraceId(),
ContextManager.getSegmentId(),
ContextManager.getSpanId())
.toString();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
......@@ -22,6 +22,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.toolkit.logging.common.log.SkyWalkingContext;
import java.lang.reflect.Method;
......@@ -37,9 +38,9 @@ public class PrintMDCTraceIdInterceptor implements InstanceMethodsAroundIntercep
Object ret) throws Throwable {
if (!ContextManager.isActive()) {
if (allArguments[0] instanceof EnhancedInstance) {
String tid = (String) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (tid != null) {
return "TID:" + tid;
SkyWalkingContext skyWalkingContext = (SkyWalkingContext) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (skyWalkingContext != null) {
return "TID:" + skyWalkingContext.getTraceId();
}
}
}
......
......@@ -15,9 +15,11 @@
# limitations under the License.
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.LogbackPatternConverterActivation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.LogbackSkyWalkingContextPatternConverterActivation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.mdc.MDCConverterActivation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.async.AsyncAppenderBaseInstrumentation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.async.LoggingEventInstrumentation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash.TcpSocketAppenderActivation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash.TraceIdJsonProviderActivation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash.SkyWalkingContextJsonProviderActivation
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.log.GRPCLogAppenderActivation
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.toolkit.logging.common.log;
import org.apache.skywalking.apm.agent.core.conf.Config;
public class SkyWalkingContext {
private final String serviceName = Config.Agent.SERVICE_NAME;
private final String instanceName = Config.Agent.INSTANCE_NAME;
private String traceId;
private String traceSegmentId;
private int spanId;
public SkyWalkingContext(String traceId, String traceSegmentId, int spanId) {
this.traceId = traceId;
this.traceSegmentId = traceSegmentId;
this.spanId = spanId;
}
public String getTraceId() {
return traceId;
}
@Override
public String toString() {
if (-1 == spanId) {
return "[" + String.join(",", serviceName, instanceName, "N/A", "N/A", "-1") + "]";
}
return "[" + String.join(",", serviceName, instanceName, traceId, traceSegmentId, String.valueOf(spanId)) + "]";
}
}
......@@ -11,7 +11,7 @@
* Config a layout
```properties
log4j.appender.CONSOLE.layout=TraceIdPatternLayout
log4j.appender.CONSOLE.layout=org.apache.skywalking.apm.toolkit.log.log4j.v1.x.TraceIdPatternLayout
```
* set `%T` in `layout.ConversionPattern` ( In 2.0-2016, you should use %x, [Why change?](https://github.com/wu-sheng/sky-walking/issues/77) )
......@@ -19,7 +19,13 @@ log4j.appender.CONSOLE.layout=TraceIdPatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%T] %-5p %c{1}:%L - %m%n
```
* When you use `-javaagent` to active the sky-walking tracer, log4j will output **traceId**, if it existed. If the tracer is inactive, the output will be `TID: N/A`.
* When you use `-javaagent` to active the SkyWalking tracer, log4j will output **traceId**, if it existed. If the tracer is inactive, the output will be `TID: N/A`.
# Print SkyWalking context in your logs
* Your only need to replace pattern `%T` with `%T{SW_CTX}`.
* When you use `-javaagent` to active the SkyWalking tracer, log4j will output `SW_CTX: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId]`, if it existed. If the tracer is inactive, the output will be `SW_CTX: N/A`.
# gRPC reporter
......
......@@ -102,7 +102,13 @@
</Loggers>
</Configuration>
```
* When you use `-javaagent` to active the sky-walking tracer, log4j2 will output **traceId**, if it existed. If the tracer is inactive, the output will be `TID: N/A`.
* When you use `-javaagent` to active the SkyWalking tracer, log4j2 will output **traceId**, if it existed. If the tracer is inactive, the output will be `TID: N/A`.
# Print SkyWalking context in your logs
* Your only need to replace pattern `%traceId` with `%sw_ctx`.
* When you use `-javaagent` to active the SkyWalking tracer, log4j2 will output `SW_CTX: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId]`, if it existed. If the tracer is inactive, the output will be `SW_CTX: N/A`.
# gRPC reporter
......
......@@ -57,7 +57,13 @@
</configuration>
```
* When you use `-javaagent` to active the sky-walking tracer, logback will output **traceId**, if it existed. If the tracer is inactive, the output will be `TID: N/A`.
* When you use `-javaagent` to active the SkyWalking tracer, logback will output **traceId**, if it existed. If the tracer is inactive, the output will be `TID: N/A`.
# Print SkyWalking context in your logs
* Your only need to replace pattern `%tid` or `%X{tid]}` with `%sw_ctx` or `%X{sw_ctx}`.
* When you use `-javaagent` to active the SkyWalking tracer, logback will output `SW_CTX: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId]`, if it existed. If the tracer is inactive, the output will be `SW_CTX: N/A`.
# logstash logback plugin
......@@ -75,17 +81,23 @@
```xml
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
<!-- add TID(traceId) field -->
<provider class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.TraceIdJsonProvider">
</provider>
<!-- add SW_CTX(SkyWalking context) field -->
<provider class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.SkyWalkingContextJsonProvider">
</provider>
</encoder>
```
* set `LoggingEventCompositeJsonEncoder` of logstash in logback-spring.xml for custom json format
1.add converter for %tid as child of <configuration> node
1.add converter for %tid or %sw_ctx as child of <configuration> node
```xml
<!--add converter for %tid -->
<conversionRule conversionWord="tid" converterClass="org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackPatternConverter"/>
<!-- add converter for %tid -->
<conversionRule conversionWord="tid" converterClass="org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackPatternConverter"/>
<!-- add converter for %sw_ctx -->
<conversionRule conversionWord="sw_ctx" converterClass="org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackSkyWalkingContextPatternConverter"/>
```
2.add json encoder for custom json format
......@@ -98,12 +110,13 @@
<pattern>
<pattern>
{
"level": "%level",
"tid": "%tid",
"thread": "%thread",
"class": "%logger{1.}:%L",
"message": "%message",
"stackTrace": "%exception{10}"
"level": "%level",
"tid": "%tid",
"skyWalkingContext": "%sw_ctx",
"thread": "%thread",
"class": "%logger{1.}:%L",
"message": "%message",
"stackTrace": "%exception{10}"
}
</pattern>
</pattern>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册