TraceContextProcessor extends AbstractProcessor<ExecutionContext> { @Override public void handle(final ExecutionContext context) { String traceparent = context.request().headers().getFirst(HEADER_TRACE_PARENT); if (traceparent == null) { traceparent = TraceparentHelper.buildTraceparentFrom(UUID.random()); context.request().headers().set(HEADER_TRACE_PARENT, traceparent); context.request().headers().remove(HEADER_TRACE_STATE); } else if (!TraceparentHelper.isValid(traceparent)) { traceparent = TraceparentHelper.buildTraceparentFrom(UUID.random()); context.request().headers().set(HEADER_TRACE_PARENT, traceparent); context.request().headers().remove(HEADER_TRACE_STATE); } context.response().headers().set(HEADER_TRACE_PARENT, traceparent); next.handle(context); }  @Override void handle(final ExecutionContext context);  }
@Test public void shouldPropagateSameTraceparent() throws InterruptedException { final CountDownLatch lock = new CountDownLatch(1); final String traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"; request.headers().set(HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); request.headers().set(HEADER_TRACE_PARENT, traceparent); new TraceContextProcessor() .handler(context -> { Assert.assertEquals(traceparent, context.request().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertEquals(traceparent, context.response().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertEquals("congo=ucfJifl5GOE,rojo=00f067aa0ba902b7", context.request().headers().getFirst(TraceContextProcessor.HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void shouldNotPropagate_Tracestate_IfInvalidTraceParent() throws InterruptedException { final CountDownLatch lock = new CountDownLatch(1); final String traceparent = "00-00000000000000000000000000000000-00f067aa0ba902b7-01"; request.headers().set(HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); request.headers().set(HEADER_TRACE_PARENT, traceparent); new TraceContextProcessor() .handler(context -> { Assert.assertNotEquals(traceparent, context.request().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNotEquals(traceparent, context.response().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNull(context.request().headers().getFirst(HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void shouldNotPropagateSameTraceparent_withInvalidVersion() throws InterruptedException { final CountDownLatch lock = new CountDownLatch(1); final String traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"; request.headers().set(HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); request.headers().set(HEADER_TRACE_PARENT, traceparent); new TraceContextProcessor() .handler(context -> { Assert.assertNotEquals(traceparent, context.request().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNotEquals(traceparent, context.response().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNull(context.request().headers().getFirst(HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void shouldNotPropagateSameTraceparent_withInvalidTraceId_All0() throws InterruptedException { final CountDownLatch lock = new CountDownLatch(1); final String traceparent = "00-00000000000000000000000000000000-00f067aa0ba902b7-01"; request.headers().set(HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); request.headers().set(HEADER_TRACE_PARENT, traceparent); new TraceContextProcessor() .handler(context -> { Assert.assertNotEquals(traceparent, context.request().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNotEquals(traceparent, context.response().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNull(context.request().headers().getFirst(HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void shouldNotPropagateSameTraceparent_withInvalidTraceId_Length() throws InterruptedException { final CountDownLatch lock = new CountDownLatch(1); final String traceparent = "00-00f067aa0ba902b7-00f067aa0ba902b7-01"; request.headers().set(HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); request.headers().set(HEADER_TRACE_PARENT, traceparent); new TraceContextProcessor() .handler(context -> { Assert.assertNotEquals(traceparent, context.request().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNotEquals(traceparent, context.response().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNull(context.request().headers().getFirst(HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void shouldNotPropagateSameTraceparent_InvalidParentId() throws InterruptedException { final CountDownLatch lock = new CountDownLatch(1); final String traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000000-01"; request.headers().set(HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); request.headers().set(HEADER_TRACE_PARENT, traceparent); new TraceContextProcessor() .handler(context -> { Assert.assertNotEquals(traceparent, context.request().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNotEquals(traceparent, context.response().headers().getFirst(HEADER_TRACE_PARENT)); Assert.assertNull(context.request().headers().getFirst(HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void shouldHaveTraceparent() throws InterruptedException { final String traceparent_regex = "(([0-9a-e][0-9a-f])|([0-9a-f][0-9a-e]))-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})"; final CountDownLatch lock = new CountDownLatch(1); Mockito.when(request.id()).thenReturn(UUID.toString(UUID.random())); request.headers().set(TraceContextProcessor.HEADER_TRACE_STATE, "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7"); new TraceContextProcessor() .handler(context -> { Assert.assertTrue(context.request().headers().getFirst(HEADER_TRACE_PARENT).matches(traceparent_regex)); Assert.assertTrue(context.response().headers().getFirst(HEADER_TRACE_PARENT).matches(traceparent_regex)); Assert.assertNull(context.request().headers().getFirst(HEADER_TRACE_STATE)); lock.countDown(); }).handle(context); Assert.assertTrue(lock.await(10000, TimeUnit.MILLISECONDS)); }