Search in sources :

Example 6 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class RouteTransformMetricsExampleTest method responsePayloadSizeHistogram.

/**
 * Middleware to track response payload size in a Histogram,
 * tagged with an endpoint tag set to the given endpoint name.
 */
public Middleware<AsyncHandler<Response<ByteString>>, AsyncHandler<Response<ByteString>>> responsePayloadSizeHistogram(String endpointName) {
    final MetricId histogramId = MetricId.build().tagged("service", serviceName).tagged("endpoint", endpointName).tagged("what", "endpoint-response-size");
    final Histogram histogram = registry.histogram(histogramId);
    return (inner) -> (requestContext) -> inner.invoke(requestContext).whenComplete((response, t) -> {
        if (response != null) {
            histogram.update(response.payload().map(ByteString::size).orElse(0));
        }
    });
}
Also used : Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Histogram(com.codahale.metrics.Histogram) Response(com.spotify.apollo.Response) AsyncHandler(com.spotify.apollo.route.AsyncHandler) RequestContext(com.spotify.apollo.RequestContext) Matchers.allOf(org.hamcrest.Matchers.allOf) MetricId(com.spotify.metrics.core.MetricId) SemanticMetricRegistry(com.spotify.metrics.core.SemanticMetricRegistry) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Middleware(com.spotify.apollo.route.Middleware) Route(com.spotify.apollo.route.Route) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ByteString(okio.ByteString) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) MetricId(com.spotify.metrics.core.MetricId) Histogram(com.codahale.metrics.Histogram) ByteString(okio.ByteString)

Example 7 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class RequestHandlerImplTest method shouldSetRequestContextMetadata.

@Test
public void shouldSetRequestContextMetadata() throws Exception {
    requestHandler.handle(ongoingRequest);
    verify(requestRunnable).run(continuationCaptor.capture());
    continuationCaptor.getValue().accept(ongoingRequest, match);
    final RequestContext requestContext = requestContextCaptor.getValue();
    assertThat(requestContext.metadata(), is(requestMetadata));
}
Also used : RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 8 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class AsyncHandlerTest method shouldFlatMap.

@Test
public void shouldFlatMap() throws Exception {
    RequestContext context = TestContext.forPathArgs(ImmutableMap.of("foo", "bar", "bar", "baz"));
    AsyncHandler<String> a = ctx -> completedFuture(ctx.pathArgs().get("foo"));
    AsyncHandler<String> b = a.flatMap(fooVal -> ctx -> completedFuture(ctx.pathArgs().get(fooVal)));
    String baz = b.invoke(context).toCompletableFuture().get();
    assertThat(baz, is("baz"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assert.assertThat(org.junit.Assert.assertThat) ImmutableMap(com.google.common.collect.ImmutableMap) RequestContext(com.spotify.apollo.RequestContext) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Test(org.junit.Test) RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 9 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class AsyncHandlerTest method shouldFlatMapSync.

@Test
public void shouldFlatMapSync() throws Exception {
    RequestContext context = TestContext.forPathArgs(ImmutableMap.of("foo", "bar", "bar", "baz"));
    AsyncHandler<String> a = ctx -> completedFuture(ctx.pathArgs().get("foo"));
    AsyncHandler<String> b = a.flatMapSync(fooVal -> ctx -> ctx.pathArgs().get(fooVal));
    String baz = b.invoke(context).toCompletableFuture().get();
    assertThat(baz, is("baz"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assert.assertThat(org.junit.Assert.assertThat) ImmutableMap(com.google.common.collect.ImmutableMap) RequestContext(com.spotify.apollo.RequestContext) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Test(org.junit.Test) RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 10 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class RequestHandlerImpl method handleEndpointMatch.

/**
 * Continuation for the {@link RequestRunnableFactory}
 *
 * @param request  request being processed
 * @param match    the match that was made
 */
private void handleEndpointMatch(OngoingRequest request, RuleMatch<Endpoint> match) {
    final Endpoint endpoint = match.getRule().getTarget();
    final Map<String, String> parsedPathArguments = match.parsedPathArguments();
    final Client requestScopedClient = client.wrapRequest(request.request());
    final RequestContext requestContext = RequestContexts.create(request.request(), requestScopedClient, parsedPathArguments, request.arrivalTimeNanos(), request.metadata());
    erf.create(request, requestContext, endpoint).run();
}
Also used : Endpoint(com.spotify.apollo.dispatch.Endpoint) RequestContext(com.spotify.apollo.RequestContext) IncomingRequestAwareClient(com.spotify.apollo.environment.IncomingRequestAwareClient) Client(com.spotify.apollo.Client)

Aggregations

RequestContext (com.spotify.apollo.RequestContext)10 Test (org.junit.Test)7 AsyncHandler (com.spotify.apollo.route.AsyncHandler)4 ByteString (okio.ByteString)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Client (com.spotify.apollo.Client)3 Response (com.spotify.apollo.Response)3 Route (com.spotify.apollo.route.Route)3 CoreMatchers.is (org.hamcrest.CoreMatchers.is)3 Assert.assertThat (org.junit.Assert.assertThat)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Request (com.spotify.apollo.Request)2 Album (com.spotify.apollo.example.data.Album)2 Artist (com.spotify.apollo.example.data.Artist)2 JsonSerializerMiddlewares (com.spotify.apollo.route.JsonSerializerMiddlewares)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2