Search in sources :

Example 1 with TracingPolicy

use of io.vertx.core.tracing.TracingPolicy in project vert.x by eclipse.

the class HttpTracerTestBase method testHttpServerError.

@Test
public void testHttpServerError() throws Exception {
    waitFor(2);
    String key = TestUtils.randomAlphaString(10);
    Object val = new Object();
    AtomicInteger seq = new AtomicInteger();
    tracer = new VertxTracer() {

        @Override
        public Object receiveRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, Iterable headers, TagExtractor tagExtractor) {
            assertNull(context.getLocal(key));
            context.putLocal(key, val);
            assertTrue(seq.compareAndSet(0, 1));
            return request;
        }

        @Override
        public void sendResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) {
            assertTrue(seq.compareAndSet(1, 2));
            assertNull(response);
            assertNotNull(failure);
            assertTrue(context.removeLocal(key));
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        assertEquals(1, seq.get());
        ContextInternal ctx = (ContextInternal) Vertx.currentContext();
        assertSame(val, ctx.localContextData().get(key));
        req.exceptionHandler(v -> {
            assertNull(ctx.localContextData().get(key));
            assertEquals(2, seq.get());
            complete();
        });
    }).listen(8080, "localhost", onSuccess(v -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    client.request(new RequestOptions().setPort(8080).setHost("localhost").setURI("/")).onComplete(onSuccess(req -> {
        req.response(onFailure(err -> {
            complete();
        })).setChunked(true).sendHead(v -> {
            req.connection().close();
        });
    }));
    await();
}
Also used : Context(io.vertx.core.Context) HttpResponse(io.vertx.core.spi.observability.HttpResponse) Vertx(io.vertx.core.Vertx) ContextInternal(io.vertx.core.impl.ContextInternal) HttpRequest(io.vertx.core.spi.observability.HttpRequest) Test(org.junit.Test) Context(io.vertx.core.Context) io.vertx.core.http(io.vertx.core.http) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) TracingPolicy(io.vertx.core.tracing.TracingPolicy) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingPolicy(io.vertx.core.tracing.TracingPolicy) Test(org.junit.Test)

Example 2 with TracingPolicy

use of io.vertx.core.tracing.TracingPolicy in project vert.x by eclipse.

the class HttpTracerTestBase method testHttpServer.

@Test
public void testHttpServer() throws Exception {
    String key = TestUtils.randomAlphaString(10);
    Object val = new Object();
    AtomicInteger seq = new AtomicInteger();
    tracer = new VertxTracer() {

        @Override
        public Object receiveRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, Iterable headers, TagExtractor tagExtractor) {
            assertNull(context.getLocal(key));
            context.putLocal(key, val);
            assertTrue(seq.compareAndSet(0, 1));
            return request;
        }

        @Override
        public void sendResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) {
            assertTrue(seq.compareAndSet(1, 2));
            assertNotNull(response);
            assertTrue(response instanceof HttpServerResponse);
            assertNull(failure);
            assertSame(val, context.getLocal(key));
            assertTrue(context.removeLocal(key));
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        assertEquals(1, seq.get());
        ContextInternal ctx = (ContextInternal) Vertx.currentContext();
        assertSame(val, ctx.localContextData().get(key));
        req.response().endHandler(v -> {
            assertNull(ctx.localContextData().get(key));
            assertEquals(2, seq.get());
        });
        req.response().end();
    }).listen(8080, "localhost", onSuccess(v -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    client.request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(req -> {
        req.send(onSuccess(resp -> {
            testComplete();
        }));
    }));
    await();
}
Also used : Context(io.vertx.core.Context) HttpResponse(io.vertx.core.spi.observability.HttpResponse) Vertx(io.vertx.core.Vertx) ContextInternal(io.vertx.core.impl.ContextInternal) HttpRequest(io.vertx.core.spi.observability.HttpRequest) Test(org.junit.Test) Context(io.vertx.core.Context) io.vertx.core.http(io.vertx.core.http) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) TracingPolicy(io.vertx.core.tracing.TracingPolicy) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingPolicy(io.vertx.core.tracing.TracingPolicy) Test(org.junit.Test)

Example 3 with TracingPolicy

use of io.vertx.core.tracing.TracingPolicy in project vert.x by eclipse.

the class OutboundDeliveryContext method next.

@Override
public void next() {
    if (iter.hasNext()) {
        Handler<DeliveryContext> handler = iter.next();
        try {
            if (handler != null) {
                handler.handle(this);
            } else {
                next();
            }
        } catch (Throwable t) {
            EventBusImpl.log.error("Failure in interceptor", t);
        }
    } else {
        VertxTracer tracer = ctx.tracer();
        if (tracer != null) {
            if (message.trace == null) {
                src = true;
                BiConsumer<String, String> biConsumer = (String key, String val) -> message.headers().set(key, val);
                TracingPolicy tracingPolicy = options.getTracingPolicy();
                if (tracingPolicy == null) {
                    tracingPolicy = TracingPolicy.PROPAGATE;
                }
                message.trace = tracer.sendRequest(ctx, SpanKind.RPC, tracingPolicy, message, message.send ? "send" : "publish", biConsumer, MessageTagExtractor.INSTANCE);
            } else {
                // Handle failure here
                tracer.sendResponse(ctx, null, message.trace, null, TagExtractor.empty());
            }
        }
        bus.sendOrPub(this);
    }
}
Also used : VertxTracer(io.vertx.core.spi.tracing.VertxTracer) TracingPolicy(io.vertx.core.tracing.TracingPolicy) DeliveryContext(io.vertx.core.eventbus.DeliveryContext)

Example 4 with TracingPolicy

use of io.vertx.core.tracing.TracingPolicy in project vert.x by eclipse.

the class HttpTracerTestBase method testHttpClientRequest.

private void testHttpClientRequest(RequestOptions request, String expectedOperation) throws Exception {
    String key = TestUtils.randomAlphaString(10);
    Object val = new Object();
    AtomicInteger seq = new AtomicInteger();
    tracer = new VertxTracer() {

        @Override
        public Object sendRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, BiConsumer headers, TagExtractor tagExtractor) {
            assertSame(val, context.getLocal(key));
            assertTrue(seq.compareAndSet(0, 1));
            headers.accept("header-key", "header-value");
            assertNotNull(request);
            assertTrue(request instanceof HttpRequest);
            assertEquals(expectedOperation, operation);
            return request;
        }

        @Override
        public void receiveResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) {
            assertSame(val, context.getLocal(key));
            assertTrue(context.removeLocal(key));
            assertNotNull(response);
            assertTrue(response instanceof HttpResponse);
            assertNull(failure);
            assertTrue(seq.compareAndSet(1, 2));
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        assertEquals("header-value", req.getHeader("header-key"));
        req.response().end();
    }).listen(8080, "localhost", onSuccess(v -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    Context ctx = vertx.getOrCreateContext();
    ctx.runOnContext(v1 -> {
        ConcurrentMap<Object, Object> tracerMap = ((ContextInternal) ctx).localContextData();
        tracerMap.put(key, val);
        client.request(request, onSuccess(req -> {
            req.send(onSuccess(resp -> {
                resp.endHandler(v2 -> {
                    // Updates are done on the HTTP client context, so we need to run task on this context
                    // to avoid data race
                    ctx.runOnContext(v -> {
                        assertNull(tracerMap.get(key));
                        testComplete();
                    });
                });
            }));
        }));
    });
    await();
}
Also used : Context(io.vertx.core.Context) HttpRequest(io.vertx.core.spi.observability.HttpRequest) HttpResponse(io.vertx.core.spi.observability.HttpResponse) Vertx(io.vertx.core.Vertx) ContextInternal(io.vertx.core.impl.ContextInternal) HttpRequest(io.vertx.core.spi.observability.HttpRequest) Test(org.junit.Test) Context(io.vertx.core.Context) io.vertx.core.http(io.vertx.core.http) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) TracingPolicy(io.vertx.core.tracing.TracingPolicy) HttpResponse(io.vertx.core.spi.observability.HttpResponse) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingPolicy(io.vertx.core.tracing.TracingPolicy) BiConsumer(java.util.function.BiConsumer)

Example 5 with TracingPolicy

use of io.vertx.core.tracing.TracingPolicy in project vert.x by eclipse.

the class HttpTracerTestBase method testHttpClientError.

@Test
public void testHttpClientError() throws Exception {
    String key = TestUtils.randomAlphaString(10);
    Object val = new Object();
    AtomicInteger seq = new AtomicInteger();
    tracer = new VertxTracer() {

        @Override
        public Object sendRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, BiConsumer headers, TagExtractor tagExtractor) {
            assertSame(val, context.getLocal(key));
            assertTrue(seq.compareAndSet(0, 1));
            headers.accept("header-key", "header-value");
            return request;
        }

        @Override
        public void receiveResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) {
            assertSame(val, context.getLocal(key));
            assertTrue(context.removeLocal(key));
            assertNull(response);
            assertNotNull(failure);
            assertTrue(seq.compareAndSet(1, 2));
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        assertEquals("header-value", req.getHeader("header-key"));
        req.connection().close();
    }).listen(8080, "localhost", onSuccess(v -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    Context ctx = vertx.getOrCreateContext();
    ctx.runOnContext(v1 -> {
        ConcurrentMap<Object, Object> tracerMap = ((ContextInternal) ctx).localContextData();
        tracerMap.put(key, val);
        client.request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(req -> {
            req.send(onFailure(err -> {
                assertEquals(2, seq.get());
                assertEquals(2, seq.get());
                assertNull(tracerMap.get(key));
                testComplete();
            }));
        }));
    });
    await();
}
Also used : Context(io.vertx.core.Context) HttpResponse(io.vertx.core.spi.observability.HttpResponse) Vertx(io.vertx.core.Vertx) ContextInternal(io.vertx.core.impl.ContextInternal) HttpRequest(io.vertx.core.spi.observability.HttpRequest) Test(org.junit.Test) Context(io.vertx.core.Context) io.vertx.core.http(io.vertx.core.http) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) TracingPolicy(io.vertx.core.tracing.TracingPolicy) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingPolicy(io.vertx.core.tracing.TracingPolicy) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test)

Aggregations

TracingPolicy (io.vertx.core.tracing.TracingPolicy)5 Context (io.vertx.core.Context)4 Vertx (io.vertx.core.Vertx)4 io.vertx.core.http (io.vertx.core.http)4 ContextInternal (io.vertx.core.impl.ContextInternal)4 HttpRequest (io.vertx.core.spi.observability.HttpRequest)4 HttpResponse (io.vertx.core.spi.observability.HttpResponse)4 TestUtils (io.vertx.test.core.TestUtils)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BiConsumer (java.util.function.BiConsumer)4 Test (org.junit.Test)4 DeliveryContext (io.vertx.core.eventbus.DeliveryContext)1 VertxTracer (io.vertx.core.spi.tracing.VertxTracer)1