This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new 57b345d  add logstash logback plugin (#3365)
57b345d is described below

commit 57b345ddaa53da7b457f59c7e3d56578f91a2015
Author: wuxingye <23226...@qq.com>
AuthorDate: Thu Aug 29 09:58:49 2019 -0500

    add logstash logback plugin (#3365)
    
    * add logstash logback plugin
    
    * add logstash logback plugin, set pom provided, refine logback doc
    
    * refine logstash logback doc
    
    * refine the comment
    
    * remove the app_id that not related to SkyWalking
    
    * remove tag @date for CI error: unknown tag: date
    
    * add description for iLoggingEvent for ci warning: no description for 
@param
---
 .../apm-toolkit-logback-1.x/pom.xml                |  7 ++
 .../log/logback/v1/x/LogbackPatternConverter.java  |  2 +-
 .../logback/v1/x/logstash/TraceIdJsonProvider.java | 47 ++++++++++++++
 .../apm-toolkit-logback-1.x-activation/pom.xml     | 14 +++-
 .../v1/x/logstash/TcpSocketAppenderActivation.java | 74 ++++++++++++++++++++++
 .../x/logstash/TcpSocketAppenderInterceptor.java   | 51 +++++++++++++++
 .../src/main/resources/skywalking-plugin.def       |  1 +
 .../java-agent/Application-toolkit-logback-1.x.md  | 22 +++++++
 8 files changed, 216 insertions(+), 2 deletions(-)

diff --git a/apm-application-toolkit/apm-toolkit-logback-1.x/pom.xml 
b/apm-application-toolkit/apm-toolkit-logback-1.x/pom.xml
index 00689b9..9d56014 100644
--- a/apm-application-toolkit/apm-toolkit-logback-1.x/pom.xml
+++ b/apm-application-toolkit/apm-toolkit-logback-1.x/pom.xml
@@ -29,6 +29,7 @@
 
     <properties>
         <logback-classic.version>1.2.3</logback-classic.version>
+        <logstash.version>6.1</logstash.version>
     </properties>
 
     <dependencies>
@@ -38,5 +39,11 @@
             <version>${logback-classic.version}</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>net.logstash.logback</groupId>
+            <artifactId>logstash-logback-encoder</artifactId>
+            <version>${logstash.version}</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 </project>
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 4fd9e11..6a1cb18 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
@@ -30,7 +30,7 @@ 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 in the 
recent Context, if existed.
      *
-     * @param iLoggingEvent
+     * @param iLoggingEvent the event
      * @return the traceId: N/A, empty String, or the real traceId.
      */
     @Override
diff --git 
a/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/logstash/TraceIdJsonProvider.java
 
b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/logstash/TraceIdJsonProvider.java
new file mode 100644
index 0000000..3469edd
--- /dev/null
+++ 
b/apm-application-toolkit/apm-toolkit-logback-1.x/src/main/java/org/apache/skywalking/apm/toolkit/log/logback/v1/x/logstash/TraceIdJsonProvider.java
@@ -0,0 +1,47 @@
+/*
+ * 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 java.io.IOException;
+import java.util.Map;
+import net.logstash.logback.composite.AbstractFieldJsonProvider;
+import net.logstash.logback.composite.FieldNamesAware;
+import net.logstash.logback.composite.JsonWritingUtils;
+import net.logstash.logback.fieldnames.LogstashFieldNames;
+
+/**
+ * @author wuxingye
+ */
+public class TraceIdJsonProvider extends 
AbstractFieldJsonProvider<ILoggingEvent> implements 
FieldNamesAware<LogstashFieldNames> {
+
+    public static final String TRACING_ID = "TID";
+
+    @Override
+    public void writeTo(JsonGenerator generator, ILoggingEvent event) throws 
IOException {
+        Map<String, String> map = event.getLoggerContextVO().getPropertyMap();
+        JsonWritingUtils.writeStringField(generator, getFieldName(), 
map.get(TRACING_ID));
+    }
+
+    @Override
+    public void setFieldNames(LogstashFieldNames fieldNames) {
+        setFieldName(TRACING_ID);
+    }
+}
diff --git 
a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/pom.xml 
b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/pom.xml
index 1217286..906e275 100644
--- 
a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/pom.xml
+++ 
b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logback-1.x-activation/pom.xml
@@ -17,7 +17,8 @@
   ~
   -->
 
-<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
     <parent>
         <artifactId>apm-toolkit-activation</artifactId>
         <groupId>org.apache.skywalking</groupId>
@@ -27,5 +28,16 @@
 
     <artifactId>apm-toolkit-logback-1.x-activation</artifactId>
 
+    <properties>
+        <logback-classic.version>1.2.3</logback-classic.version>
+    </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>${logback-classic.version}</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
 </project>
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/TcpSocketAppenderActivation.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/TcpSocketAppenderActivation.java
new file mode 100644
index 0000000..543dd7a
--- /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/TcpSocketAppenderActivation.java
@@ -0,0 +1,74 @@
+/*
+ * 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 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;
+
+/**
+ * enhance the method prepareForDeferredProcessing of the logstash logback 
class "net.logstash.logback.appender.LogstashTcpSocketAppender".
+ *
+ * @author wuxingye
+ */
+public class TcpSocketAppenderActivation extends 
ClassInstanceMethodsEnhancePluginDefine {
+
+    public static final String INTERCEPT_CLASS = 
"org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.logstash.TcpSocketAppenderInterceptor";
+    public static final String ENHANCE_CLASS = 
"net.logstash.logback.appender.LogstashTcpSocketAppender";
+    public static final String ENHANCE_METHOD = "prepareForDeferredProcessing";
+
+    @Override
+    protected ClassMatch enhanceClass() {
+        return byName(ENHANCE_CLASS);
+    }
+
+    @Override
+    public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
+        return null;
+    }
+
+    @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;
+                }
+            }
+        };
+    }
+}
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
new file mode 100644
index 0000000..9f7b7cd
--- /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/TcpSocketAppenderInterceptor.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.logback.v1.x.logstash;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import java.lang.reflect.Method;
+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;
+
+/**
+ * @author wuxingye
+ */
+public class TcpSocketAppenderInterceptor implements 
InstanceMethodsAroundInterceptor {
+
+    @Override public void beforeMethod(EnhancedInstance objInst, Method 
method, Object[] allArguments,
+        Class<?>[] argumentsTypes, MethodInterceptResult result) throws 
Throwable {
+        ILoggingEvent event = (ILoggingEvent)allArguments[0];
+        if (event != null && event.getLoggerContextVO() != null && 
event.getLoggerContextVO().getPropertyMap() != null) {
+            event.getLoggerContextVO().getPropertyMap().put("TID", 
ContextManager.getGlobalTraceId());
+        }
+    }
+
+    @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-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 5db3c7d..0e84bd0 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
@@ -18,3 +18,4 @@ 
toolkit-logback=org.apache.skywalking.apm.toolkit.activation.log.logback.v1.x.Lo
 
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
\ No newline at end of file
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 fcbfe90..ea4c830 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
@@ -1,3 +1,4 @@
+# logback plugin
 * Dependency the toolkit, such as using maven or gradle
 ```xml
     <dependency>
@@ -55,3 +56,24 @@
 ```
 
 * 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`.
+
+# logstash logback plugin
+
+* Dependency the toolkit, such as using maven or gradle
+
+```xml
+<dependency>
+    <groupId>org.apache.skywalking</groupId>
+    <artifactId>apm-toolkit-logback-1.x</artifactId>
+    <version>${skywalking.version}</version>
+</dependency>
+```
+
+* set `LogstashEncoder` of logback.xml
+
+```xml
+<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
+    <provider 
class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.TraceIdJsonProvider">
+    </provider>
+</encoder>
+```
\ No newline at end of file

Reply via email to