You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/01/09 07:38:52 UTC

[incubator-servicecomb-java-chassis] 01/03: [JAV-587] add traceId into invocation context

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

wujimin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit d2b7480cf7496af05938c468c9ef2980ef6ff773
Author: yaohaishi <ya...@huawei.com>
AuthorDate: Thu Jan 4 12:00:59 2018 +0800

    [JAV-587] add traceId into invocation context
---
 .../common/rest/filter/tracing/TracingFilter.java  | 76 ++++++++++++++++++++
 ...servicecomb.common.rest.filter.HttpServerFilter | 18 +++++
 .../rest/filter/tracing/TracingFilterTest.java     | 80 ++++++++++++++++++++++
 core/pom.xml                                       |  4 ++
 core/src/main/java/io/servicecomb/core/Const.java  |  4 +-
 .../core/tracing/BraveTraceIdGenerator.java        | 32 +++++++++
 .../servicecomb/core/tracing/TraceIdGenerator.java | 22 ++++++
 .../core/tracing/BraveTraceIdGeneratorTest.java    | 39 +++++++++++
 8 files changed, 274 insertions(+), 1 deletion(-)

diff --git a/common/common-rest/src/main/java/io/servicecomb/common/rest/filter/tracing/TracingFilter.java b/common/common-rest/src/main/java/io/servicecomb/common/rest/filter/tracing/TracingFilter.java
new file mode 100644
index 0000000..8646c2b
--- /dev/null
+++ b/common/common-rest/src/main/java/io/servicecomb/common/rest/filter/tracing/TracingFilter.java
@@ -0,0 +1,76 @@
+/*
+ * 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 io.servicecomb.common.rest.filter.tracing;
+
+import org.springframework.util.StringUtils;
+
+import io.servicecomb.common.rest.filter.HttpServerFilter;
+import io.servicecomb.core.Const;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.core.tracing.BraveTraceIdGenerator;
+import io.servicecomb.core.tracing.TraceIdGenerator;
+import io.servicecomb.foundation.vertx.http.HttpServletRequestEx;
+import io.servicecomb.foundation.vertx.http.HttpServletResponseEx;
+import io.servicecomb.swagger.invocation.Response;
+
+/**
+ * Ensure the invocation contains traceId
+ */
+public class TracingFilter implements HttpServerFilter {
+  private TraceIdGenerator traceIdGenerator = getTraceIdGenerator();
+
+  @Override
+  public int getOrder() {
+    return 0;
+  }
+
+  /**
+   * Ensure the invocation contains traceId
+   * @return {@code null}
+   */
+  @Override
+  public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
+    if (!StringUtils.isEmpty(invocation.getContext(Const.TRACE_ID_NAME))) {
+      // if invocation context contains traceId, nothing needed to do
+      return null;
+    }
+
+    String traceId = requestEx.getHeader(Const.TRACE_ID_NAME);
+    if (!StringUtils.isEmpty(traceId)) {
+      // if request header contains traceId, move traceId into invocation context
+      invocation.addContext(Const.TRACE_ID_NAME, traceId);
+      return null;
+    }
+
+    // if traceId not found, generate a traceId
+    invocation.addContext(Const.TRACE_ID_NAME, traceIdGenerator.generateStringId());
+
+    return null;
+  }
+
+  /**
+   * nothing to do
+   */
+  @Override
+  public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+  }
+
+  protected TraceIdGenerator getTraceIdGenerator() {
+    return BraveTraceIdGenerator.INSTANCE;
+  }
+}
diff --git a/common/common-rest/src/main/resources/META-INF/services/io.servicecomb.common.rest.filter.HttpServerFilter b/common/common-rest/src/main/resources/META-INF/services/io.servicecomb.common.rest.filter.HttpServerFilter
new file mode 100644
index 0000000..dd2626f
--- /dev/null
+++ b/common/common-rest/src/main/resources/META-INF/services/io.servicecomb.common.rest.filter.HttpServerFilter
@@ -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.
+#
+
+io.servicecomb.common.rest.filter.tracing.TracingFilter
diff --git a/common/common-rest/src/test/java/io/servicecomb/common/rest/filter/tracing/TracingFilterTest.java b/common/common-rest/src/test/java/io/servicecomb/common/rest/filter/tracing/TracingFilterTest.java
new file mode 100644
index 0000000..10bc13b
--- /dev/null
+++ b/common/common-rest/src/test/java/io/servicecomb/common/rest/filter/tracing/TracingFilterTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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 io.servicecomb.common.rest.filter.tracing;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import io.servicecomb.core.Const;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.core.tracing.TraceIdGenerator;
+import io.servicecomb.foundation.vertx.http.HttpServletRequestEx;
+
+public class TracingFilterTest {
+  private static final TestTracingFilter FILTER = new TestTracingFilter();
+
+  @Test
+  public void testAfterReceiveRequestOnInvocationContainsTraceId() {
+    Invocation invocation = Mockito.mock(Invocation.class);
+    String traceId = "traceIdTest";
+    HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
+
+    Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(traceId);
+
+    FILTER.afterReceiveRequest(invocation, requestEx);
+
+    Mockito.verify(requestEx, Mockito.times(0)).getHeader(Const.TRACE_ID_NAME);
+  }
+
+  @Test
+  public void testAfterReceiveRequestOnHeaderContainsTraceId() {
+    Invocation invocation = Mockito.mock(Invocation.class);
+    String traceId = "traceIdTest";
+    HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
+
+    Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(null);
+    Mockito.when(requestEx.getHeader(Const.TRACE_ID_NAME)).thenReturn(traceId);
+
+    FILTER.afterReceiveRequest(invocation, requestEx);
+
+    Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME, traceId);
+  }
+
+  @Test
+  public void testAfterReceiveRequestOnGenerateTraceId() {
+    Invocation invocation = Mockito.mock(Invocation.class);
+    HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
+
+    Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(null);
+    Mockito.when(requestEx.getHeader(Const.TRACE_ID_NAME)).thenReturn(null);
+
+    FILTER.afterReceiveRequest(invocation, requestEx);
+
+    Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME, TestTracingFilter.TRACE_ID);
+  }
+
+  static class TestTracingFilter extends TracingFilter {
+
+    static final String TRACE_ID = "" + Long.MAX_VALUE;
+
+    @Override
+    protected TraceIdGenerator getTraceIdGenerator() {
+      return () -> TRACE_ID;
+    }
+  }
+}
\ No newline at end of file
diff --git a/core/pom.xml b/core/pom.xml
index 573a489..ceee759 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -39,6 +39,10 @@
       <artifactId>foundation-metrics</artifactId>
     </dependency>
     <dependency>
+      <groupId>io.zipkin.brave</groupId>
+      <artifactId>brave</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.jmockit</groupId>
       <artifactId>jmockit</artifactId>
       <scope>provided</scope>
diff --git a/core/src/main/java/io/servicecomb/core/Const.java b/core/src/main/java/io/servicecomb/core/Const.java
index 63af71a..b0c2d7b 100644
--- a/core/src/main/java/io/servicecomb/core/Const.java
+++ b/core/src/main/java/io/servicecomb/core/Const.java
@@ -40,6 +40,8 @@ public final class Const {
   public static final String TARGET_MICROSERVICE = "x-cse-target-microservice";
 
   public static final String REMOTE_ADDRESS = "x-cse-remote-address";
-  
+
   public static final String AUTH_TOKEN = "x-cse-auth-rsatoken";
+
+  public static final String TRACE_ID_NAME = "X-B3-TraceId";
 }
diff --git a/core/src/main/java/io/servicecomb/core/tracing/BraveTraceIdGenerator.java b/core/src/main/java/io/servicecomb/core/tracing/BraveTraceIdGenerator.java
new file mode 100644
index 0000000..dbacc78
--- /dev/null
+++ b/core/src/main/java/io/servicecomb/core/tracing/BraveTraceIdGenerator.java
@@ -0,0 +1,32 @@
+/*
+ * 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 io.servicecomb.core.tracing;
+
+import brave.internal.Platform;
+
+public class BraveTraceIdGenerator implements TraceIdGenerator {
+  public static final BraveTraceIdGenerator INSTANCE = new BraveTraceIdGenerator();
+
+  @Override
+  public String generateStringId() {
+    return String.valueOf(Platform.get().nextTraceIdHigh());
+  }
+
+  private BraveTraceIdGenerator() {
+  }
+}
diff --git a/core/src/main/java/io/servicecomb/core/tracing/TraceIdGenerator.java b/core/src/main/java/io/servicecomb/core/tracing/TraceIdGenerator.java
new file mode 100644
index 0000000..43a4490
--- /dev/null
+++ b/core/src/main/java/io/servicecomb/core/tracing/TraceIdGenerator.java
@@ -0,0 +1,22 @@
+/*
+ * 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 io.servicecomb.core.tracing;
+
+public interface TraceIdGenerator {
+  String generateStringId();
+}
diff --git a/core/src/test/java/io/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java b/core/src/test/java/io/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java
new file mode 100644
index 0000000..2815039
--- /dev/null
+++ b/core/src/test/java/io/servicecomb/core/tracing/BraveTraceIdGeneratorTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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 io.servicecomb.core.tracing;
+
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class BraveTraceIdGeneratorTest {
+
+  @Test
+  public void generateStringId() {
+    TraceIdGenerator traceIdGenerator = BraveTraceIdGenerator.INSTANCE;
+    assertNotEquals(traceIdGenerator.generateStringId(), traceIdGenerator.generateStringId());
+
+    String traceId = traceIdGenerator.generateStringId();
+    try {
+      Long.valueOf(traceId);
+    } catch (NumberFormatException e) {
+      fail("wrong traceId format: " + traceId);
+    }
+  }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.