mirror of
https://github.com/google/nomulus.git
synced 2025-06-07 04:55:42 +02:00
Record metrics for WHOIS commands
Note that this does not write out metrics for invocations of the nomulus tool. This requires a slight refactoring of the existing WhoisResponse interface so as to also support returning the number of results found by the WHOIS query. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=149461208
This commit is contained in:
parent
4eef02f17f
commit
3fcb564251
15 changed files with 247 additions and 62 deletions
|
@ -62,9 +62,9 @@ final class DomainWhoisResponse extends WhoisResponseImpl {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getPlainTextOutput(final boolean preferUnicode, String disclaimer) {
|
||||
public WhoisResponseResults getResponse(final boolean preferUnicode, String disclaimer) {
|
||||
Registrar registrar = getRegistrar(domain.getCurrentSponsorClientId());
|
||||
return new DomainEmitter()
|
||||
String plaintext = new DomainEmitter()
|
||||
.emitField(
|
||||
"Domain Name", maybeFormatHostname(domain.getFullyQualifiedDomainName(), preferUnicode))
|
||||
.emitField("Domain ID", domain.getRepoId())
|
||||
|
@ -97,6 +97,7 @@ final class DomainWhoisResponse extends WhoisResponseImpl {
|
|||
.emitAwipMessage()
|
||||
.emitFooter(disclaimer)
|
||||
.toString();
|
||||
return WhoisResponseResults.create(plaintext, 1);
|
||||
}
|
||||
|
||||
/** Returns the contact of the given type, or null if it does not exist. */
|
||||
|
|
|
@ -42,7 +42,7 @@ final class NameserverWhoisResponse extends WhoisResponseImpl {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getPlainTextOutput(boolean preferUnicode, String disclaimer) {
|
||||
public WhoisResponseResults getResponse(boolean preferUnicode, String disclaimer) {
|
||||
BasicEmitter emitter = new BasicEmitter();
|
||||
for (int i = 0; i < hosts.size(); i++) {
|
||||
HostResource host = hosts.get(i);
|
||||
|
@ -63,6 +63,7 @@ final class NameserverWhoisResponse extends WhoisResponseImpl {
|
|||
emitter.emitNewline();
|
||||
}
|
||||
}
|
||||
return emitter.emitLastUpdated(getTimestamp()).emitFooter(disclaimer).toString();
|
||||
String plaintext = emitter.emitLastUpdated(getTimestamp()).emitFooter(disclaimer).toString();
|
||||
return WhoisResponseResults.create(plaintext, hosts.size());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,9 +43,9 @@ class RegistrarWhoisResponse extends WhoisResponseImpl {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getPlainTextOutput(boolean preferUnicode, String disclaimer) {
|
||||
public WhoisResponseResults getResponse(boolean preferUnicode, String disclaimer) {
|
||||
Set<RegistrarContact> contacts = registrar.getContacts();
|
||||
return new RegistrarEmitter()
|
||||
String plaintext = new RegistrarEmitter()
|
||||
.emitField("Registrar Name", registrar.getRegistrarName())
|
||||
.emitAddress(
|
||||
null,
|
||||
|
@ -62,6 +62,7 @@ class RegistrarWhoisResponse extends WhoisResponseImpl {
|
|||
.emitLastUpdated(getTimestamp())
|
||||
.emitFooter(disclaimer)
|
||||
.toString();
|
||||
return WhoisResponseResults.create(plaintext, 1);
|
||||
}
|
||||
|
||||
/** An emitter with logic for registrars. */
|
||||
|
|
|
@ -46,9 +46,10 @@ public final class Whois {
|
|||
.create(now)
|
||||
.readCommand(new StringReader(query))
|
||||
.executeQuery(now)
|
||||
.getPlainTextOutput(preferUnicode, disclaimer);
|
||||
.getResponse(preferUnicode, disclaimer)
|
||||
.plainTextOutput();
|
||||
} catch (WhoisException e) {
|
||||
return e.getPlainTextOutput(preferUnicode, disclaimer);
|
||||
return e.getResponse(preferUnicode, disclaimer).plainTextOutput();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -60,11 +60,12 @@ public final class WhoisException extends Exception implements WhoisResponse {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getPlainTextOutput(boolean preferUnicode, String disclaimer) {
|
||||
return new WhoisResponseImpl.BasicEmitter()
|
||||
public WhoisResponseResults getResponse(boolean preferUnicode, String disclaimer) {
|
||||
String plaintext = new WhoisResponseImpl.BasicEmitter()
|
||||
.emitRawLine(getMessage())
|
||||
.emitLastUpdated(getTimestamp())
|
||||
.emitFooter(disclaimer)
|
||||
.toString();
|
||||
return WhoisResponseResults.create(plaintext, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import static com.google.common.net.HttpHeaders.LAST_MODIFIED;
|
|||
import static com.google.common.net.HttpHeaders.X_CONTENT_TYPE_OPTIONS;
|
||||
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
@ -33,6 +34,8 @@ import google.registry.request.RequestPath;
|
|||
import google.registry.request.Response;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import google.registry.whois.WhoisMetrics.WhoisMetric;
|
||||
import google.registry.whois.WhoisResponse.WhoisResponseResults;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
@ -133,6 +136,8 @@ public final class WhoisHttpServer implements Runnable {
|
|||
@Inject @Config("whoisHttpExpires") Duration expires;
|
||||
@Inject WhoisReaderFactory whoisReaderFactory;
|
||||
@Inject @RequestPath String requestPath;
|
||||
@Inject WhoisMetric.Builder metricBuilder;
|
||||
@Inject WhoisMetrics whoisMetrics;
|
||||
@Inject WhoisHttpServer() {}
|
||||
|
||||
@Override
|
||||
|
@ -141,27 +146,38 @@ public final class WhoisHttpServer implements Runnable {
|
|||
String path = nullToEmpty(requestPath);
|
||||
try {
|
||||
// Extremely permissive parsing that turns stuff like "/hello/world/" into "hello world".
|
||||
String command = decode(JOINER.join(SLASHER.split(path.substring(PATH.length())))) + "\r\n";
|
||||
String commandText =
|
||||
decode(JOINER.join(SLASHER.split(path.substring(PATH.length())))) + "\r\n";
|
||||
DateTime now = clock.nowUtc();
|
||||
sendResponse(
|
||||
SC_OK,
|
||||
whoisReaderFactory.create(now).readCommand(new StringReader(command)).executeQuery(now));
|
||||
WhoisCommand command =
|
||||
whoisReaderFactory.create(now).readCommand(new StringReader(commandText));
|
||||
metricBuilder.setCommand(command);
|
||||
sendResponse(SC_OK, command.executeQuery(now));
|
||||
} catch (WhoisException e) {
|
||||
metricBuilder.setStatus(e.getStatus());
|
||||
metricBuilder.setNumResults(0);
|
||||
sendResponse(e.getStatus(), e);
|
||||
} catch (IOException e) {
|
||||
metricBuilder.setStatus(SC_INTERNAL_SERVER_ERROR);
|
||||
metricBuilder.setNumResults(0);
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
whoisMetrics.recordWhoisMetric(metricBuilder.build());
|
||||
}
|
||||
}
|
||||
|
||||
private void sendResponse(int status, WhoisResponse whoisResponse) {
|
||||
response.setStatus(status);
|
||||
metricBuilder.setStatus(status);
|
||||
response.setDateHeader(LAST_MODIFIED, whoisResponse.getTimestamp());
|
||||
response.setDateHeader(EXPIRES, whoisResponse.getTimestamp().plus(expires));
|
||||
response.setHeader(CACHE_CONTROL, CACHE_CONTROL_VALUE);
|
||||
response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, CORS_ALLOW_ORIGIN);
|
||||
response.setHeader(X_CONTENT_TYPE_OPTIONS, X_CONTENT_NO_SNIFF);
|
||||
response.setContentType(PLAIN_TEXT_UTF_8);
|
||||
response.setPayload(whoisResponse.getPlainTextOutput(true, disclaimer));
|
||||
WhoisResponseResults results = whoisResponse.getResponse(true, disclaimer);
|
||||
metricBuilder.setNumResults(results.numResults());
|
||||
response.setPayload(results.plainTextOutput());
|
||||
}
|
||||
|
||||
/** Removes {@code %xx} escape codes from request path components. */
|
||||
|
|
|
@ -14,15 +14,18 @@
|
|||
|
||||
package google.registry.whois;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static google.registry.monitoring.metrics.EventMetric.DEFAULT_FITTER;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.monitoring.metrics.EventMetric;
|
||||
import google.registry.monitoring.metrics.IncrementableMetric;
|
||||
import google.registry.monitoring.metrics.LabelDescriptor;
|
||||
import google.registry.monitoring.metrics.MetricRegistryImpl;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.whois.WhoisMetrics.WhoisMetric;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
|
@ -57,36 +60,28 @@ public class WhoisMetrics {
|
|||
@Inject
|
||||
public WhoisMetrics() {}
|
||||
|
||||
/**
|
||||
* Increment a counter which tracks WHOIS requests.
|
||||
*
|
||||
* @see WhoisServer
|
||||
*/
|
||||
public void incrementWhoisRequests(WhoisMetric metric) {
|
||||
/** Records the given {@link WhoisMetric} and its associated processing time. */
|
||||
public void recordWhoisMetric(WhoisMetric metric) {
|
||||
whoisRequests.increment(
|
||||
metric.commandName(),
|
||||
metric.numResults().toString(),
|
||||
metric.status());
|
||||
}
|
||||
|
||||
/** Record the server-side processing time for a WHOIS request. */
|
||||
public void recordProcessingTime(WhoisMetric metric) {
|
||||
metric.commandName().or(""),
|
||||
Integer.toString(metric.numResults()),
|
||||
Integer.toString(metric.status()));
|
||||
processingTime.record(
|
||||
metric.endTimestamp().getMillis() - metric.startTimestamp().getMillis(),
|
||||
metric.commandName(),
|
||||
metric.numResults().toString(),
|
||||
metric.status());
|
||||
metric.commandName().or(""),
|
||||
Integer.toString(metric.numResults()),
|
||||
Integer.toString(metric.status()));
|
||||
}
|
||||
|
||||
/** A value class for recording attributes of a WHOIS metric. */
|
||||
@AutoValue
|
||||
public abstract static class WhoisMetric {
|
||||
|
||||
public abstract String commandName();
|
||||
public abstract Optional<String> commandName();
|
||||
|
||||
public abstract Integer numResults();
|
||||
public abstract int numResults();
|
||||
|
||||
public abstract String status();
|
||||
public abstract int status();
|
||||
|
||||
public abstract DateTime startTimestamp();
|
||||
|
||||
|
@ -111,14 +106,20 @@ public class WhoisMetrics {
|
|||
@AutoValue.Builder
|
||||
public abstract static class Builder {
|
||||
|
||||
boolean wasBuilt = false;
|
||||
|
||||
/** Builder-only clock to support automatic recording of endTimestamp on {@link #build()}. */
|
||||
private Clock clock = null;
|
||||
|
||||
public Builder setCommand(WhoisCommand command) {
|
||||
return setCommandName(command.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public abstract Builder setCommandName(String commandName);
|
||||
|
||||
public abstract Builder setNumResults(Integer numResults);
|
||||
public abstract Builder setNumResults(int numResults);
|
||||
|
||||
public abstract Builder setStatus(String status);
|
||||
public abstract Builder setStatus(int status);
|
||||
|
||||
abstract Builder setStartTimestamp(DateTime startTimestamp);
|
||||
|
||||
|
@ -136,9 +137,12 @@ public class WhoisMetrics {
|
|||
* current timestamp of the clock; otherwise end timestamp must have been previously set.
|
||||
*/
|
||||
public WhoisMetric build() {
|
||||
checkState(
|
||||
!wasBuilt, "WhoisMetric was already built; building it again is most likely an error");
|
||||
if (clock != null) {
|
||||
setEndTimestamp(clock.nowUtc());
|
||||
}
|
||||
wasBuilt = true;
|
||||
return autoBuild();
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ import org.joda.time.DateTime;
|
|||
* @see <a href="http://tools.ietf.org/html/rfc3912">RFC 3912</a>
|
||||
* @see <a href="http://www.iana.org/assignments/registrar-ids">IANA Registrar IDs</a>
|
||||
*/
|
||||
@AutoFactory
|
||||
@AutoFactory(allowSubclasses = true)
|
||||
class WhoisReader {
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,13 +14,14 @@
|
|||
|
||||
package google.registry.whois;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/** Representation of a WHOIS query response. */
|
||||
public interface WhoisResponse {
|
||||
|
||||
/**
|
||||
* Returns a plain text WHOIS response.
|
||||
* Returns the WHOIS response.
|
||||
*
|
||||
* @param preferUnicode if {@code false} will cause the output to be converted to ASCII whenever
|
||||
* possible; for example, converting IDN hostname labels to punycode. However certain things
|
||||
|
@ -29,10 +30,19 @@ public interface WhoisResponse {
|
|||
* be set to {@code true}.
|
||||
* @param disclaimer text to show at bottom of output
|
||||
*/
|
||||
String getPlainTextOutput(boolean preferUnicode, String disclaimer);
|
||||
WhoisResponseResults getResponse(boolean preferUnicode, String disclaimer);
|
||||
|
||||
/**
|
||||
* Returns the time at which this response was created.
|
||||
*/
|
||||
/** Returns the time at which this response was created. */
|
||||
DateTime getTimestamp();
|
||||
|
||||
/** A wraper class for the plaintext response of a WHOIS command and its number of results. */
|
||||
@AutoValue
|
||||
abstract static class WhoisResponseResults {
|
||||
public abstract String plainTextOutput();
|
||||
public abstract int numResults();
|
||||
|
||||
static WhoisResponseResults create(String plainTextOutput, int numResults) {
|
||||
return new AutoValue_WhoisResponse_WhoisResponseResults(plainTextOutput, numResults);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package google.registry.whois;
|
||||
|
||||
import static google.registry.request.Action.Method.POST;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
|
||||
import com.google.common.net.MediaType;
|
||||
|
@ -23,6 +24,8 @@ import google.registry.request.Action;
|
|||
import google.registry.request.Response;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import google.registry.whois.WhoisMetrics.WhoisMetric;
|
||||
import google.registry.whois.WhoisResponse.WhoisResponseResults;
|
||||
import java.io.Reader;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.DateTime;
|
||||
|
@ -60,6 +63,8 @@ public class WhoisServer implements Runnable {
|
|||
@Inject Response response;
|
||||
@Inject WhoisReaderFactory whoisReaderFactory;
|
||||
@Inject @Config("whoisDisclaimer") String disclaimer;
|
||||
@Inject WhoisMetric.Builder metricBuilder;
|
||||
@Inject WhoisMetrics whoisMetrics;
|
||||
@Inject WhoisServer() {}
|
||||
|
||||
@Override
|
||||
|
@ -67,17 +72,23 @@ public class WhoisServer implements Runnable {
|
|||
String responseText;
|
||||
DateTime now = clock.nowUtc();
|
||||
try {
|
||||
responseText =
|
||||
whoisReaderFactory
|
||||
.create(now)
|
||||
.readCommand(input)
|
||||
.executeQuery(now)
|
||||
.getPlainTextOutput(PREFER_UNICODE, disclaimer);
|
||||
WhoisCommand command = whoisReaderFactory.create(now).readCommand(input);
|
||||
metricBuilder.setCommand(command);
|
||||
WhoisResponseResults results =
|
||||
command.executeQuery(now).getResponse(PREFER_UNICODE, disclaimer);
|
||||
responseText = results.plainTextOutput();
|
||||
metricBuilder.setNumResults(results.numResults());
|
||||
metricBuilder.setStatus(SC_OK);
|
||||
} catch (WhoisException e) {
|
||||
responseText = e.getPlainTextOutput(PREFER_UNICODE, disclaimer);
|
||||
WhoisResponseResults results = e.getResponse(PREFER_UNICODE, disclaimer);
|
||||
responseText = results.plainTextOutput();
|
||||
metricBuilder.setNumResults(0);
|
||||
metricBuilder.setStatus(e.getStatus());
|
||||
} catch (Throwable t) {
|
||||
logger.severe(t, "WHOIS request crashed");
|
||||
responseText = "Internal Server Error";
|
||||
metricBuilder.setNumResults(0);
|
||||
metricBuilder.setStatus(SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
// Note that we always return 200 (OK) even if an error was hit. This is because returning an
|
||||
// non-OK HTTP status code will cause the proxy server to silently close the connection. Since
|
||||
|
@ -86,5 +97,6 @@ public class WhoisServer implements Runnable {
|
|||
response.setStatus(SC_OK);
|
||||
response.setContentType(CONTENT_TYPE);
|
||||
response.setPayload(responseText);
|
||||
whoisMetrics.recordWhoisMetric(metricBuilder.build());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue