[ 
https://issues.apache.org/jira/browse/SCB-906?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16616982#comment-16616982
 ] 

ASF GitHub Bot commented on SCB-906:
------------------------------------

liubao68 closed pull request #903: [SCB-906] add sample for invocation apm by 
java agent
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/903
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/samples/apm-agent/pom.xml b/samples/apm-agent/pom.xml
new file mode 100644
index 000000000..2fd1d75e8
--- /dev/null
+++ b/samples/apm-agent/pom.xml
@@ -0,0 +1,91 @@
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.servicecomb.samples</groupId>
+  <artifactId>apm-agent</artifactId>
+  <version>1.1.0-SNAPSHOT</version>
+  <name>Java Chassis::Samples::apm-agent</name>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <java.version>1.8</java.version>
+
+    <argLine>-Dfile.encoding=UTF-8</argLine>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencyManagement>
+    <dependencies>
+      <dependency>
+        <groupId>org.apache.servicecomb</groupId>
+        <artifactId>java-chassis-dependencies</artifactId>
+        <version>1.1.0-SNAPSHOT</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+    </dependencies>
+  </dependencyManagement>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>provider-rest-common</artifactId>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>3.1.0</version>
+        <executions>
+          <execution>
+            <id>default-jar</id>
+            <phase>package</phase>
+          </execution>
+        </executions>
+        <configuration>
+          <archive>
+            <manifestEntries>
+              
<Premain-Class>org.apache.servicecomb.samples.apm.AgentMain</Premain-Class>
+            </manifestEntries>
+          </archive>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.1</version>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+          <compilerArgs>
+            <arg>-Werror</arg>
+            <arg>-Xlint:all</arg>
+          </compilerArgs>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
\ No newline at end of file
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/AgentMain.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/AgentMain.java
new file mode 100644
index 000000000..213c79004
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/AgentMain.java
@@ -0,0 +1,28 @@
+/*
+ * 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.servicecomb.samples.apm;
+
+import java.lang.instrument.Instrumentation;
+
+public class AgentMain {
+  public static void premain(String args, Instrumentation inst) {
+    // to support web container, we can not just only inject spiJar to system 
classloader
+    // in this sample, javaAgent jar equals ServiceComb plugin jar
+    inst.addTransformer(
+        new 
SCBClassFileTransformer(AgentMain.class.getProtectionDomain().getCodeSource().getLocation()));
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/SCBClassFileTransformer.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/SCBClassFileTransformer.java
new file mode 100644
index 000000000..ae6d8cfb6
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/SCBClassFileTransformer.java
@@ -0,0 +1,62 @@
+/*
+ * 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.servicecomb.samples.apm;
+
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.security.ProtectionDomain;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class SCBClassFileTransformer implements ClassFileTransformer {
+  private static final Logger LOGGER = 
Logger.getLogger(SCBClassFileTransformer.class.getName());
+
+  private volatile boolean loaded;
+
+  private URL scbSpiJar;
+
+  public SCBClassFileTransformer(URL scbSpiJar) {
+    this.scbSpiJar = scbSpiJar;
+  }
+
+  @Override
+  public byte[] transform(ClassLoader loader, String className, Class<?> 
classBeingRedefined,
+      ProtectionDomain protectionDomain, byte[] classfileBuffer) {
+    if (!loaded && className.startsWith("org/apache/servicecomb")) {
+      injectSPI(loader);
+    }
+    return classfileBuffer;
+  }
+
+  private synchronized void injectSPI(ClassLoader loader) {
+    if (loaded) {
+      return;
+    }
+
+    try {
+      Method addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", 
URL.class);
+      addURLMethod.setAccessible(true);
+      addURLMethod.invoke(loader, scbSpiJar);
+      loaded = true;
+    } catch (Throwable e) {
+      LOGGER.log(Level.SEVERE,
+          String.format("Failed to inject %s to classLoader %s.", scbSpiJar, 
loader.getClass().getName()), e);
+    }
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmBootListener.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmBootListener.java
new file mode 100644
index 000000000..7ad4284db
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmBootListener.java
@@ -0,0 +1,90 @@
+/*
+ * 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.servicecomb.samples.apm.impl;
+
+import org.apache.servicecomb.core.BootListener;
+import org.apache.servicecomb.core.event.InvocationBusinessMethodFinishEvent;
+import org.apache.servicecomb.core.event.InvocationBusinessMethodStartEvent;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.event.InvocationStartEvent;
+import org.apache.servicecomb.samples.apm.impl.output.AbstractOutputGenerator;
+import org.apache.servicecomb.samples.apm.impl.output.ConsumerOutputGenerator;
+import org.apache.servicecomb.samples.apm.impl.output.EdgeOutputGenerator;
+import org.apache.servicecomb.samples.apm.impl.output.HeaderOutputGenerator;
+import org.apache.servicecomb.samples.apm.impl.output.ProducerOutputGenerator;
+
+import com.google.common.eventbus.AllowConcurrentEvents;
+import com.google.common.eventbus.Subscribe;
+
+public class ApmBootListener implements BootListener {
+  private AbstractOutputGenerator headerOutputGenerator = new 
HeaderOutputGenerator();
+
+  private AbstractOutputGenerator consumerOutputGenerator = new 
ConsumerOutputGenerator();
+
+  private AbstractOutputGenerator producerOutputGenerator = new 
ProducerOutputGenerator();
+
+  private AbstractOutputGenerator edgeOutputGenerator = new 
EdgeOutputGenerator();
+
+  @AllowConcurrentEvents
+  @Subscribe
+  public void onInvocationStart(InvocationStartEvent event) {
+    ApmContext apmContext = new ApmContext();
+    apmContext.setTraceId(event.getInvocation().getTraceId());
+    event.getInvocation().addLocalContext("apm", apmContext);
+  }
+
+  @AllowConcurrentEvents
+  @Subscribe
+  public void onInvocationBusinessStart(InvocationBusinessMethodStartEvent 
event) {
+    ApmContext apmContext = event.getInvocation().getLocalContext("apm");
+    ApmContextUtils.setApmContext(apmContext);
+  }
+
+  @AllowConcurrentEvents
+  @Subscribe
+  public void onInvocationBusinessFinish(InvocationBusinessMethodFinishEvent 
event) {
+    ApmContextUtils.removeApmContext();
+  }
+
+  /**
+   * in real APM implementation: create span and so on for stages
+   * @param event
+   */
+  @AllowConcurrentEvents
+  @Subscribe
+  public void onInvocationFinish(InvocationFinishEvent event) {
+    StringBuilder sb = new StringBuilder();
+    headerOutputGenerator.generate(sb, event);
+    if (event.getInvocation().isConsumer()) {
+      if (event.getInvocation().isEdge()) {
+        edgeOutputGenerator.generate(sb, event);
+      } else {
+        consumerOutputGenerator.generate(sb, event);
+      }
+    } else {
+      producerOutputGenerator.generate(sb, event);
+    }
+    System.out.println(sb.toString());
+  }
+
+  @Override
+  public void onBootEvent(BootEvent event) {
+    if (EventType.BEFORE_HANDLER.equals(event.getEventType())) {
+      event.getScbEngine().getEventBus().register(this);
+    }
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmContext.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmContext.java
new file mode 100644
index 000000000..9ab46ae31
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmContext.java
@@ -0,0 +1,29 @@
+/*
+ * 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.servicecomb.samples.apm.impl;
+
+public class ApmContext {
+  private String traceId;
+
+  public String getTraceId() {
+    return traceId;
+  }
+
+  public void setTraceId(String traceId) {
+    this.traceId = traceId;
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmContextUtils.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmContextUtils.java
new file mode 100644
index 000000000..3fd8cb077
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/ApmContextUtils.java
@@ -0,0 +1,41 @@
+/*
+ * 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.servicecomb.samples.apm.impl;
+
+public class ApmContextUtils {
+  private static ThreadLocal<ApmContext> threadApmContext = new 
ThreadLocal<>();
+
+  public static ApmContext getApmContext() {
+    return threadApmContext.get();
+  }
+
+  public static ApmContext getAndRemoveApmContext() {
+    ApmContext context = threadApmContext.get();
+    if (context != null) {
+      threadApmContext.remove();
+    }
+    return context;
+  }
+
+  public static void setApmContext(ApmContext apmContext) {
+    threadApmContext.set(apmContext);
+  }
+
+  public static void removeApmContext() {
+    threadApmContext.remove();
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/AbstractOutputGenerator.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/AbstractOutputGenerator.java
new file mode 100644
index 000000000..9c731a20e
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/AbstractOutputGenerator.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.servicecomb.samples.apm.impl.output;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+
+import com.google.common.base.Strings;
+
+public abstract class AbstractOutputGenerator {
+  static final String PAD2_KEY11_FMT = generateFmt(2, 11);
+
+  static final String PAD2_TIME_FMT = generateTimeFmt(2);
+
+  static final String PAD4_TIME_FMT = generateTimeFmt(4);
+
+  static final String PAD6_TIME_FMT = generateTimeFmt(6);
+
+  protected static String generateFmt(int leftPad, int keyLen) {
+    return Strings.repeat(" ", leftPad) + "%-" + keyLen + "s: %s\n";
+  }
+
+  protected static String generateTimeFmt(int leftPad) {
+    return Strings.repeat(" ", leftPad) + "%-" + (27 - leftPad) + "s: 
%.3fms\n";
+  }
+
+  protected void appendLine(StringBuilder sb, String fmt, String headerKey, 
Object value) {
+    sb.append(String.format(fmt, headerKey, value));
+  }
+
+  protected void appendTimeLine(StringBuilder sb, String fmt, String 
headerKey, double nano) {
+    sb.append(String.format(fmt, headerKey, nano / 
TimeUnit.MILLISECONDS.toNanos(1)));
+  }
+
+  abstract public void generate(StringBuilder sb, InvocationFinishEvent event);
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/ConsumerOutputGenerator.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/ConsumerOutputGenerator.java
new file mode 100644
index 000000000..12e35ffee
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/ConsumerOutputGenerator.java
@@ -0,0 +1,40 @@
+/*
+ * 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.servicecomb.samples.apm.impl.output;
+
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.invocation.InvocationStageTrace;
+
+public class ConsumerOutputGenerator extends AbstractOutputGenerator {
+  @Override
+  public void generate(StringBuilder sb, InvocationFinishEvent event) {
+    InvocationStageTrace stageTrace = 
event.getInvocation().getInvocationStageTrace();
+
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.HANDLERS_REQUEST, 
stageTrace.calcHandlersRequestTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.CLIENT_FILTERS_REQUEST,
+        stageTrace.calcClientFiltersRequestTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.CONSUMER_SEND_REQUEST, stageTrace.calcSendRequestTime());
+    appendTimeLine(sb, PAD6_TIME_FMT, 
InvocationStageTrace.CONSUMER_GET_CONNECTION, 
stageTrace.calcGetConnectionTime());
+    appendTimeLine(sb, PAD6_TIME_FMT, 
InvocationStageTrace.CONSUMER_WRITE_TO_BUF, stageTrace.calcWriteToBufferTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.CONSUMER_WAIT_RESPONSE,
+        stageTrace.calcReceiveResponseTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.CONSUMER_WAKE_CONSUMER, stageTrace.calcWakeConsumer());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.CLIENT_FILTERS_RESPONSE,
+        stageTrace.calcClientFiltersResponseTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.HANDLERS_RESPONSE, 
stageTrace.calcHandlersResponseTime());
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/EdgeOutputGenerator.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/EdgeOutputGenerator.java
new file mode 100644
index 000000000..5beb10dfd
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/EdgeOutputGenerator.java
@@ -0,0 +1,37 @@
+/*
+ * 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.servicecomb.samples.apm.impl.output;
+
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.invocation.InvocationStageTrace;
+
+public class EdgeOutputGenerator extends ConsumerOutputGenerator {
+  @Override
+  public void generate(StringBuilder sb, InvocationFinishEvent event) {
+    InvocationStageTrace stageTrace = 
event.getInvocation().getInvocationStageTrace();
+
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.THREAD_POOL_QUEUE, 
stageTrace.calcThreadPoolQueueTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.SERVER_FILTERS_REQUEST,
+        stageTrace.calcServerFiltersRequestTime());
+
+    super.generate(sb, event);
+
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.SERVER_FILTERS_RESPONSE,
+        stageTrace.calcServerFiltersResponseTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.PRODUCER_SEND_RESPONSE, stageTrace.calcSendResponseTime());
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/HeaderOutputGenerator.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/HeaderOutputGenerator.java
new file mode 100644
index 000000000..4ee3570a3
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/HeaderOutputGenerator.java
@@ -0,0 +1,41 @@
+/*
+ * 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.servicecomb.samples.apm.impl.output;
+
+import org.apache.servicecomb.common.rest.RestConst;
+import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.invocation.InvocationStageTrace;
+
+public class HeaderOutputGenerator extends AbstractOutputGenerator {
+  @Override
+  public void generate(StringBuilder sb, InvocationFinishEvent event) {
+    Invocation invocation = event.getInvocation();
+    RestOperationMeta restOperationMeta = 
invocation.getOperationMeta().getExtData(RestConst.SWAGGER_REST_OPERATION);
+    InvocationStageTrace stageTrace = 
event.getInvocation().getInvocationStageTrace();
+
+    sb.append(invocation.getInvocationQualifiedName()).append(":\n");
+    appendLine(sb, PAD2_KEY11_FMT, "http method", 
restOperationMeta.getHttpMethod());
+    appendLine(sb, PAD2_KEY11_FMT, "url", restOperationMeta.getAbsolutePath());
+    appendLine(sb, PAD2_KEY11_FMT, "status code", 
event.getResponse().getStatusCode());
+    appendLine(sb, PAD2_KEY11_FMT, "traceId", invocation.getTraceId());
+
+    appendTimeLine(sb, PAD2_TIME_FMT, "total", stageTrace.calcTotalTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.PREPARE, 
stageTrace.calcInvocationPrepareTime());
+  }
+}
diff --git 
a/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/ProducerOutputGenerator.java
 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/ProducerOutputGenerator.java
new file mode 100644
index 000000000..1b6250f00
--- /dev/null
+++ 
b/samples/apm-agent/src/main/java/org/apache/servicecomb/samples/apm/impl/output/ProducerOutputGenerator.java
@@ -0,0 +1,40 @@
+/*
+ * 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.servicecomb.samples.apm.impl.output;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.invocation.InvocationStageTrace;
+
+public class ProducerOutputGenerator extends AbstractOutputGenerator {
+  @Override
+  public void generate(StringBuilder sb, InvocationFinishEvent event) {
+    Invocation invocation = event.getInvocation();
+    InvocationStageTrace stageTrace = invocation.getInvocationStageTrace();
+
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.THREAD_POOL_QUEUE, 
stageTrace.calcThreadPoolQueueTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.SERVER_FILTERS_REQUEST,
+        stageTrace.calcServerFiltersRequestTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.HANDLERS_REQUEST, 
stageTrace.calcHandlersRequestTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
invocation.getOperationMeta().getSchemaQualifiedName(),
+        stageTrace.calcBusinessTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, InvocationStageTrace.HANDLERS_RESPONSE, 
stageTrace.calcHandlersResponseTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.SERVER_FILTERS_RESPONSE,
+        stageTrace.calcServerFiltersResponseTime());
+    appendTimeLine(sb, PAD4_TIME_FMT, 
InvocationStageTrace.PRODUCER_SEND_RESPONSE, stageTrace.calcSendResponseTime());
+  }
+}
diff --git 
a/samples/apm-agent/src/main/resources/META-INF/services/org.apache.servicecomb.core.BootListener
 
b/samples/apm-agent/src/main/resources/META-INF/services/org.apache.servicecomb.core.BootListener
new file mode 100644
index 000000000..e0efc733b
--- /dev/null
+++ 
b/samples/apm-agent/src/main/resources/META-INF/services/org.apache.servicecomb.core.BootListener
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.samples.apm.impl.ApmBootListener
\ No newline at end of file
diff --git a/samples/pom.xml b/samples/pom.xml
index 7b4ceed78..4c6055915 100644
--- a/samples/pom.xml
+++ b/samples/pom.xml
@@ -40,6 +40,7 @@
     <module>use-log4j2-sample</module>
     <module>local-service-registry</module>
     <module>trust-sample</module>
+    <module>apm-agent</module>
   </modules>
 
   <dependencyManagement>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> add sample for invocation apm by java agent
> -------------------------------------------
>
>                 Key: SCB-906
>                 URL: https://issues.apache.org/jira/browse/SCB-906
>             Project: Apache ServiceComb
>          Issue Type: Task
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>             Fix For: java-chassis-1.1.0
>
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to