You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/04/14 01:33:06 UTC

[skywalking] branch master updated: Add Nginx correlation e2e test (#4646)

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

kezhenxu94 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 171620b  Add Nginx correlation e2e test (#4646)
171620b is described below

commit 171620bfa8e74bfc84e7d322c260f319943b7191
Author: mrproliu <74...@qq.com>
AuthorDate: Tue Apr 14 09:32:54 2020 +0800

    Add Nginx correlation e2e test (#4646)
---
 .../apm/agent/core/context/CorrelationContext.java |  7 +--
 .../agent/core/context/CorrelationContextTest.java |  2 +-
 .../skywalking/apm/toolkit/trace/TraceContext.java | 57 ++++++++++++++++++++++
 .../apache/skywalking/e2e/lua/LuaController.java   |  8 +++
 test/e2e/e2e-test/docker/lua/Dockerfile.nginx      |  2 +-
 test/e2e/e2e-test/docker/lua/nginx.conf            |  4 +-
 .../java/org/apache/skywalking/e2e/LuaE2E.java     | 18 ++++---
 .../test/resources/expected/lua/endpoints-end.yml  |  4 +-
 .../src/test/resources/expected/lua/traces.yml     |  2 +-
 9 files changed, 87 insertions(+), 17 deletions(-)

diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/CorrelationContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/CorrelationContext.java
index 5fbc56e..4f860b2 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/CorrelationContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/CorrelationContext.java
@@ -108,9 +108,10 @@ public class CorrelationContext {
                 break;
             }
             final String[] parts = perData.split(":");
-            String perDataKey = parts[0];
-            String perDataValue = parts.length > 1 ? parts[1] : "";
-            data.put(Base64.decode2UTFString(perDataKey), Base64.decode2UTFString(perDataValue));
+            if (parts.length != 2) {
+                continue;
+            }
+            data.put(Base64.decode2UTFString(parts[0]), Base64.decode2UTFString(parts[1]));
         }
     }
 
diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/CorrelationContextTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/CorrelationContextTest.java
index 11db7b3..41eba35 100644
--- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/CorrelationContextTest.java
+++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/context/CorrelationContextTest.java
@@ -111,7 +111,7 @@ public class CorrelationContextTest {
         // empty value
         context = new CorrelationContext();
         context.deserialize("dGVzdDE=:");
-        Assert.assertEquals("", context.get("test1").get());
+        Assert.assertFalse(context.get("test1").isPresent());
 
         // empty string
         context = new CorrelationContext();
diff --git a/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.java b/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.java
new file mode 100644
index 0000000..c846f44
--- /dev/null
+++ b/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.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.trace;
+
+import java.util.Optional;
+
+/**
+ * Try to access the sky-walking tracer context. The context is not existed, always. only the middleware, component, or
+ * rpc-framework are supported in the current invoke stack, in the same thread, the context will be available.
+ * <p>
+ */
+public class TraceContext {
+
+    /**
+     * Try to get the traceId of current trace context.
+     *
+     * @return traceId, if it exists, or empty {@link String}.
+     */
+    public static String traceId() {
+        return "";
+    }
+
+    /**
+     * Try to get the custom value from trace context.
+     *
+     * @return custom data value.
+     */
+    public static Optional<String> getCorrelation(String key) {
+        return Optional.empty();
+    }
+
+    /**
+     * Put the custom key/value into trace context.
+     *
+     * @return previous value if it exists.
+     */
+    public static Optional<String> putCorrelation(String key, String value) {
+        return Optional.empty();
+    }
+
+}
diff --git a/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/e2e/lua/LuaController.java b/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/e2e/lua/LuaController.java
index 6fad0c7..28cd19f 100644
--- a/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/e2e/lua/LuaController.java
+++ b/test/e2e/e2e-service-provider/src/main/java/org/apache/skywalking/e2e/lua/LuaController.java
@@ -19,6 +19,7 @@
 package org.apache.skywalking.e2e.lua;
 
 import lombok.RequiredArgsConstructor;
+import org.apache.skywalking.apm.toolkit.trace.TraceContext;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -36,8 +37,15 @@ public class LuaController {
     @PostMapping("/nginx/entry/info")
     private String nginxEntry(String backend) throws MalformedURLException, URISyntaxException {
         final URL url = new URL("http://nginx:8080/nginx/info");
+        TraceContext.putCorrelation("entry", "entry_value");
         final ResponseEntity<String> response = restTemplate.postForEntity(url.toURI(), null, String.class);
         return response.getBody();
     }
 
+    @PostMapping("/nginx/end/info")
+    private String nginxEnd() throws MalformedURLException, URISyntaxException {
+        return TraceContext.getCorrelation("entry").orElse("")
+            + "_" + TraceContext.getCorrelation("nginx").orElse("");
+    }
+
 }
diff --git a/test/e2e/e2e-test/docker/lua/Dockerfile.nginx b/test/e2e/e2e-test/docker/lua/Dockerfile.nginx
index af9de7c..5b57c17 100644
--- a/test/e2e/e2e-test/docker/lua/Dockerfile.nginx
+++ b/test/e2e/e2e-test/docker/lua/Dockerfile.nginx
@@ -15,7 +15,7 @@
 
 FROM openresty/openresty
 
-ENV COMMIT_HASH=45cf64640047a4a54619fa1d3451b4d0a65a62d6
+ENV COMMIT_HASH=7b23a978bb27787c69a57f734e4fa5865494ef79
 
 WORKDIR /usr/share/skywalking-nginx-lua
 
diff --git a/test/e2e/e2e-test/docker/lua/nginx.conf b/test/e2e/e2e-test/docker/lua/nginx.conf
index cf2c3f2..09669ca 100644
--- a/test/e2e/e2e-test/docker/lua/nginx.conf
+++ b/test/e2e/e2e-test/docker/lua/nginx.conf
@@ -47,10 +47,10 @@ http {
         location /nginx/info {
 
             rewrite_by_lua_block {
-                require("tracer"):start("User_Service_Name")
+                require("tracer"):start("User_Service_Name", {nginx = "nginx_value"})
             }
 
-            proxy_pass http://provider-end:9090/info;
+            proxy_pass http://provider-end:9090/nginx/end/info;
 
             body_filter_by_lua_block {
                 require("tracer"):finish()
diff --git a/test/e2e/e2e-test/src/test/java/org/apache/skywalking/e2e/LuaE2E.java b/test/e2e/e2e-test/src/test/java/org/apache/skywalking/e2e/LuaE2E.java
index 46fb459..48ebc1d 100644
--- a/test/e2e/e2e-test/src/test/java/org/apache/skywalking/e2e/LuaE2E.java
+++ b/test/e2e/e2e-test/src/test/java/org/apache/skywalking/e2e/LuaE2E.java
@@ -18,6 +18,7 @@
 
 package org.apache.skywalking.e2e;
 
+import java.net.URL;
 import java.util.List;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.skywalking.e2e.annotation.ContainerHostAndPort;
@@ -51,8 +52,10 @@ import org.apache.skywalking.e2e.topo.TopoQuery;
 import org.apache.skywalking.e2e.trace.Trace;
 import org.apache.skywalking.e2e.trace.TracesMatcher;
 import org.apache.skywalking.e2e.trace.TracesQuery;
+import org.junit.Assert;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
+import org.springframework.http.ResponseEntity;
 import org.testcontainers.containers.DockerComposeContainer;
 
 import static org.apache.skywalking.e2e.metrics.MetricsMatcher.verifyMetrics;
@@ -81,10 +84,6 @@ public class LuaE2E extends SkyWalkingTestAdapter {
     @ContainerHostAndPort(name = "provider-entry", port = 9090)
     private HostAndPort entryProvider;
 
-    @SuppressWarnings("unused")
-    @ContainerHostAndPort(name = "nginx", port = 8080)
-    private HostAndPort nginxHostPort;
-
     private final String nginxServiceName = "User_Service_Name";
     private final String entryServiceName = "e2e-service-entry-provider";
 
@@ -92,7 +91,7 @@ public class LuaE2E extends SkyWalkingTestAdapter {
     public void setUp() throws Exception {
         queryClient(swWebappHostPort);
 
-        trafficController(entryProvider, "/nginx/entry/info?backend=" + nginxHostPort.host() + ":" + nginxHostPort.port());
+        trafficController(entryProvider, "/nginx/entry/info");
     }
 
     @AfterAll
@@ -101,6 +100,13 @@ public class LuaE2E extends SkyWalkingTestAdapter {
     }
 
     @RetryableTest
+    void correlation() throws Exception {
+        final URL url = new URL("http", entryProvider.host(), entryProvider.port(), "/nginx/entry/info");
+        final ResponseEntity<String> response = restTemplate.postForEntity(url.toURI(), trafficData, String.class);
+        Assert.assertEquals(response.getBody(), "entry_value_nginx_value");
+    }
+
+    @RetryableTest
     void services() throws Exception {
         final List<Service> services = graphql.services(new ServicesQuery().start(startTime).end(now()));
 
@@ -213,7 +219,7 @@ public class LuaE2E extends SkyWalkingTestAdapter {
 
     private void verifyEndpointsMetrics(Endpoints endpoints) throws Exception {
         for (Endpoint endpoint : endpoints.getEndpoints()) {
-            if (!endpoint.getLabel().equals("/info") && !endpoint.getLabel().equals("/nginx/info")) {
+            if (!endpoint.getLabel().equals("/nginx/end/info") && !endpoint.getLabel().equals("/nginx/info")) {
                 continue;
             }
             for (final String metricName : ALL_ENDPOINT_METRICS) {
diff --git a/test/e2e/e2e-test/src/test/resources/expected/lua/endpoints-end.yml b/test/e2e/e2e-test/src/test/resources/expected/lua/endpoints-end.yml
index ec9f776..610960e 100644
--- a/test/e2e/e2e-test/src/test/resources/expected/lua/endpoints-end.yml
+++ b/test/e2e/e2e-test/src/test/resources/expected/lua/endpoints-end.yml
@@ -15,7 +15,5 @@
 
 endpoints:
   - key: not null
-    label: /info
-
-
+    label: /nginx/end/info
 
diff --git a/test/e2e/e2e-test/src/test/resources/expected/lua/traces.yml b/test/e2e/e2e-test/src/test/resources/expected/lua/traces.yml
index ef118f8..8a6a879 100644
--- a/test/e2e/e2e-test/src/test/resources/expected/lua/traces.yml
+++ b/test/e2e/e2e-test/src/test/resources/expected/lua/traces.yml
@@ -34,7 +34,7 @@ traces:
 
   - key: not null
     endpointNames:
-      - /info
+      - /nginx/end/info
     duration: ge 0
     start: gt 0
     isError: false