From f2cb5b7d45e20c1849c1b5f7039e5fa9f2b2e1e9 Mon Sep 17 00:00:00 2001 From: enoch Date: Mon, 25 Sep 2023 10:57:02 +0800 Subject: [PATCH] feat: add forwarded plugin for independent from Trace --- ...onGetResponseCodeForwardedInterceptor.java | 50 +++++++++ ...tResponseCodeForwardedInterceptorTest.java | 56 ++++++++++ ...nectionGetResponseCodeInterceptorTest.java | 76 +------------ .../interceptor/TestConst.java | 23 ---- .../interceptor/TestUtils.java | 105 ++++++++++++++++++ 5 files changed, 215 insertions(+), 95 deletions(-) create mode 100644 plugins/httpurlconnection/src/main/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptor.java create mode 100644 plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptorTest.java delete mode 100644 plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestConst.java create mode 100644 plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestUtils.java diff --git a/plugins/httpurlconnection/src/main/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptor.java b/plugins/httpurlconnection/src/main/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptor.java new file mode 100644 index 00000000..24ea16a3 --- /dev/null +++ b/plugins/httpurlconnection/src/main/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptor.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023, MegaEase + * All rights reserved. + * + * Licensed 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 com.megaease.easeagent.plugin.httpurlconnection.interceptor; + +import com.megaease.easeagent.plugin.annotation.AdviceTo; +import com.megaease.easeagent.plugin.api.Context; +import com.megaease.easeagent.plugin.api.config.ConfigConst; +import com.megaease.easeagent.plugin.enums.Order; +import com.megaease.easeagent.plugin.httpurlconnection.ForwardedPlugin; +import com.megaease.easeagent.plugin.httpurlconnection.HttpURLConnectionPlugin; +import com.megaease.easeagent.plugin.httpurlconnection.advice.HttpURLConnectionGetResponseCodeAdvice; +import com.megaease.easeagent.plugin.interceptor.Interceptor; +import com.megaease.easeagent.plugin.interceptor.MethodInfo; + +import java.net.HttpURLConnection; + +@AdviceTo(value = HttpURLConnectionGetResponseCodeAdvice.class, qualifier = "default", plugin = ForwardedPlugin.class) +public class HttpURLConnectionGetResponseCodeForwardedInterceptor implements Interceptor { + + @Override + public void before(MethodInfo methodInfo, Context context) { + HttpURLConnection httpURLConnection = (HttpURLConnection) methodInfo.getInvoker(); + context.injectForwardedHeaders(httpURLConnection::setRequestProperty); + } + + @Override + public String getType() { + return ConfigConst.PluginID.FORWARDED; + } + + @Override + public int order() { + return Order.FORWARDED.getOrder(); + } +} diff --git a/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptorTest.java b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptorTest.java new file mode 100644 index 00000000..fb39ec55 --- /dev/null +++ b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeForwardedInterceptorTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023, MegaEase + * All rights reserved. + * + * Licensed 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 com.megaease.easeagent.plugin.httpurlconnection.interceptor; + +import com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner; +import com.megaease.easeagent.plugin.api.Context; +import com.megaease.easeagent.plugin.api.config.ConfigConst; +import com.megaease.easeagent.plugin.bridge.EaseAgent; +import com.megaease.easeagent.plugin.interceptor.MethodInfo; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.net.HttpURLConnection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(EaseAgentJunit4ClassRunner.class) +public class HttpURLConnectionGetResponseCodeForwardedInterceptorTest { + + @Test + public void before() { + Context context = EaseAgent.getContext(); + HttpURLConnection conn = TestUtils.mockHttpURLConnection(); + MethodInfo methodInfo = TestUtils.mockMethodInfo(conn); + HttpURLConnectionGetResponseCodeForwardedInterceptor httpClientDoExecuteForwardedInterceptor = new HttpURLConnectionGetResponseCodeForwardedInterceptor(); + + httpClientDoExecuteForwardedInterceptor.before(methodInfo, context); + assertNull(conn.getRequestProperty(TestUtils.FORWARDED_NAME)); + context.put(TestUtils.FORWARDED_NAME, TestUtils.FORWARDED_VALUE); + httpClientDoExecuteForwardedInterceptor.before(methodInfo, context); + assertEquals(TestUtils.FORWARDED_VALUE, conn.getRequestProperty(TestUtils.FORWARDED_NAME)); + context.remove(TestUtils.FORWARDED_NAME); + } + + @Test + public void getType() { + HttpURLConnectionGetResponseCodeForwardedInterceptor httpClientDoExecuteForwardedInterceptor = new HttpURLConnectionGetResponseCodeForwardedInterceptor(); + assertEquals(ConfigConst.PluginID.FORWARDED, httpClientDoExecuteForwardedInterceptor.getType()); + } +} diff --git a/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeInterceptorTest.java b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeInterceptorTest.java index 84119d2f..54d6d0aa 100644 --- a/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeInterceptorTest.java +++ b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/HttpURLConnectionGetResponseCodeInterceptorTest.java @@ -17,9 +17,6 @@ package com.megaease.easeagent.plugin.httpurlconnection.interceptor; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.net.HttpHeaders; import com.megaease.easeagent.mock.plugin.api.MockEaseAgent; import com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner; import com.megaease.easeagent.plugin.api.Context; @@ -34,13 +31,6 @@ import org.junit.Test; import org.junit.runner.RunWith; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import static org.junit.Assert.*; @RunWith(EaseAgentJunit4ClassRunner.class) @@ -50,7 +40,7 @@ public class HttpURLConnectionGetResponseCodeInterceptorTest { @Test public void before() { Context context = EaseAgent.getContext(); - MethodInfo methodInfo = mockMethodInfo(); + MethodInfo methodInfo = TestUtils.mockMethodInfo(); HttpURLConnectionGetResponseCodeInterceptor httpURLConnectionWriteRequestsInterceptor = new HttpURLConnectionGetResponseCodeInterceptor(); MockEaseAgent.cleanLastSpan(); @@ -59,7 +49,7 @@ public void before() { ReportSpan mockSpan = MockEaseAgent.getLastSpan(); assertNotNull(mockSpan); assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind()); - assertEquals(TestConst.RESPONSE_TAG_VALUE, mockSpan.tag(TestConst.RESPONSE_TAG_NAME)); + assertEquals(TestUtils.RESPONSE_TAG_VALUE, mockSpan.tag(TestUtils.RESPONSE_TAG_NAME)); assertNull(mockSpan.parentId()); Span span = context.nextSpan(); @@ -84,7 +74,7 @@ public void after() { @Test public void getRequest() { Context context = EaseAgent.getContext(); - MethodInfo methodInfo = mockMethodInfo(); + MethodInfo methodInfo = TestUtils.mockMethodInfo(); HttpURLConnectionGetResponseCodeInterceptor httpClientDoExecuteInterceptor = new HttpURLConnectionGetResponseCodeInterceptor(); HttpRequest request = httpClientDoExecuteInterceptor.getRequest(methodInfo, context); @@ -96,69 +86,11 @@ public void getRequest() { @Test public void getResponse() { Context context = EaseAgent.getContext(); - MethodInfo methodInfo = mockMethodInfo(); + MethodInfo methodInfo = TestUtils.mockMethodInfo(); HttpURLConnectionGetResponseCodeInterceptor httpClientDoExecuteInterceptor = new HttpURLConnectionGetResponseCodeInterceptor(); HttpResponse httpResponse = httpClientDoExecuteInterceptor.getResponse(methodInfo, context); assertEquals(200, httpResponse.statusCode()); } - - @SneakyThrows - private MethodInfo mockMethodInfo() { - URL url = new URL("http://127.0.0.1:8080"); - Map responseHeader = ImmutableMap.of(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE); - HttpURLConnection httpURLConnection = getConnection(url, "GET", null, responseHeader); - MethodInfo methodInfo = MethodInfo.builder() - .invoker(httpURLConnection).retValue(httpURLConnection) - .build(); - return methodInfo; - } - - private static HttpURLConnection getConnection( - URL url, String method, Map requestHeaders, Map responseHeader) throws IOException { - - HttpURLConnection conn = new HttpURLConnection(url) { - - @Override - public void connect() throws IOException { - - } - - @Override - public void disconnect() { - - } - - @Override - public boolean usingProxy() { - return false; - } - - @Override - public int getResponseCode() throws IOException { - return 200; - } - - @Override - public Map> getHeaderFields() { - Map> fields = new HashMap<>(); - for (String key : responseHeader.keySet()) { - fields.put(key, Lists.newArrayList(responseHeader.get(key))); - } - return fields; - } - }; - conn.setRequestMethod(method); - conn.setDoInput(true); - conn.setDoOutput(true); - conn.setRequestProperty(HttpHeaders.HOST, url.getHost()); - if (requestHeaders != null) { - for (String key : requestHeaders.keySet()) { - conn.setRequestProperty(key, requestHeaders.get(key)); - } - } - - return conn; - } } diff --git a/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestConst.java b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestConst.java deleted file mode 100644 index 89c4fee0..00000000 --- a/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestConst.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2021, MegaEase - * All rights reserved. - * - * Licensed 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 com.megaease.easeagent.plugin.httpurlconnection.interceptor; - -public class TestConst { - public static final String RESPONSE_TAG_NAME = "X-EG-Test"; - public static final String RESPONSE_TAG_VALUE = "X-EG-Test-Value"; -} diff --git a/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestUtils.java b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestUtils.java new file mode 100644 index 00000000..4f7fa4f0 --- /dev/null +++ b/plugins/httpurlconnection/src/test/java/com/megaease/easeagent/plugin/httpurlconnection/interceptor/TestUtils.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023, MegaEase + * All rights reserved. + * + * Licensed 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 com.megaease.easeagent.plugin.httpurlconnection.interceptor; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.net.HttpHeaders; +import com.megaease.easeagent.plugin.interceptor.MethodInfo; +import lombok.SneakyThrows; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +final class TestUtils { + public static final String FORWARDED_NAME = "X-Forwarded-For"; + public static final String FORWARDED_VALUE = "testForwarded"; + public static final String RESPONSE_TAG_NAME = "X-EG-Test"; + public static final String RESPONSE_TAG_VALUE = "X-EG-Test-Value"; + + @SneakyThrows + static MethodInfo mockMethodInfo() { + HttpURLConnection httpURLConnection = mockHttpURLConnection(); + return mockMethodInfo(httpURLConnection); + } + + static MethodInfo mockMethodInfo(HttpURLConnection httpURLConnection) { + MethodInfo methodInfo = MethodInfo.builder() + .invoker(httpURLConnection).retValue(httpURLConnection) + .build(); + return methodInfo; + } + + @SneakyThrows + static HttpURLConnection mockHttpURLConnection() { + URL url = new URL("http://127.0.0.1:8080"); + Map responseHeader = ImmutableMap.of(TestUtils.RESPONSE_TAG_NAME, TestUtils.RESPONSE_TAG_VALUE); + return getConnection(url, "GET", null, responseHeader); + } + + static HttpURLConnection getConnection( + URL url, String method, Map requestHeaders, Map responseHeader) throws IOException { + + HttpURLConnection conn = new HttpURLConnection(url) { + + @Override + public void connect() throws IOException { + + } + + @Override + public void disconnect() { + + } + + @Override + public boolean usingProxy() { + return false; + } + + @Override + public int getResponseCode() throws IOException { + return 200; + } + + @Override + public Map> getHeaderFields() { + Map> fields = new HashMap<>(); + for (String key : responseHeader.keySet()) { + fields.put(key, Lists.newArrayList(responseHeader.get(key))); + } + return fields; + } + }; + conn.setRequestMethod(method); + conn.setDoInput(true); + conn.setDoOutput(true); + conn.setRequestProperty(HttpHeaders.HOST, url.getHost()); + if (requestHeaders != null) { + for (String key : requestHeaders.keySet()) { + conn.setRequestProperty(key, requestHeaders.get(key)); + } + } + + return conn; + } +}