mirror of
https://github.com/google/nomulus.git
synced 2025-05-15 00:47:11 +02:00
Refactor EppMetrics into the EppMetric value type
This change refactors EppMetrics from the mutable self-exporting thing that it was into a real value type EppMetric, and delegates exporting functionality to the BigQueryMetricsEnqueuer. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132387660
This commit is contained in:
parent
b77ebd1df9
commit
42a39b0ddc
16 changed files with 484 additions and 234 deletions
|
@ -22,6 +22,7 @@ java_library(
|
|||
"//third_party/java/appengine:appengine-api",
|
||||
"//third_party/java/appengine_mapreduce2:appengine_mapreduce",
|
||||
"//third_party/java/auto:auto_factory",
|
||||
"//third_party/java/auto:auto_value",
|
||||
"//third_party/java/dagger",
|
||||
"//third_party/java/joda_time",
|
||||
"//third_party/java/jsr305_annotations",
|
||||
|
|
36
java/google/registry/monitoring/whitebox/BigQueryMetric.java
Normal file
36
java/google/registry/monitoring/whitebox/BigQueryMetric.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.monitoring.whitebox;
|
||||
|
||||
import com.google.api.services.bigquery.model.TableFieldSchema;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
/**
|
||||
* A metric which can be encoded into a BigQuery row.
|
||||
*
|
||||
* @see BigQueryMetricsEnqueuer
|
||||
*/
|
||||
public interface BigQueryMetric {
|
||||
|
||||
/** Get the BigQuery table name for this metric. */
|
||||
String getTableId();
|
||||
|
||||
/** Get the schema description for the BigQuery table. */
|
||||
ImmutableList<TableFieldSchema> getSchemaFields();
|
||||
|
||||
/** Get a map of the row values for this metric instance. */
|
||||
ImmutableMap<String, String> getBigQueryRowEncoding();
|
||||
}
|
|
@ -16,67 +16,54 @@ package google.registry.monitoring.whitebox;
|
|||
|
||||
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
|
||||
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
|
||||
import static google.registry.bigquery.BigqueryUtils.toBigqueryTimestamp;
|
||||
|
||||
import com.google.appengine.api.modules.ModulesService;
|
||||
import com.google.appengine.api.modules.ModulesServiceFactory;
|
||||
import com.google.appengine.api.taskqueue.TaskOptions;
|
||||
import com.google.appengine.api.taskqueue.TransientFailureException;
|
||||
import com.google.common.base.Supplier;
|
||||
import google.registry.util.Clock;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import google.registry.util.NonFinalForTesting;
|
||||
import google.registry.util.SystemClock;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/** A collector of metric information. */
|
||||
public abstract class Metrics {
|
||||
/**
|
||||
* A collector of metric information. Enqueues collected metrics to a task queue to be written to
|
||||
* BigQuery asynchronously.
|
||||
*
|
||||
* @see MetricsExportAction
|
||||
*/
|
||||
public class BigQueryMetricsEnqueuer {
|
||||
|
||||
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
||||
|
||||
public static final String QUEUE = "bigquery-streaming-metrics";
|
||||
|
||||
@NonFinalForTesting
|
||||
private static ModulesService modulesService = ModulesServiceFactory.getModulesService();
|
||||
@Inject ModulesService modulesService;
|
||||
|
||||
@NonFinalForTesting
|
||||
private static Clock clock = new SystemClock();
|
||||
@Inject
|
||||
BigQueryMetricsEnqueuer() {}
|
||||
|
||||
@NonFinalForTesting
|
||||
private static Supplier<String> idGenerator =
|
||||
new Supplier<String>() {
|
||||
@Override
|
||||
public String get() {
|
||||
return UUID.randomUUID().toString();
|
||||
}};
|
||||
|
||||
protected final Map<String, Object> fields = new HashMap<>();
|
||||
|
||||
private final long startTimeMillis = clock.nowUtc().getMillis();
|
||||
|
||||
public void setTableId(String tableId) {
|
||||
fields.put("tableId", tableId);
|
||||
}
|
||||
|
||||
public void export() {
|
||||
@VisibleForTesting
|
||||
void export(BigQueryMetric metric, String insertId) {
|
||||
try {
|
||||
String hostname = modulesService.getVersionHostname("backend", null);
|
||||
TaskOptions opts = withUrl(MetricsExportAction.PATH)
|
||||
.header("Host", hostname)
|
||||
.param("insertId", idGenerator.get())
|
||||
.param("startTime", toBigqueryTimestamp(startTimeMillis, TimeUnit.MILLISECONDS))
|
||||
.param("endTime", toBigqueryTimestamp(clock.nowUtc().getMillis(), TimeUnit.MILLISECONDS));
|
||||
for (Entry<String, Object> entry : fields.entrySet()) {
|
||||
opts.param(entry.getKey(), String.valueOf(entry.getValue()));
|
||||
TaskOptions opts =
|
||||
withUrl(MetricsExportAction.PATH)
|
||||
.header("Host", hostname)
|
||||
.param("insertId", insertId);
|
||||
for (Entry<String, String> entry : metric.getBigQueryRowEncoding().entrySet()) {
|
||||
opts.param(entry.getKey(), entry.getValue());
|
||||
}
|
||||
opts.param("tableId", metric.getTableId());
|
||||
getQueue(QUEUE).add(opts);
|
||||
} catch (TransientFailureException e) {
|
||||
// Log and swallow. We may drop some metrics here but this should be rare.
|
||||
logger.info(e, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** Enqueue a metric to be exported to BigQuery. */
|
||||
public void export(BigQueryMetric metric) {
|
||||
export(metric, UUID.randomUUID().toString());
|
||||
}
|
||||
}
|
228
java/google/registry/monitoring/whitebox/EppMetric.java
Normal file
228
java/google/registry/monitoring/whitebox/EppMetric.java
Normal file
|
@ -0,0 +1,228 @@
|
|||
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.monitoring.whitebox;
|
||||
|
||||
import static com.google.apphosting.api.ApiProxy.getCurrentEnvironment;
|
||||
import static google.registry.bigquery.BigqueryUtils.toBigqueryTimestamp;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import com.google.api.services.bigquery.model.TableFieldSchema;
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import google.registry.bigquery.BigqueryUtils.FieldType;
|
||||
import google.registry.model.eppoutput.Result.Code;
|
||||
import google.registry.request.RequestScope;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* A value class for recording attributes of an EPP metric.
|
||||
*
|
||||
* @see BigQueryMetricsEnqueuer
|
||||
*/
|
||||
@AutoValue
|
||||
@RequestScope
|
||||
public abstract class EppMetric implements BigQueryMetric {
|
||||
|
||||
static final String TABLE_ID = "eppMetrics";
|
||||
static final ImmutableList<TableFieldSchema> SCHEMA_FIELDS =
|
||||
ImmutableList.of(
|
||||
new TableFieldSchema().setName("requestId").setType(FieldType.STRING.name()),
|
||||
new TableFieldSchema().setName("startTime").setType(FieldType.TIMESTAMP.name()),
|
||||
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("privilegeLevel").setType(FieldType.STRING.name()),
|
||||
new TableFieldSchema().setName("eppTarget").setType(FieldType.STRING.name()),
|
||||
new TableFieldSchema().setName("eppStatus").setType(FieldType.INTEGER.name()),
|
||||
new TableFieldSchema().setName("attempts").setType(FieldType.INTEGER.name()));
|
||||
|
||||
private static final String REQUEST_LOG_ID = "com.google.appengine.runtime.request_log_id";
|
||||
|
||||
private static EppMetric create(
|
||||
String requestId,
|
||||
DateTime startTimestamp,
|
||||
DateTime endTimestamp,
|
||||
String commandName,
|
||||
String clientId,
|
||||
String privilegeLevel,
|
||||
String eppTarget,
|
||||
Code status,
|
||||
int attempts) {
|
||||
return new AutoValue_EppMetric(
|
||||
requestId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
Optional.ofNullable(commandName),
|
||||
Optional.ofNullable(clientId),
|
||||
Optional.ofNullable(privilegeLevel),
|
||||
Optional.ofNullable(eppTarget),
|
||||
Optional.ofNullable(status),
|
||||
attempts);
|
||||
}
|
||||
|
||||
public abstract String getRequestId();
|
||||
|
||||
public abstract DateTime getStartTimestamp();
|
||||
|
||||
public abstract DateTime getEndTimestamp();
|
||||
|
||||
public abstract Optional<String> getCommandName();
|
||||
|
||||
public abstract Optional<String> getClientId();
|
||||
|
||||
public abstract Optional<String> getPrivilegeLevel();
|
||||
|
||||
public abstract Optional<String> getEppTarget();
|
||||
|
||||
public abstract Optional<Code> getStatus();
|
||||
|
||||
public abstract Integer getAttempts();
|
||||
|
||||
@Override
|
||||
public String getTableId() {
|
||||
return TABLE_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableList<TableFieldSchema> getSchemaFields() {
|
||||
return SCHEMA_FIELDS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableMap<String, String> getBigQueryRowEncoding() {
|
||||
// Create map builder, start with required values
|
||||
ImmutableMap.Builder<String, String> map =
|
||||
ImmutableMap.<String, String>builder()
|
||||
.put("requestId", getRequestId())
|
||||
.put(
|
||||
"startTime",
|
||||
toBigqueryTimestamp(getStartTimestamp().getMillis(), TimeUnit.MILLISECONDS))
|
||||
.put(
|
||||
"endTime",
|
||||
toBigqueryTimestamp(getEndTimestamp().getMillis(), TimeUnit.MILLISECONDS))
|
||||
.put("attempts", getAttempts().toString());
|
||||
// Populate optional values, if present
|
||||
addOptional("commandName", getCommandName(), map);
|
||||
addOptional("clientId", getClientId(), map);
|
||||
addOptional("privilegeLevel", getPrivilegeLevel(), map);
|
||||
addOptional("eppTarget", getEppTarget(), map);
|
||||
addOptional("status", getStatus(), map);
|
||||
|
||||
return map.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to populate an {@link com.google.common.collect.ImmutableMap.Builder} with an
|
||||
* {@link Optional} value if the value is {@link Optional#isPresent()}.
|
||||
*/
|
||||
private static <T> void addOptional(
|
||||
String key, Optional<T> value, ImmutableMap.Builder<String, String> map) {
|
||||
if (value.isPresent()) {
|
||||
map.put(key, value.get().toString());
|
||||
}
|
||||
}
|
||||
|
||||
/** A builder to create instances of {@link EppMetric}. */
|
||||
public static class Builder {
|
||||
|
||||
// Required values
|
||||
private final String requestId;
|
||||
private final DateTime startTimestamp;
|
||||
private int attempts = 0;
|
||||
|
||||
// Optional values
|
||||
private String commandName;
|
||||
private String clientId;
|
||||
private String privilegeLevel;
|
||||
private String eppTarget;
|
||||
private Code status;
|
||||
|
||||
/**
|
||||
* Create an {@link EppMetric.Builder}.
|
||||
*
|
||||
* <p>The start timestamp of metrics created via this instance's {@link Builder#build()} will be
|
||||
* the time that this builder was created.
|
||||
*/
|
||||
@Inject
|
||||
public Builder() {
|
||||
this(DateTime.now(UTC));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
Builder(DateTime startTimestamp) {
|
||||
this.requestId = getCurrentEnvironment().getAttributes().get(REQUEST_LOG_ID).toString();
|
||||
this.startTimestamp = startTimestamp;
|
||||
this.attempts = 0;
|
||||
}
|
||||
|
||||
public Builder setCommandName(String value) {
|
||||
commandName = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setClientId(String value) {
|
||||
clientId = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setPrivilegeLevel(String value) {
|
||||
privilegeLevel = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setEppTarget(String value) {
|
||||
eppTarget = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setStatus(Code value) {
|
||||
status = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder incrementAttempts() {
|
||||
attempts++;
|
||||
return this;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
EppMetric build(DateTime endTimestamp) {
|
||||
return EppMetric.create(
|
||||
requestId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
commandName,
|
||||
clientId,
|
||||
privilegeLevel,
|
||||
eppTarget,
|
||||
status,
|
||||
attempts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an instance of {@link EppMetric} using this builder.
|
||||
*
|
||||
* <p>The end timestamp of the metric will be the current time.
|
||||
*/
|
||||
public EppMetric build() {
|
||||
return build(DateTime.now(UTC));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.monitoring.whitebox;
|
||||
|
||||
import static com.google.apphosting.api.ApiProxy.getCurrentEnvironment;
|
||||
|
||||
import com.google.api.services.bigquery.model.TableFieldSchema;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.bigquery.BigqueryUtils.FieldType;
|
||||
import google.registry.model.eppoutput.Result.Code;
|
||||
import google.registry.request.RequestScope;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/** The EPP Metrics collector. See {@link Metrics}. */
|
||||
@RequestScope
|
||||
public class EppMetrics extends Metrics {
|
||||
|
||||
private static final String REQUEST_LOG_ID = "com.google.appengine.runtime.request_log_id";
|
||||
|
||||
static final String TABLE_ID = "eppMetrics";
|
||||
|
||||
static final ImmutableList<TableFieldSchema> SCHEMA_FIELDS =
|
||||
ImmutableList.of(
|
||||
new TableFieldSchema().setName("requestId").setType(FieldType.STRING.name()),
|
||||
new TableFieldSchema().setName("startTime").setType(FieldType.TIMESTAMP.name()),
|
||||
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("privilegeLevel").setType(FieldType.STRING.name()),
|
||||
new TableFieldSchema().setName("eppTarget").setType(FieldType.STRING.name()),
|
||||
new TableFieldSchema().setName("eppStatus").setType(FieldType.INTEGER.name()),
|
||||
new TableFieldSchema().setName("attempts").setType(FieldType.INTEGER.name()));
|
||||
|
||||
@Inject
|
||||
public EppMetrics() {
|
||||
setTableId(TABLE_ID);
|
||||
fields.put("attempts", 0);
|
||||
fields.put("requestId", getCurrentEnvironment().getAttributes().get(REQUEST_LOG_ID).toString());
|
||||
}
|
||||
|
||||
public void setCommandName(String name) {
|
||||
fields.put("commandName", name);
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
fields.put("clientId", clientId);
|
||||
}
|
||||
|
||||
public void setPrivilegeLevel(String level) {
|
||||
fields.put("privilegeLevel", level);
|
||||
}
|
||||
|
||||
public void setEppTarget(String eppTarget) {
|
||||
fields.put("eppTarget", eppTarget);
|
||||
}
|
||||
|
||||
public void setEppStatus(Code status) {
|
||||
fields.put("eppStatus", String.valueOf(status.code));
|
||||
}
|
||||
|
||||
public void incrementAttempts() {
|
||||
fields.put("attempts", ((int) fields.get("attempts")) + 1);
|
||||
}
|
||||
}
|
|
@ -35,9 +35,9 @@ public class WhiteboxModule {
|
|||
|
||||
@Provides
|
||||
@IntoMap
|
||||
@StringKey(EppMetrics.TABLE_ID)
|
||||
@StringKey(EppMetric.TABLE_ID)
|
||||
static ImmutableList<TableFieldSchema> provideEppMetricsSchema() {
|
||||
return EppMetrics.SCHEMA_FIELDS;
|
||||
return EppMetric.SCHEMA_FIELDS;
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue