Search in sources :

Example 1 with Middleware

use of com.spotify.apollo.route.Middleware 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)

Aggregations

Histogram (com.codahale.metrics.Histogram)1 RequestContext (com.spotify.apollo.RequestContext)1 Response (com.spotify.apollo.Response)1 AsyncHandler (com.spotify.apollo.route.AsyncHandler)1 Middleware (com.spotify.apollo.route.Middleware)1 Route (com.spotify.apollo.route.Route)1 MetricId (com.spotify.metrics.core.MetricId)1 SemanticMetricRegistry (com.spotify.metrics.core.SemanticMetricRegistry)1 Collections (java.util.Collections)1 ByteString (okio.ByteString)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.allOf (org.hamcrest.Matchers.allOf)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Matchers.hasEntry (org.hamcrest.Matchers.hasEntry)1 Matchers.hasProperty (org.hamcrest.Matchers.hasProperty)1 Test (org.junit.Test)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1