Skip to content

Commit

Permalink
Fixed occasional duplicate injection of correlation Headers (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Kunz authored Jun 30, 2020
1 parent f71b38a commit e7890a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
public class ServletApiContextPropagationTest {

public static final int PORT = 9999;

public static final String TEST_PATH = "/test";

public static final String TEST_URL = "http://localhost:" + PORT + TEST_PATH;

private Server server;


@BeforeAll
static void waitForInstrumentation() throws Exception {
TestUtils.waitForClassInstrumentations(Arrays.asList(TestFilter.class, HttpServlet.class, CloseableHttpClient.class,
Expand Down Expand Up @@ -67,14 +68,15 @@ void startServer(Consumer<ServletHandler> shInitializer) {
}
}


public static class TestServlet extends HttpServlet {

public static Map<String, String> lastTags;

public static String writerResponse;

public static String outputStreamResponse;
public static Object upPropagationValue;

public static Object upPropagationValue;

public static void reset() {
lastTags = null;
Expand Down Expand Up @@ -123,6 +125,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
public static class TestFilter implements Filter {

public static Map<String, String> lastTags;

public static Map<String, String> overrideTags;

public static void reset() {
Expand Down Expand Up @@ -167,15 +170,18 @@ void testPropagationViaServlet() throws Exception {
);
TestServlet.reset();


HttpURLConnection urlConnection = (HttpURLConnection) new URL(TEST_URL).openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Correlation-Context", "down_propagated = hello world , something = testPropagationViaServlet");
InternalInspectitContext ctx = Instances.contextManager.enterNewContext();
ctx.setData("down_propagated", "hello world");
ctx.setData("down_propagated_2", "testPropagationViaServlet");
ctx.makeActive();
int code = urlConnection.getResponseCode();
ctx.close();

assertThat(code).isEqualTo(200);
assertThat(TestServlet.lastTags).containsEntry("down_propagated", "hello world");
assertThat(TestServlet.lastTags).containsEntry("something", "testPropagationViaServlet");
assertThat(TestServlet.lastTags).containsEntry("down_propagated_2", "testPropagationViaServlet");
}

@Test
Expand All @@ -187,20 +193,23 @@ void testPropagationHappensOnlyOnce() throws Exception {
TestServlet.reset();
TestFilter.reset();

TestFilter.overrideTags = ImmutableMap.of("something", "overridden!");

TestFilter.overrideTags = ImmutableMap.of("down_propagated_2", "overridden!");

HttpURLConnection urlConnection = (HttpURLConnection) new URL(TEST_URL).openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Correlation-Context", "down_propagated = hello world , something = testPropagationViaServlet");
InternalInspectitContext ctx = Instances.contextManager.enterNewContext();
ctx.setData("down_propagated", "hello world");
ctx.setData("down_propagated_2", "testPropagationViaServlet");
ctx.makeActive();
int code = urlConnection.getResponseCode();
ctx.close();

assertThat(code).isEqualTo(200);
assertThat(TestFilter.lastTags).containsEntry("down_propagated", "hello world");
assertThat(TestFilter.lastTags).containsEntry("something", "testPropagationViaServlet");
assertThat(TestFilter.lastTags).containsEntry("down_propagated_2", "testPropagationViaServlet");

assertThat(TestServlet.lastTags).containsEntry("down_propagated", "hello world");
assertThat(TestServlet.lastTags).containsEntry("something", "overridden!");
assertThat(TestServlet.lastTags).containsEntry("down_propagated_2", "overridden!");
}
}

Expand All @@ -215,15 +224,13 @@ void testUpPropagationWithEmptyResponse() throws Exception {
TestServlet.reset();
TestServlet.upPropagationValue = Math.PI;


HttpURLConnection urlConnection = (HttpURLConnection) new URL(TEST_URL).openConnection();
urlConnection.setRequestMethod("GET");
String correlHeader = urlConnection.getHeaderField("Correlation-Context");

assertThat(correlHeader).contains("up_propagated=" + Math.PI + ";type=d");
}


@Test
void testUpPropagationWithResponseViaWriter() throws Exception {
startServer(sh ->
Expand All @@ -232,17 +239,14 @@ void testUpPropagationWithResponseViaWriter() throws Exception {
TestServlet.upPropagationValue = Math.PI;
TestServlet.writerResponse = "Hallo Welt!";


HttpURLConnection urlConnection = (HttpURLConnection) new URL(TEST_URL).openConnection();
urlConnection.setRequestMethod("GET");
String correlHeader = urlConnection.getHeaderField("Correlation-Context");


assertThat(correlHeader).contains("up_propagated=" + Math.PI + ";type=d");
assertThat(IOUtil.readLines(urlConnection.getInputStream())).containsExactly("Hallo Welt!");
}


@Test
void testUpPropagationWithResponseViaOutputStream() throws Exception {
startServer(sh ->
Expand All @@ -252,12 +256,10 @@ void testUpPropagationWithResponseViaOutputStream() throws Exception {
TestServlet.upPropagationValue = Math.PI;
TestServlet.outputStreamResponse = "Hallo Welt!";


HttpURLConnection urlConnection = (HttpURLConnection) new URL(TEST_URL).openConnection();
urlConnection.setRequestMethod("GET");
String correlHeader = urlConnection.getHeaderField("Correlation-Context");


assertThat(correlHeader).contains("up_propagated=" + Math.PI + ";type=d");
assertThat(IOUtil.readLines(urlConnection.getInputStream())).containsExactly("Hallo Welt!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inspectit:
down_propagated:
down-propagation: GLOBAL
is-tag: true
down_propagated_2:
down-propagation: GLOBAL
is-tag: true
something:
down-propagation: JVM_LOCAL
is-tag: true
is-tag: true
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ inspectit:
Iterator it = headers.entrySet().iterator();
while (it.hasNext()) {
Map$Entry e = (Map$Entry) it.next();
_this.addRequestProperty((String) e.getKey(), (String) e.getValue());
_this.setRequestProperty((String) e.getKey(), (String) e.getValue());
}
} catch (Exception e) {
// silently ignore, this will occur if the url has already been connected
Expand Down

0 comments on commit e7890a0

Please sign in to comment.