Add metrics measuring all request processing times

The cardinality of this new metric is:
buckets - 16
path + method - around 100 (the number of Actions we have)
authLevel - at most 3
success - 2

Total: 16*100*3*2 = 9,600

This is still low, especially for the value it could give in understanding our system (graphs of all endpoints, how often are they called, how long they take, how often do they fail)

Instead of "success true/false", we might want to give the actual status code. This can be a bit annoying because HttpServletResponse doesn't have a getStatus.

But it's possible, and worth considering.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=219312400
This commit is contained in:
guyben 2018-10-30 09:23:55 -07:00 committed by jianglai
parent a45d3d3bc7
commit 57f06258d3
5 changed files with 103 additions and 3 deletions

View file

@ -23,6 +23,8 @@ import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import com.google.common.flogger.FluentLogger;
import google.registry.request.auth.AuthResult;
import google.registry.request.auth.RequestAuthenticator;
import google.registry.util.NonFinalForTesting;
import google.registry.util.SystemClock;
import google.registry.util.TypeUtils.TypeInstantiator;
import java.io.IOException;
import java.util.Optional;
@ -30,6 +32,8 @@ import javax.annotation.Nullable;
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;
import org.joda.time.Duration;
/**
* Dagger-based request processor.
@ -64,6 +68,10 @@ public class RequestHandler<C> {
private final Router router;
private final Provider<? extends RequestComponentBuilder<C>> requestComponentBuilderProvider;
private final RequestAuthenticator requestAuthenticator;
private final SystemClock clock = new SystemClock();
@NonFinalForTesting
RequestMetrics requestMetrics = new RequestMetrics();
/**
* Constructor for subclasses to create a new request handler for a specific request component.
@ -143,6 +151,8 @@ public class RequestHandler<C> {
.requestModule(new RequestModule(req, rsp, authResult.get()))
.build();
// Apply the selected Route to the component to produce an Action instance, and run it.
boolean success = true;
DateTime startTime = clock.nowUtc();
try {
route.get().instantiator().apply(component).run();
if (route.get().action().automaticallyPrintOk()) {
@ -151,6 +161,14 @@ public class RequestHandler<C> {
}
} catch (HttpException e) {
e.send(rsp);
success = false;
} finally {
requestMetrics.record(
new Duration(startTime, clock.nowUtc()),
path,
method,
authResult.get().authLevel(),
success);
}
}
}