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();
}
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();
}
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);
}
}
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();
}
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();
}
Aggregations