未验证 提交 171620bf 编写于 作者: 静夜思朝颜's avatar 静夜思朝颜 提交者: GitHub

Add Nginx correlation e2e test (#4646)

上级 b3b8aaea
...@@ -108,9 +108,10 @@ public class CorrelationContext { ...@@ -108,9 +108,10 @@ public class CorrelationContext {
break; break;
} }
final String[] parts = perData.split(":"); final String[] parts = perData.split(":");
String perDataKey = parts[0]; if (parts.length != 2) {
String perDataValue = parts.length > 1 ? parts[1] : ""; continue;
data.put(Base64.decode2UTFString(perDataKey), Base64.decode2UTFString(perDataValue)); }
data.put(Base64.decode2UTFString(parts[0]), Base64.decode2UTFString(parts[1]));
} }
} }
......
...@@ -111,7 +111,7 @@ public class CorrelationContextTest { ...@@ -111,7 +111,7 @@ public class CorrelationContextTest {
// empty value // empty value
context = new CorrelationContext(); context = new CorrelationContext();
context.deserialize("dGVzdDE=:"); context.deserialize("dGVzdDE=:");
Assert.assertEquals("", context.get("test1").get()); Assert.assertFalse(context.get("test1").isPresent());
// empty string // empty string
context = new CorrelationContext(); context = new CorrelationContext();
......
/*
* 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();
}
}
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.apache.skywalking.e2e.lua; package org.apache.skywalking.e2e.lua;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -36,8 +37,15 @@ public class LuaController { ...@@ -36,8 +37,15 @@ public class LuaController {
@PostMapping("/nginx/entry/info") @PostMapping("/nginx/entry/info")
private String nginxEntry(String backend) throws MalformedURLException, URISyntaxException { private String nginxEntry(String backend) throws MalformedURLException, URISyntaxException {
final URL url = new URL("http://nginx:8080/nginx/info"); 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); final ResponseEntity<String> response = restTemplate.postForEntity(url.toURI(), null, String.class);
return response.getBody(); return response.getBody();
} }
@PostMapping("/nginx/end/info")
private String nginxEnd() throws MalformedURLException, URISyntaxException {
return TraceContext.getCorrelation("entry").orElse("")
+ "_" + TraceContext.getCorrelation("nginx").orElse("");
}
} }
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
FROM openresty/openresty FROM openresty/openresty
ENV COMMIT_HASH=45cf64640047a4a54619fa1d3451b4d0a65a62d6 ENV COMMIT_HASH=7b23a978bb27787c69a57f734e4fa5865494ef79
WORKDIR /usr/share/skywalking-nginx-lua WORKDIR /usr/share/skywalking-nginx-lua
......
...@@ -47,10 +47,10 @@ http { ...@@ -47,10 +47,10 @@ http {
location /nginx/info { location /nginx/info {
rewrite_by_lua_block { 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 { body_filter_by_lua_block {
require("tracer"):finish() require("tracer"):finish()
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.apache.skywalking.e2e; package org.apache.skywalking.e2e;
import java.net.URL;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.e2e.annotation.ContainerHostAndPort; import org.apache.skywalking.e2e.annotation.ContainerHostAndPort;
...@@ -51,8 +52,10 @@ import org.apache.skywalking.e2e.topo.TopoQuery; ...@@ -51,8 +52,10 @@ import org.apache.skywalking.e2e.topo.TopoQuery;
import org.apache.skywalking.e2e.trace.Trace; import org.apache.skywalking.e2e.trace.Trace;
import org.apache.skywalking.e2e.trace.TracesMatcher; import org.apache.skywalking.e2e.trace.TracesMatcher;
import org.apache.skywalking.e2e.trace.TracesQuery; import org.apache.skywalking.e2e.trace.TracesQuery;
import org.junit.Assert;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.springframework.http.ResponseEntity;
import org.testcontainers.containers.DockerComposeContainer; import org.testcontainers.containers.DockerComposeContainer;
import static org.apache.skywalking.e2e.metrics.MetricsMatcher.verifyMetrics; import static org.apache.skywalking.e2e.metrics.MetricsMatcher.verifyMetrics;
...@@ -81,10 +84,6 @@ public class LuaE2E extends SkyWalkingTestAdapter { ...@@ -81,10 +84,6 @@ public class LuaE2E extends SkyWalkingTestAdapter {
@ContainerHostAndPort(name = "provider-entry", port = 9090) @ContainerHostAndPort(name = "provider-entry", port = 9090)
private HostAndPort entryProvider; private HostAndPort entryProvider;
@SuppressWarnings("unused")
@ContainerHostAndPort(name = "nginx", port = 8080)
private HostAndPort nginxHostPort;
private final String nginxServiceName = "User_Service_Name"; private final String nginxServiceName = "User_Service_Name";
private final String entryServiceName = "e2e-service-entry-provider"; private final String entryServiceName = "e2e-service-entry-provider";
...@@ -92,7 +91,7 @@ public class LuaE2E extends SkyWalkingTestAdapter { ...@@ -92,7 +91,7 @@ public class LuaE2E extends SkyWalkingTestAdapter {
public void setUp() throws Exception { public void setUp() throws Exception {
queryClient(swWebappHostPort); queryClient(swWebappHostPort);
trafficController(entryProvider, "/nginx/entry/info?backend=" + nginxHostPort.host() + ":" + nginxHostPort.port()); trafficController(entryProvider, "/nginx/entry/info");
} }
@AfterAll @AfterAll
...@@ -100,6 +99,13 @@ public class LuaE2E extends SkyWalkingTestAdapter { ...@@ -100,6 +99,13 @@ public class LuaE2E extends SkyWalkingTestAdapter {
trafficController.stop(); trafficController.stop();
} }
@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 @RetryableTest
void services() throws Exception { void services() throws Exception {
final List<Service> services = graphql.services(new ServicesQuery().start(startTime).end(now())); final List<Service> services = graphql.services(new ServicesQuery().start(startTime).end(now()));
...@@ -213,7 +219,7 @@ public class LuaE2E extends SkyWalkingTestAdapter { ...@@ -213,7 +219,7 @@ public class LuaE2E extends SkyWalkingTestAdapter {
private void verifyEndpointsMetrics(Endpoints endpoints) throws Exception { private void verifyEndpointsMetrics(Endpoints endpoints) throws Exception {
for (Endpoint endpoint : endpoints.getEndpoints()) { 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; continue;
} }
for (final String metricName : ALL_ENDPOINT_METRICS) { for (final String metricName : ALL_ENDPOINT_METRICS) {
......
...@@ -15,7 +15,5 @@ ...@@ -15,7 +15,5 @@
endpoints: endpoints:
- key: not null - key: not null
label: /info label: /nginx/end/info
...@@ -34,7 +34,7 @@ traces: ...@@ -34,7 +34,7 @@ traces:
- key: not null - key: not null
endpointNames: endpointNames:
- /info - /nginx/end/info
duration: ge 0 duration: ge 0
start: gt 0 start: gt 0
isError: false isError: false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册