diff --git a/CHANGES.md b/CHANGES.md
index 1de6eaf7a..20e778874 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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)
diff --git a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/SkyWalkingContextPatternConverter.java b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/SkyWalkingContextPatternConverter.java
new file mode 100644
index 000000000..2b9e7255c
--- /dev/null
+++ b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/SkyWalkingContextPatternConverter.java
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ *
+ */
+
+public class SkyWalkingContextPatternConverter extends PatternConverter {
+ @Override
+ protected String convert(LoggingEvent loggingEvent) {
+ return "SW_CTX: N/A";
+ }
+}
diff --git a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternConverter.java b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternConverter.java
index 5fe6e9892..a653497a1 100644
--- a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternConverter.java
+++ b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternConverter.java
@@ -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.
*
*/
diff --git a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternLayout.java b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternLayout.java
index 172d2a4cf..5c4e44927 100644
--- a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternLayout.java
+++ b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternLayout.java
@@ -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.
*/
public class TraceIdPatternLayout extends PatternLayout {
diff --git a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternParser.java b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternParser.java
index 173cb526b..96fc4f720 100644
--- a/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternParser.java
+++ b/apm-application-toolkit/apm-toolkit-log4j-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v1/x/TraceIdPatternParser.java
@@ -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.
*
*/
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);
}
diff --git a/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2OutputAppender.java b/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2OutputAppender.java
index bc3d017bf..68253c93d 100644
--- a/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2OutputAppender.java
+++ b/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2OutputAppender.java
@@ -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.
diff --git a/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2SkyWalkingContextOutputAppender.java b/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2SkyWalkingContextOutputAppender.java
new file mode 100644
index 000000000..64b9fea8e
--- /dev/null
+++ b/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/Log4j2SkyWalkingContextOutputAppender.java
@@ -0,0 +1,31 @@
+/*
+ * 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");
+ }
+}
diff --git a/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/SkyWalkingContextConverter.java b/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/SkyWalkingContextConverter.java
new file mode 100644
index 000000000..6912a4f45
--- /dev/null
+++ b/apm-application-toolkit/apm-toolkit-log4j-2.x/src/main/java/org/apache/skywalking/apm/toolkit/log/log4j/v2/x/SkyWalkingContextConverter.java
@@ -0,0 +1,53 @@
+/*
+ * 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
+ *
+ */
+@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);
+ }
+}
diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackPatternConverter.java b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackPatternConverter.java
index bde970e8b..2240a3319 100644
--- a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackPatternConverter.java
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackPatternConverter.java
@@ -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
diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackSkyWalkingContextPatternConverter.java b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackSkyWalkingContextPatternConverter.java
new file mode 100644
index 000000000..998cce5e0
--- /dev/null
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackSkyWalkingContextPatternConverter.java
@@ -0,0 +1,36 @@
+/*
+ * 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";
+ }
+}
diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/TraceIdPatternLogbackLayout.java b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/TraceIdPatternLogbackLayout.java
index b35090912..e49ce2e96 100644
--- a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/TraceIdPatternLogbackLayout.java
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/TraceIdPatternLogbackLayout.java
@@ -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.
*
*/
public class TraceIdPatternLogbackLayout extends PatternLayout {
static {
defaultConverterMap.put("tid", LogbackPatternConverter.class.getName());
+ defaultConverterMap.put("sw_ctx", LogbackSkyWalkingContextPatternConverter.class.getName());
}
}
diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/logstash/SkyWalkingContextJsonProvider.java b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/logstash/SkyWalkingContextJsonProvider.java
new file mode 100644
index 000000000..32ede80e6
--- /dev/null
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/logstash/SkyWalkingContextJsonProvider.java
@@ -0,0 +1,50 @@
+/*
+ * 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 implements FieldNamesAware {
+
+ 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 map = event.getLoggerContextVO().getPropertyMap();
+ return map.get(SKYWALKING_CONTEXT);
+ }
+}
diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/LogbackMDCPatternConverter.java b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/LogbackMDCPatternConverter.java
index b45921ec4..3705bd0e9 100644
--- a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/LogbackMDCPatternConverter.java
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/LogbackMDCPatternConverter.java
@@ -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";
+ }
}
diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/TraceIdMDCPatternLogbackLayout.java b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/TraceIdMDCPatternLogbackLayout.java
index 737a05bbf..861705965 100644
--- a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/TraceIdMDCPatternLogbackLayout.java
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/mdc/TraceIdMDCPatternLogbackLayout.java
@@ -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());
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v1/x/PrintSkyWalkingContextInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v1/x/PrintSkyWalkingContextInterceptor.java
new file mode 100644
index 000000000..58f34582f
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v1/x/PrintSkyWalkingContextInterceptor.java
@@ -0,0 +1,51 @@
+/*
+ * 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) {
+
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v1/x/SkyWalkingContextPatternConverterActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v1/x/SkyWalkingContextPatternConverterActivation.java
new file mode 100644
index 000000000..9448182d2
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v1/x/SkyWalkingContextPatternConverterActivation.java
@@ -0,0 +1,83 @@
+/*
+ * 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 getMethodsMatcher() {
+ return named(ENHANCE_METHOD);
+ }
+
+ @Override
+ public String getMethodsInterceptor() {
+ return INTERCEPT_CLASS;
+ }
+
+ @Override
+ public boolean isOverrideArgs() {
+ return false;
+ }
+ }
+ };
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/resources/skywalking-plugin.def
index 1de7a3558..18cf1d198 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/resources/skywalking-plugin.def
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/src/main/resources/skywalking-plugin.def
@@ -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
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/SkyWalkingContextConverterActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/SkyWalkingContextConverterActivation.java
new file mode 100644
index 000000000..40e8cc080
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/SkyWalkingContextConverterActivation.java
@@ -0,0 +1,70 @@
+/*
+ * 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 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);
+ }
+}
+
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/SkyWalkingContextConverterMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/SkyWalkingContextConverterMethodInterceptor.java
new file mode 100644
index 000000000..a5bf1f4f7
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/SkyWalkingContextConverterMethodInterceptor.java
@@ -0,0 +1,64 @@
+/*
+ * 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) {
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterMethodInterceptor.java
index 425a54e1f..8be797e24 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterMethodInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterMethodInterceptor.java
@@ -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();
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncAppenderMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncAppenderMethodInterceptor.java
index fe2205da7..f1ba5c69a 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncAppenderMethodInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncAppenderMethodInterceptor.java
@@ -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);
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigMethodInterceptor.java
index 187872ae6..a27538ef6 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigMethodInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigMethodInterceptor.java
@@ -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;
/**
- * Pass the global trace Id into the _sw field of Lo4jLogEvent instance after enhancing
+ * Pass the global trace context into the _sw field of Lo4jLogEvent instance after enhancing
*/
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);
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventConstructorInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventConstructorInterceptor.java
index 40ca081a7..6000d6226 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventConstructorInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventConstructorInterceptor.java
@@ -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);
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventMethodInterceptor.java
index cd0f658f6..0c15c912b 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventMethodInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventMethodInterceptor.java
@@ -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;
/**
- * Pass the global trace Id into the _sw field of RingBufferLogEvent instance after enhancing
+ * Pass the global trace context into the _sw field of RingBufferLogEvent instance after enhancing
*/
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
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def
index 7b5e52c46..d19c9d20e 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def
@@ -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
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/LogbackSkyWalkingContextPatternConverterActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/LogbackSkyWalkingContextPatternConverterActivation.java
new file mode 100644
index 000000000..1a251966c
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/LogbackSkyWalkingContextPatternConverterActivation.java
@@ -0,0 +1,87 @@
+/*
+ * 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.
+ *
+ */
+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 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;
+ }
+ }
+ };
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintSkyWalkingContextInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintSkyWalkingContextInterceptor.java
new file mode 100644
index 000000000..b8c213718
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintSkyWalkingContextInterceptor.java
@@ -0,0 +1,59 @@
+/*
+ * 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) {
+
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java
index d323e0498..408e7741b 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java
@@ -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();
}
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/async/AsyncAppenderBaseMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/async/AsyncAppenderBaseMethodInterceptor.java
index a2d740bf1..e28d15f33 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/async/AsyncAppenderBaseMethodInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/async/AsyncAppenderBaseMethodInterceptor.java
@@ -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);
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/SkyWalkingContextJsonProviderActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/SkyWalkingContextJsonProviderActivation.java
new file mode 100644
index 000000000..50886a69f
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/SkyWalkingContextJsonProviderActivation.java
@@ -0,0 +1,68 @@
+/*
+ * 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 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);
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/SkyWalkingContextJsonProviderInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/SkyWalkingContextJsonProviderInterceptor.java
new file mode 100644
index 000000000..4f885f670
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/SkyWalkingContextJsonProviderInterceptor.java
@@ -0,0 +1,59 @@
+/*
+ * 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) {
+
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TcpSocketAppenderInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TcpSocketAppenderInterceptor.java
index 1205d48a8..e6987b985 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TcpSocketAppenderInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TcpSocketAppenderInterceptor.java
@@ -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 propertyMap = event.getLoggerContextVO().getPropertyMap();
+ propertyMap.put("TID", ContextManager.getGlobalTraceId());
+ propertyMap.put("SW_CTX", new SkyWalkingContext(ContextManager.getGlobalTraceId(),
+ ContextManager.getSegmentId(), ContextManager.getSpanId()).toString());
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TraceIdJsonProviderInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TraceIdJsonProviderInterceptor.java
index 81ff0f37e..2f860b740 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TraceIdJsonProviderInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/logstash/TraceIdJsonProviderInterceptor.java
@@ -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();
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/MDCConverterActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/MDCConverterActivation.java
index 5f26bbe83..237bf9968 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/MDCConverterActivation.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/MDCConverterActivation.java
@@ -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 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 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
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCSkyWalkingContextInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCSkyWalkingContextInterceptor.java
new file mode 100644
index 000000000..fc02ba792
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCSkyWalkingContextInterceptor.java
@@ -0,0 +1,57 @@
+/*
+ * 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) {
+
+ }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCTraceIdInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCTraceIdInterceptor.java
index 7b92da4b3..8f24fe1f0 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCTraceIdInterceptor.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/logback/v1/x/mdc/PrintMDCTraceIdInterceptor.java
@@ -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();
}
}
}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/resources/skywalking-plugin.def
index 385c91913..ac254dbfc 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/resources/skywalking-plugin.def
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/src/main/resources/skywalking-plugin.def
@@ -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
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/SkyWalkingContext.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/SkyWalkingContext.java
new file mode 100644
index 000000000..da0882322
--- /dev/null
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/SkyWalkingContext.java
@@ -0,0 +1,48 @@
+/*
+ * 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)) + "]";
+ }
+}
diff --git a/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-1.x.md b/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-1.x.md
index a3abcfa55..a14c23885 100644
--- a/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-1.x.md
+++ b/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-1.x.md
@@ -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
diff --git a/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-2.x.md b/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-2.x.md
index 5dcc4d326..d688ce84c 100644
--- a/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-2.x.md
+++ b/docs/en/setup/service-agent/java-agent/Application-toolkit-log4j-2.x.md
@@ -102,7 +102,13 @@
```
-* 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
diff --git a/docs/en/setup/service-agent/java-agent/Application-toolkit-logback-1.x.md b/docs/en/setup/service-agent/java-agent/Application-toolkit-logback-1.x.md
index c5d4e14ba..133f1cf7a 100644
--- a/docs/en/setup/service-agent/java-agent/Application-toolkit-logback-1.x.md
+++ b/docs/en/setup/service-agent/java-agent/Application-toolkit-logback-1.x.md
@@ -57,7 +57,13 @@
```
-* 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
+
+
+
+
```
* set `LoggingEventCompositeJsonEncoder` of logstash in logback-spring.xml for custom json format
-1.add converter for %tid as child of node
+1.add converter for %tid or %sw_ctx as child of node
```xml
-
-
+
+
+
+
```
2.add json encoder for custom json format
@@ -98,12 +110,13 @@
{
- "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}"
}