Search in sources :

Example 1 with TraceTokenModule

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);
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) Key(com.google.inject.Key) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Target(java.lang.annotation.Target) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Assert.assertNotNull(org.testng.Assert.assertNotNull) ElementType(java.lang.annotation.ElementType) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Duration(io.airlift.units.Duration) Retention(java.lang.annotation.Retention) Injector(com.google.inject.Injector) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) HttpClientBinder.httpClientBinder(io.airlift.http.client.HttpClientBinder.httpClientBinder) ImmutableList(com.google.common.collect.ImmutableList) Assert.assertNotSame(org.testng.Assert.assertNotSame) Bootstrap(io.airlift.bootstrap.Bootstrap) Qualifier(javax.inject.Qualifier) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) Test(org.testng.annotations.Test)

Example 2 with TraceTokenModule

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);
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) Key(com.google.inject.Key) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Target(java.lang.annotation.Target) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Assert.assertNotNull(org.testng.Assert.assertNotNull) ElementType(java.lang.annotation.ElementType) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Duration(io.airlift.units.Duration) Retention(java.lang.annotation.Retention) Injector(com.google.inject.Injector) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) HttpClientBinder.httpClientBinder(io.airlift.http.client.HttpClientBinder.httpClientBinder) ImmutableList(com.google.common.collect.ImmutableList) Assert.assertNotSame(org.testng.Assert.assertNotSame) Bootstrap(io.airlift.bootstrap.Bootstrap) Qualifier(javax.inject.Qualifier) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Injector(com.google.inject.Injector) Bootstrap(io.airlift.bootstrap.Bootstrap) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) Test(org.testng.annotations.Test)

Example 3 with TraceTokenModule

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);
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) Key(com.google.inject.Key) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Target(java.lang.annotation.Target) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Assert.assertNotNull(org.testng.Assert.assertNotNull) ElementType(java.lang.annotation.ElementType) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Duration(io.airlift.units.Duration) Retention(java.lang.annotation.Retention) Injector(com.google.inject.Injector) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) HttpClientBinder.httpClientBinder(io.airlift.http.client.HttpClientBinder.httpClientBinder) ImmutableList(com.google.common.collect.ImmutableList) Assert.assertNotSame(org.testng.Assert.assertNotSame) Bootstrap(io.airlift.bootstrap.Bootstrap) Qualifier(javax.inject.Qualifier) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) Test(org.testng.annotations.Test)

Example 4 with TraceTokenModule

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));
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) Key(com.google.inject.Key) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Target(java.lang.annotation.Target) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Assert.assertNotNull(org.testng.Assert.assertNotNull) ElementType(java.lang.annotation.ElementType) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Duration(io.airlift.units.Duration) Retention(java.lang.annotation.Retention) Injector(com.google.inject.Injector) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) HttpClientBinder.httpClientBinder(io.airlift.http.client.HttpClientBinder.httpClientBinder) ImmutableList(com.google.common.collect.ImmutableList) Assert.assertNotSame(org.testng.Assert.assertNotSame) Bootstrap(io.airlift.bootstrap.Bootstrap) Qualifier(javax.inject.Qualifier) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) Duration(io.airlift.units.Duration) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) Test(org.testng.annotations.Test)

Example 5 with TraceTokenModule

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());
}
Also used : HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI) ConfigurationModule(io.airlift.configuration.ConfigurationModule) HttpServerBinder.httpServerBinder(io.airlift.http.server.HttpServerBinder.httpServerBinder) Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) InMemoryEventModule(io.airlift.event.client.InMemoryEventModule) Module(com.google.inject.Module) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) EventModule(io.airlift.event.client.EventModule) ConfigurationModule(io.airlift.configuration.ConfigurationModule) InMemoryEventModule(io.airlift.event.client.InMemoryEventModule) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

Injector (com.google.inject.Injector)7 TraceTokenModule (io.airlift.tracetoken.TraceTokenModule)7 Bootstrap (io.airlift.bootstrap.Bootstrap)6 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)5 Test (org.testng.annotations.Test)5 ImmutableList (com.google.common.collect.ImmutableList)4 Key (com.google.inject.Key)4 LifeCycleManager (io.airlift.bootstrap.LifeCycleManager)4 HttpClientBinder.httpClientBinder (io.airlift.http.client.HttpClientBinder.httpClientBinder)4 Assertions.assertInstanceOf (io.airlift.testing.Assertions.assertInstanceOf)4 Duration (io.airlift.units.Duration)4 ElementType (java.lang.annotation.ElementType)4 Retention (java.lang.annotation.Retention)4 RUNTIME (java.lang.annotation.RetentionPolicy.RUNTIME)4 Target (java.lang.annotation.Target)4 MINUTES (java.util.concurrent.TimeUnit.MINUTES)4 Qualifier (javax.inject.Qualifier)4 Assert.assertEquals (org.testng.Assert.assertEquals)4 Assert.assertFalse (org.testng.Assert.assertFalse)4 Assert.assertNotNull (org.testng.Assert.assertNotNull)4