Record a version of EPP metrics with TLD for domain commands

Also fixes the issue that dry run EPP commands were incorrectly being
reported on.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171062984
This commit is contained in:
mcilwain 2017-10-04 14:26:20 -07:00 committed by jianglai
parent 7e68ffa16a
commit 302a27f0db
13 changed files with 283 additions and 61 deletions

View file

@ -17,6 +17,7 @@ package google.registry.flows;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.io.BaseEncoding.base64;
import static google.registry.flows.EppXmlTransformer.unmarshal;
import static google.registry.flows.FlowReporter.extractTlds;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.annotations.VisibleForTesting;
@ -89,6 +90,9 @@ public final class EppController {
}
if (!eppInput.getTargetIds().isEmpty()) {
eppMetricBuilder.setEppTarget(Joiner.on(',').join(eppInput.getTargetIds()));
if (eppInput.isDomainResourceType()) {
eppMetricBuilder.setTlds(extractTlds(eppInput.getTargetIds()));
}
}
EppOutput output = runFlowConvertEppErrors(flowComponentBuilder
.flowModule(new FlowModule.Builder()
@ -106,10 +110,12 @@ public final class EppController {
}
return output;
} finally {
EppMetric metric = eppMetricBuilder.build();
bigQueryMetricsEnqueuer.export(metric);
eppMetrics.incrementEppRequests(metric);
eppMetrics.recordProcessingTime(metric);
if (!isDryRun) {
EppMetric metric = eppMetricBuilder.build();
bigQueryMetricsEnqueuer.export(metric);
eppMetrics.incrementEppRequests(metric);
eppMetrics.recordProcessingTime(metric);
}
}
}

View file

@ -14,6 +14,8 @@
package google.registry.flows;
import static google.registry.monitoring.metrics.EventMetric.DEFAULT_FITTER;
import com.google.common.collect.ImmutableSet;
import google.registry.monitoring.metrics.EventMetric;
import google.registry.monitoring.metrics.IncrementableMetric;
@ -25,31 +27,57 @@ import javax.inject.Inject;
/** EPP Instrumentation. */
public class EppMetrics {
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS =
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS_BY_REGISTRAR =
ImmutableSet.of(
LabelDescriptor.create("command", "The name of the command."),
LabelDescriptor.create("client_id", "The name of the client."),
LabelDescriptor.create("status", "The return status of the command."));
private static final IncrementableMetric eppRequests =
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS_BY_TLD =
ImmutableSet.of(
LabelDescriptor.create("command", "The name of the command."),
LabelDescriptor.create("tld", "The TLD acted on by the command (if applicable)."),
LabelDescriptor.create("status", "The return status of the command."));
private static final IncrementableMetric eppRequestsByRegistrar =
MetricRegistryImpl.getDefault()
.newIncrementableMetric(
"/epp/requests", "Count of EPP Requests", "count", LABEL_DESCRIPTORS);
"/epp/requests",
"Count of EPP Requests By Registrar",
"count",
LABEL_DESCRIPTORS_BY_REGISTRAR);
private static final EventMetric processingTime =
private static final IncrementableMetric eppRequestsByTld =
MetricRegistryImpl.getDefault()
.newIncrementableMetric(
"/epp/requests_by_tld",
"Count of EPP Requests By TLD",
"count",
LABEL_DESCRIPTORS_BY_TLD);
private static final EventMetric processingTimeByRegistrar =
MetricRegistryImpl.getDefault()
.newEventMetric(
"/epp/processing_time",
"EPP Processing Time",
"EPP Processing Time By Registrar",
"milliseconds",
LABEL_DESCRIPTORS,
EventMetric.DEFAULT_FITTER);
LABEL_DESCRIPTORS_BY_REGISTRAR,
DEFAULT_FITTER);
private static final EventMetric processingTimeByTld =
MetricRegistryImpl.getDefault()
.newEventMetric(
"/epp/processing_time_by_tld",
"EPP Processing Time By TLD",
"milliseconds",
LABEL_DESCRIPTORS_BY_TLD,
DEFAULT_FITTER);
@Inject
public EppMetrics() {}
/**
* Increment a counter which tracks EPP requests.
* Increments the counters which tracks EPP requests.
*
* @see EppController
* @see FlowRunner
@ -57,20 +85,21 @@ public class EppMetrics {
public void incrementEppRequests(EppMetric metric) {
String eppStatusCode =
metric.getStatus().isPresent() ? String.valueOf(metric.getStatus().get().code) : "";
eppRequests.increment(
metric.getCommandName().or(""),
metric.getClientId().or(""),
eppStatusCode);
eppRequestsByRegistrar.increment(
metric.getCommandName().or(""), metric.getClientId().or(""), eppStatusCode);
eppRequestsByTld.increment(
metric.getCommandName().or(""), metric.getTld().or(""), eppStatusCode);
}
/** Record the server-side processing time for an EPP request. */
/** Records the server-side processing time for an EPP request. */
public void recordProcessingTime(EppMetric metric) {
String eppStatusCode =
metric.getStatus().isPresent() ? String.valueOf(metric.getStatus().get().code) : "";
processingTime.record(
metric.getEndTimestamp().getMillis() - metric.getStartTimestamp().getMillis(),
metric.getCommandName().or(""),
metric.getClientId().or(""),
eppStatusCode);
long processingTime =
metric.getEndTimestamp().getMillis() - metric.getStartTimestamp().getMillis();
processingTimeByRegistrar.record(
processingTime, metric.getCommandName().or(""), metric.getClientId().or(""), eppStatusCode);
processingTimeByTld.record(
processingTime, metric.getCommandName().or(""), metric.getTld().or(""), eppStatusCode);
}
}

View file

@ -65,31 +65,34 @@ public class FlowReporter {
logger.infofmt(
"%s: %s",
EPPINPUT_LOG_SIGNATURE,
JSONValue.toJSONString(ImmutableMap.<String, Object>of(
"xml", prettyPrint(inputXmlBytes),
"xmlBytes", base64().encode(inputXmlBytes))));
JSONValue.toJSONString(
ImmutableMap.<String, Object>of(
"xml", prettyPrint(inputXmlBytes),
"xmlBytes", base64().encode(inputXmlBytes))));
// Explicitly log flow metadata separately from the EPP XML itself so that it stays compact
// enough to be sure to fit in a single log entry (the XML part in rare cases could be long
// enough to overflow into multiple log entries, breaking routine parsing of the JSON format).
String resourceType = eppInput.getResourceType().or("");
boolean isDomain = "domain".equals(resourceType);
String singleTargetId = eppInput.getSingleTargetId().or("");
ImmutableList<String> targetIds = eppInput.getTargetIds();
logger.infofmt(
"%s: %s",
METADATA_LOG_SIGNATURE,
JSONValue.toJSONString(new ImmutableMap.Builder<String, Object>()
.put("serverTrid", trid.getServerTransactionId())
.put("clientId", clientId)
.put("commandType", eppInput.getCommandType())
.put("resourceType", resourceType)
.put("flowClassName", flowClass.getSimpleName())
.put("targetId", singleTargetId)
.put("targetIds", targetIds)
.put("tld", isDomain ? extractTld(singleTargetId).or("") : "")
.put("tlds", isDomain ? extractTlds(targetIds).asList() : EMPTY_LIST)
.put("icannActivityReportField", extractActivityReportField(flowClass))
.build()));
JSONValue.toJSONString(
new ImmutableMap.Builder<String, Object>()
.put("serverTrid", trid.getServerTransactionId())
.put("clientId", clientId)
.put("commandType", eppInput.getCommandType())
.put("resourceType", eppInput.getResourceType().or(""))
.put("flowClassName", flowClass.getSimpleName())
.put("targetId", singleTargetId)
.put("targetIds", targetIds)
.put(
"tld", eppInput.isDomainResourceType() ? extractTld(singleTargetId).or("") : "")
.put(
"tlds",
eppInput.isDomainResourceType() ? extractTlds(targetIds).asList() : EMPTY_LIST)
.put("icannActivityReportField", extractActivityReportField(flowClass))
.build()));
}
/**
@ -113,7 +116,7 @@ public class FlowReporter {
* Returns the set of unique results of {@link #extractTld} applied to each given domain name,
* excluding any absent results (i.e. cases where no TLD was detected).
*/
private static final ImmutableSet<String> extractTlds(Iterable<String> domainNames) {
public static final ImmutableSet<String> extractTlds(Iterable<String> domainNames) {
ImmutableSet.Builder<String> set = new ImmutableSet.Builder<>();
for (String domainName : domainNames) {
Optional<String> extractedTld = extractTld(domainName);

View file

@ -111,6 +111,11 @@ public class EppInput extends ImmutableObject {
return Optional.absent();
}
/** Returns whether this EppInput represents a command that operates on domain resources. */
public boolean isDomainResourceType() {
return getResourceType().or("").equals("domain");
}
@Nullable
private ResourceCommand getResourceCommand() {
InnerCommand innerCommand = commandWrapper.getCommand();

View file

@ -68,6 +68,11 @@ public final class Counter extends AbstractMetric<Long>
*/
private final Striped<Lock> valueLocks = Striped.lock(DEFAULT_CONCURRENCY_LEVEL);
/**
* Constructs a new Counter.
*
* <p>Note that the order of the labels is significant.
*/
Counter(
String name,
String description,

View file

@ -42,6 +42,11 @@ public final class MetricRegistryImpl implements MetricRegistry {
return INSTANCE;
}
/**
* Creates a new event metric.
*
* <p>Note that the order of the labels is significant.
*/
@Override
public EventMetric newEventMetric(
String name,
@ -57,6 +62,11 @@ public final class MetricRegistryImpl implements MetricRegistry {
return metric;
}
/**
* Creates a new gauge metric.
*
* <p>Note that the order of the labels is significant.
*/
@Override
@CanIgnoreReturnValue
public <V> Metric<V> newGauge(
@ -75,6 +85,11 @@ public final class MetricRegistryImpl implements MetricRegistry {
return metric;
}
/**
* Creates a new settable metric.
*
* <p>Note that the order of the labels is significant.
*/
@Override
public <V> SettableMetric<V> newSettableMetric(
String name,
@ -90,6 +105,11 @@ public final class MetricRegistryImpl implements MetricRegistry {
return metric;
}
/**
* Creates a new incrementable metric.
*
* <p>Note that the order of the labels is significant.
*/
@Override
public IncrementableMetric newIncrementableMetric(
String name,

View file

@ -22,8 +22,11 @@ import com.google.auto.value.AutoValue;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import google.registry.bigquery.BigqueryUtils.FieldType;
import google.registry.model.eppoutput.Result.Code;
import google.registry.model.registry.Registries;
import google.registry.util.Clock;
import org.joda.time.DateTime;
@ -43,6 +46,7 @@ public abstract class EppMetric implements BigQueryMetric {
new TableFieldSchema().setName("endTime").setType(FieldType.TIMESTAMP.name()),
new TableFieldSchema().setName("commandName").setType(FieldType.STRING.name()),
new TableFieldSchema().setName("clientId").setType(FieldType.STRING.name()),
new TableFieldSchema().setName("tld").setType(FieldType.STRING.name()),
new TableFieldSchema().setName("privilegeLevel").setType(FieldType.STRING.name()),
new TableFieldSchema().setName("eppTarget").setType(FieldType.STRING.name()),
new TableFieldSchema().setName("eppStatus").setType(FieldType.INTEGER.name()),
@ -58,6 +62,8 @@ public abstract class EppMetric implements BigQueryMetric {
public abstract Optional<String> getClientId();
public abstract Optional<String> getTld();
public abstract Optional<String> getPrivilegeLevel();
public abstract Optional<String> getEppTarget();
@ -88,6 +94,7 @@ public abstract class EppMetric implements BigQueryMetric {
// Populate optional values, if present
addOptional("commandName", getCommandName(), map);
addOptional("clientId", getClientId(), map);
addOptional("tld", getTld(), map);
addOptional("privilegeLevel", getPrivilegeLevel(), map);
addOptional("eppTarget", getEppTarget(), map);
if (getStatus().isPresent()) {
@ -155,6 +162,37 @@ public abstract class EppMetric implements BigQueryMetric {
public abstract Builder setClientId(Optional<String> clientId);
public abstract Builder setTld(String tld);
public abstract Builder setTld(Optional<String> tld);
/**
* Sets the single TLD field from a list of TLDs associated with a command.
*
* <p>Due to cardinality reasons we cannot record combinations of different TLDs as might be
* seen in a domain check command, so if this happens we record "_various" instead. We also
* record "_invalid" for a TLD that does not exist in our system, as again that could blow up
* cardinality. Underscore prefixes are used for these sentinel values so that they cannot be
* confused with actual TLDs, which cannot start with underscores.
*/
public Builder setTlds(ImmutableSet<String> tlds) {
switch (tlds.size()) {
case 0:
setTld(Optional.<String>absent());
break;
case 1:
String tld = Iterables.getOnlyElement(tlds);
// Only record TLDs that actually exist, otherwise we can blow up cardinality by recording
// an arbitrarily large number of strings.
setTld(Optional.fromNullable(Registries.getTlds().contains(tld) ? tld : "_invalid"));
break;
default:
setTld("_various");
break;
}
return this;
}
public abstract Builder setPrivilegeLevel(String privilegeLevel);
public abstract Builder setEppTarget(String eppTarget);