use of io.airlift.tracetoken.TraceTokenModule in project airlift by airlift.
the class TestHttpClientBinder method testBindClientWithFilter.
@Test
public void testBindClientWithFilter() throws Exception {
Injector injector = new Bootstrap(binder -> httpClientBinder(binder).bindHttpClient("foo", FooClient.class).withFilter(TestingRequestFilter.class).withFilter(AnotherHttpRequestFilter.class).withTracing(), new TraceTokenModule()).quiet().strictConfig().initialize();
HttpClient httpClient = injector.getInstance(Key.get(HttpClient.class, FooClient.class));
assertFilterCount(httpClient, 3);
}
use of io.airlift.tracetoken.TraceTokenModule in project airlift by airlift.
the class TestHttpClientBinder method testBindingMultipleFiltersAndClients.
@Test
public void testBindingMultipleFiltersAndClients() throws Exception {
Injector injector = new Bootstrap(binder -> {
httpClientBinder(binder).bindHttpClient("foo", FooClient.class).withFilter(TestingRequestFilter.class).withFilter(AnotherHttpRequestFilter.class).withTracing();
httpClientBinder(binder).bindHttpClient("bar", BarClient.class).withFilter(TestingRequestFilter.class).addFilterBinding().to(AnotherHttpRequestFilter.class);
}, new TraceTokenModule()).quiet().strictConfig().initialize();
assertFilterCount(injector.getInstance(Key.get(HttpClient.class, FooClient.class)), 3);
assertFilterCount(injector.getInstance(Key.get(HttpClient.class, BarClient.class)), 2);
}
use of io.airlift.tracetoken.TraceTokenModule in project airlift by airlift.
the class TestHttpClientBinder method testGlobalFilterBinding.
@Test
public void testGlobalFilterBinding() throws Exception {
HttpRequestFilter globalFilter1 = (r) -> r;
HttpRequestFilter globalFilter2 = (r) -> r;
HttpRequestFilter filter1 = (r) -> r;
HttpRequestFilter filter2 = (r) -> r;
Injector injector = new Bootstrap(binder -> {
httpClientBinder(binder).addGlobalFilterBinding().toInstance(globalFilter1);
httpClientBinder(binder).bindGlobalFilter(globalFilter2);
httpClientBinder(binder).bindHttpClient("foo", FooClient.class).addFilterBinding().toInstance(filter1);
httpClientBinder(binder).bindHttpClient("bar", BarClient.class).addFilterBinding().toInstance(filter2);
}, new TraceTokenModule()).quiet().strictConfig().initialize();
JettyHttpClient fooClient = (JettyHttpClient) injector.getInstance(Key.get(HttpClient.class, FooClient.class));
assertFilterCount(fooClient, 3);
assertEquals(fooClient.getRequestFilters().get(0), globalFilter1);
assertEquals(fooClient.getRequestFilters().get(1), globalFilter2);
assertEquals(fooClient.getRequestFilters().get(2), filter1);
JettyHttpClient barClient = (JettyHttpClient) injector.getInstance(Key.get(HttpClient.class, BarClient.class));
assertFilterCount(barClient, 3);
assertEquals(barClient.getRequestFilters().get(0), globalFilter1);
assertEquals(barClient.getRequestFilters().get(1), globalFilter2);
assertEquals(barClient.getRequestFilters().get(2), filter2);
}
use of io.airlift.tracetoken.TraceTokenModule in project airlift by airlift.
the class TestHttpClientBinder method testConfigDefaults.
@Test
public void testConfigDefaults() throws Exception {
Injector injector = new Bootstrap(binder -> httpClientBinder(binder).bindHttpClient("foo", FooClient.class).withConfigDefaults(config -> config.setRequestTimeout(new Duration(33, MINUTES))), new TraceTokenModule()).quiet().strictConfig().initialize();
JettyHttpClient httpClient = (JettyHttpClient) injector.getInstance(Key.get(HttpClient.class, FooClient.class));
assertEquals(httpClient.getRequestTimeoutMillis(), MINUTES.toMillis(33));
}
use of io.airlift.tracetoken.TraceTokenModule in project airlift by airlift.
the class TestHttpServerModule method testHttpRequestEvent.
@Test
public void testHttpRequestEvent() throws Exception {
Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("node.environment", "test").put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
ConfigurationFactory configFactory = new ConfigurationFactory(properties);
Injector injector = Guice.createInjector(new HttpServerModule(), new TestingNodeModule(), new ConfigurationModule(configFactory), new InMemoryEventModule(), new TraceTokenModule(), new Module() {
@Override
public void configure(Binder binder) {
binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON);
}
});
HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
InMemoryEventClient eventClient = injector.getInstance(InMemoryEventClient.class);
EchoServlet echoServlet = (EchoServlet) injector.getInstance(Key.get(Servlet.class, TheServlet.class));
HttpServer server = injector.getInstance(HttpServer.class);
server.start();
URI requestUri = httpServerInfo.getHttpUri().resolve("/my/path");
String userAgent = "my-user-agent";
String referrer = "http://www.google.com";
String token = "this is a trace token";
String requestBody = Joiner.on(" ").join(nCopies(50, "request"));
String requestContentType = "request/type";
int responseCode = 555;
String responseBody = Joiner.on(" ").join(nCopies(100, "response"));
String responseContentType = "response/type";
echoServlet.responseBody = responseBody;
echoServlet.responseStatusCode = responseCode;
echoServlet.responseHeaders.put("Content-Type", responseContentType);
long beforeRequest = System.currentTimeMillis();
long afterRequest;
try (JettyHttpClient client = new JettyHttpClient()) {
// test servlet bound correctly
StringResponse response = client.execute(preparePost().setUri(requestUri).addHeader(USER_AGENT, userAgent).addHeader(CONTENT_TYPE, requestContentType).addHeader(REFERER, referrer).addHeader("X-Airlift-TraceToken", token).setBodyGenerator(createStaticBodyGenerator(requestBody, UTF_8)).build(), createStringResponseHandler());
afterRequest = System.currentTimeMillis();
assertEquals(response.getStatusCode(), responseCode);
assertEquals(response.getBody(), responseBody);
assertEquals(response.getHeader("Content-Type"), responseContentType);
} finally {
server.stop();
}
List<Object> events = eventClient.getEvents();
assertEquals(events.size(), 1);
HttpRequestEvent event = (HttpRequestEvent) events.get(0);
assertEquals(event.getClientAddress(), echoServlet.remoteAddress);
assertEquals(event.getProtocol(), "http");
assertEquals(event.getMethod(), "POST");
assertEquals(event.getRequestUri(), requestUri.getPath());
assertNull(event.getUser());
assertEquals(event.getAgent(), userAgent);
assertEquals(event.getReferrer(), referrer);
assertEquals(event.getTraceToken(), token);
assertEquals(event.getRequestSize(), requestBody.length());
assertEquals(event.getRequestContentType(), requestContentType);
assertEquals(event.getResponseSize(), responseBody.length());
assertEquals(event.getResponseCode(), responseCode);
assertEquals(event.getResponseContentType(), responseContentType);
assertTrue(event.getTimeStamp().toEpochMilli() >= beforeRequest);
assertTrue(event.getTimeToLastByte() <= afterRequest - beforeRequest);
assertNotNull(event.getTimeToFirstByte());
assertTrue(event.getTimeToDispatch() <= event.getTimeToFirstByte());
assertTrue(event.getTimeToFirstByte() <= event.getTimeToLastByte());
}
Aggregations