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