From e7890a0773b14ec25b2560afe1fff02e9a7476b8 Mon Sep 17 00:00:00 2001 From: Jonas Kunz Date: Tue, 30 Jun 2020 16:39:34 +0200 Subject: [PATCH] Fixed occasional duplicate injection of correlation Headers (#797) --- .../ServletApiContextPropagationTest.java | 38 ++++++++++--------- .../config/global-propagation-test.yml | 5 ++- .../actions/http/httpurlconnection.yml | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/inspectit-ocelot-agent/src/system-test/java/rocks/inspectit/ocelot/instrumentation/special/remote/ServletApiContextPropagationTest.java b/inspectit-ocelot-agent/src/system-test/java/rocks/inspectit/ocelot/instrumentation/special/remote/ServletApiContextPropagationTest.java index e64569fc05..2f2dacf739 100644 --- a/inspectit-ocelot-agent/src/system-test/java/rocks/inspectit/ocelot/instrumentation/special/remote/ServletApiContextPropagationTest.java +++ b/inspectit-ocelot-agent/src/system-test/java/rocks/inspectit/ocelot/instrumentation/special/remote/ServletApiContextPropagationTest.java @@ -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, @@ -67,14 +68,15 @@ void startServer(Consumer shInitializer) { } } - public static class TestServlet extends HttpServlet { public static Map lastTags; + public static String writerResponse; + public static String outputStreamResponse; - public static Object upPropagationValue; + public static Object upPropagationValue; public static void reset() { lastTags = null; @@ -123,6 +125,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) { public static class TestFilter implements Filter { public static Map lastTags; + public static Map overrideTags; public static void reset() { @@ -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 @@ -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!"); } } @@ -215,7 +224,6 @@ 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"); @@ -223,7 +231,6 @@ void testUpPropagationWithEmptyResponse() throws Exception { assertThat(correlHeader).contains("up_propagated=" + Math.PI + ";type=d"); } - @Test void testUpPropagationWithResponseViaWriter() throws Exception { startServer(sh -> @@ -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 -> @@ -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!"); } diff --git a/inspectit-ocelot-agent/src/system-test/resources/config/global-propagation-test.yml b/inspectit-ocelot-agent/src/system-test/resources/config/global-propagation-test.yml index c0eb464165..ae0edddb9c 100644 --- a/inspectit-ocelot-agent/src/system-test/resources/config/global-propagation-test.yml +++ b/inspectit-ocelot-agent/src/system-test/resources/config/global-propagation-test.yml @@ -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 \ No newline at end of file + is-tag: true diff --git a/inspectit-ocelot-config/src/main/resources/rocks/inspectit/ocelot/config/default/instrumentation/actions/http/httpurlconnection.yml b/inspectit-ocelot-config/src/main/resources/rocks/inspectit/ocelot/config/default/instrumentation/actions/http/httpurlconnection.yml index 23007d7edd..e145b99a72 100644 --- a/inspectit-ocelot-config/src/main/resources/rocks/inspectit/ocelot/config/default/instrumentation/actions/http/httpurlconnection.yml +++ b/inspectit-ocelot-config/src/main/resources/rocks/inspectit/ocelot/config/default/instrumentation/actions/http/httpurlconnection.yml @@ -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