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 5fbc56e0228342859148d942abd6b746bfab937f..4f860b2b3d930ddc2dd31130ea0fb73a18751daf 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 11db7b33df9c74d5e5bfaa6d5fa2b1586a718827..41eba35f71f7c0115d4ee30d5f22128959b49864 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 0000000000000000000000000000000000000000..c846f44e0acf963335dddba3338a5d2e915842c4 --- /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. + *

+ */ +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 getCorrelation(String key) { + return Optional.empty(); + } + + /** + * Put the custom key/value into trace context. + * + * @return previous value if it exists. + */ + public static Optional 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 6fad0c79ee30ce0b211cfdf8c8dd3fb9d1816e74..28cd19ffbadf63101e48592b3edff764d1437321 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 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 af9de7cfe8d907fe3bf357d68218aac150d5a9a0..5b57c1704990d92987e5e81f3a7ccd59bd4c2898 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 cf2c3f2f5977850830a34362169cc5f1a82433c5..09669ca6339e044b8816c44621a8ee82025c3159 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 46fb45915b26808b8086711facb9bec5117edaf4..48ebc1d7121e1501f2100176f2987e74c9a5e26e 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 @@ -100,6 +99,13 @@ public class LuaE2E extends SkyWalkingTestAdapter { trafficController.stop(); } + @RetryableTest + void correlation() throws Exception { + final URL url = new URL("http", entryProvider.host(), entryProvider.port(), "/nginx/entry/info"); + final ResponseEntity response = restTemplate.postForEntity(url.toURI(), trafficData, String.class); + Assert.assertEquals(response.getBody(), "entry_value_nginx_value"); + } + @RetryableTest void services() throws Exception { final List 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 ec9f776313c7711d29128a38860298af8f9afd6e..610960e798eb82d3c966b4e81d31cf72f7d7c1c1 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 ef118f83f5c1083935ed36f8dc350dd8c1cabbb9..8a6a87974f2aef1021565c9db267ad20100e3ec9 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