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