Skip to content

Commit

Permalink
feat: add forwarded plugin for independent from Trace
Browse files Browse the repository at this point in the history
  • Loading branch information
lanxenet committed Sep 25, 2023
1 parent b131bcc commit f2cb5b7
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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<String, String> 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<String, String> requestHeaders, Map<String, String> 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<String, List<String>> getHeaderFields() {
Map<String, List<String>> 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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> requestHeaders, Map<String, String> 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<String, List<String>> getHeaderFields() {
Map<String, List<String>> 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;
}
}

0 comments on commit f2cb5b7

Please sign in to comment.