Refactor metrics servlet to be a Daggerized action

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120235694
This commit is contained in:
mcilwain 2016-04-19 09:21:53 -07:00 committed by Justine Tunney
parent 706f3b44ed
commit 0f4a1035b2
8 changed files with 85 additions and 103 deletions

View file

@ -13,13 +13,8 @@
<load-on-startup>1</load-on-startup> <load-on-startup>1</load-on-startup>
</servlet> </servlet>
<!-- Whitebox Metrics servlet. -->
<servlet>
<servlet-name>metrics</servlet-name>
<servlet-class>com.google.domain.registry.monitoring.whitebox.MetricsTaskServlet</servlet-class>
</servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>metrics</servlet-name> <servlet-name>backend-servlet</servlet-name>
<url-pattern>/_dr/task/metrics</url-pattern> <url-pattern>/_dr/task/metrics</url-pattern>
</servlet-mapping> </servlet-mapping>

View file

@ -40,7 +40,9 @@ import com.google.domain.registry.flows.async.DeleteContactResourceAction;
import com.google.domain.registry.flows.async.DeleteHostResourceAction; import com.google.domain.registry.flows.async.DeleteHostResourceAction;
import com.google.domain.registry.flows.async.DnsRefreshForHostRenameAction; import com.google.domain.registry.flows.async.DnsRefreshForHostRenameAction;
import com.google.domain.registry.mapreduce.MapreduceModule; import com.google.domain.registry.mapreduce.MapreduceModule;
import com.google.domain.registry.monitoring.whitebox.MetricsExportAction;
import com.google.domain.registry.monitoring.whitebox.VerifyEntityIntegrityAction; import com.google.domain.registry.monitoring.whitebox.VerifyEntityIntegrityAction;
import com.google.domain.registry.monitoring.whitebox.WhiteboxModule;
import com.google.domain.registry.rde.BrdaCopyAction; import com.google.domain.registry.rde.BrdaCopyAction;
import com.google.domain.registry.rde.RdeModule; import com.google.domain.registry.rde.RdeModule;
import com.google.domain.registry.rde.RdeReportAction; import com.google.domain.registry.rde.RdeReportAction;
@ -73,6 +75,7 @@ import dagger.Subcomponent;
RequestModule.class, RequestModule.class,
SheetModule.class, SheetModule.class,
TmchModule.class, TmchModule.class,
WhiteboxModule.class,
}) })
interface BackendRequestComponent { interface BackendRequestComponent {
BigqueryPollJobAction bigqueryPollJobAction(); BigqueryPollJobAction bigqueryPollJobAction();
@ -86,6 +89,7 @@ interface BackendRequestComponent {
ExportCommitLogDiffAction exportCommitLogDiffAction(); ExportCommitLogDiffAction exportCommitLogDiffAction();
ExportDomainListsAction exportDomainListsAction(); ExportDomainListsAction exportDomainListsAction();
ExportReservedTermsAction exportReservedTermsAction(); ExportReservedTermsAction exportReservedTermsAction();
MetricsExportAction metricsExportAction();
NordnUploadAction nordnUploadAction(); NordnUploadAction nordnUploadAction();
NordnVerifyAction nordnVerifyAction(); NordnVerifyAction nordnVerifyAction();
PublishDnsUpdatesAction publishDnsUpdatesAction(); PublishDnsUpdatesAction publishDnsUpdatesAction();

View file

@ -66,7 +66,7 @@ public abstract class Metrics {
public void export() { public void export() {
try { try {
String hostname = modulesService.getVersionHostname("backend", null); String hostname = modulesService.getVersionHostname("backend", null);
TaskOptions opts = withUrl(MetricsTaskServlet.PATH) TaskOptions opts = withUrl(MetricsExportAction.PATH)
.header("Host", hostname) .header("Host", hostname)
.param("insertId", idGenerator.get()) .param("insertId", idGenerator.get())
.param("startTime", toBigqueryTimestamp(startTimeMillis, TimeUnit.MILLISECONDS)) .param("startTime", toBigqueryTimestamp(startTimeMillis, TimeUnit.MILLISECONDS))

View file

@ -14,7 +14,11 @@
package com.google.domain.registry.monitoring.whitebox; package com.google.domain.registry.monitoring.whitebox;
import static com.google.domain.registry.util.HttpServletUtils.getRequiredParameterValue; import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Multimaps.filterKeys;
import static com.google.domain.registry.request.Action.Method.POST;
import static com.google.domain.registry.util.FormattingLogger.getLoggerForCallerClass;
import com.google.api.services.bigquery.Bigquery; import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.model.TableDataInsertAllRequest; import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
@ -24,69 +28,58 @@ import com.google.common.base.Function;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.domain.registry.bigquery.BigqueryFactory; import com.google.domain.registry.bigquery.BigqueryFactory;
import com.google.domain.registry.config.RegistryEnvironment; import com.google.domain.registry.config.ConfigModule.Config;
import com.google.domain.registry.request.Action;
import com.google.domain.registry.request.Parameter;
import com.google.domain.registry.request.ParameterMap;
import com.google.domain.registry.util.FormattingLogger; import com.google.domain.registry.util.FormattingLogger;
import com.google.domain.registry.util.NonFinalForTesting;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.http.HttpServlet; import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** Servlet for exporting metrics to BigQuery. */ /** Action for exporting metrics to BigQuery. */
public class MetricsTaskServlet extends HttpServlet { @Action(path = MetricsExportAction.PATH, method = POST)
public class MetricsExportAction implements Runnable {
public static final String PATH = "/_dr/task/metrics"; public static final String PATH = "/_dr/task/metrics";
private static final FormattingLogger logger = getLoggerForCallerClass();
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private static final String DATASET_ID = "metrics"; private static final String DATASET_ID = "metrics";
private static final String PROJECT_ID = RegistryEnvironment.get().config().getProjectId();
private static final Set<String> SPECIAL_PARAMS = ImmutableSet.of("tableId", "insertId"); private static final Set<String> SPECIAL_PARAMS = ImmutableSet.of("tableId", "insertId");
@NonFinalForTesting @Inject @Parameter("tableId") String tableId;
private static BigqueryFactory bigqueryFactory = new BigqueryFactory(); @Inject @Parameter("insertId") String insertId;
@Inject @Config("projectId") String projectId;
/** Returns a filtered {@link ImmutableMap} from an {@link HttpServletRequest} */ @Inject BigqueryFactory bigqueryFactory;
private static ImmutableMap<String, Object> getFilteredMapFromRequest( @Inject @ParameterMap ImmutableListMultimap<String, String> parameters;
HttpServletRequest req, @Inject MetricsExportAction() {}
Set<String> filter) {
ImmutableMap.Builder<String, Object> b = new ImmutableMap.Builder<>();
@SuppressWarnings({"cast", "unchecked"}) // Return type is always a Set<String>.
Set<String> parameterKeys = req.getParameterMap().keySet();
for (String key : Sets.difference(parameterKeys, filter)) {
b.put(key, req.getParameter(key));
}
return b.build();
}
/** Exports metrics to BigQuery. */ /** Exports metrics to BigQuery. */
@Override @Override
public void doPost(HttpServletRequest req, HttpServletResponse rsp) throws IOException { public void run() {
try { try {
final String tableId = getRequiredParameterValue(req, "tableId"); Bigquery bigquery = bigqueryFactory.create(projectId, DATASET_ID, tableId);
ImmutableMap<String, Object> fields = getFilteredMapFromRequest(req, SPECIAL_PARAMS); // Filter out the special parameters that the Action is called with. Everything that's left
Bigquery bigquery = bigqueryFactory.create(PROJECT_ID, DATASET_ID, tableId); // is returned in a Map that is suitable to pass to Bigquery as row data.
Map<String, Object> jsonRows =
ImmutableMap.<String, Object>copyOf(
filterKeys(parameters, not(in(SPECIAL_PARAMS))).entries());
TableDataInsertAllResponse response = bigquery.tabledata() TableDataInsertAllResponse response = bigquery.tabledata()
.insertAll( .insertAll(
PROJECT_ID, projectId,
DATASET_ID, DATASET_ID,
tableId, tableId,
new TableDataInsertAllRequest() new TableDataInsertAllRequest()
.setRows( .setRows(
ImmutableList.of(new TableDataInsertAllRequest.Rows() ImmutableList.of(new TableDataInsertAllRequest.Rows()
.setInsertId(req.getParameter("insertId")) .setInsertId(insertId)
.setJson(fields)))) .setJson(jsonRows))))
.execute(); .execute();
if (response.getInsertErrors() != null && !response.getInsertErrors().isEmpty()) { if (response.getInsertErrors() != null && !response.getInsertErrors().isEmpty()) {

View file

@ -17,6 +17,7 @@ package com.google.domain.registry.monitoring.whitebox;
import com.google.domain.registry.bigquery.BigqueryModule; import com.google.domain.registry.bigquery.BigqueryModule;
import com.google.domain.registry.config.ConfigModule; import com.google.domain.registry.config.ConfigModule;
import com.google.domain.registry.request.Modules.DatastoreServiceModule; import com.google.domain.registry.request.Modules.DatastoreServiceModule;
import com.google.domain.registry.util.SystemSleeper.SystemSleeperModule;
import dagger.Component; import dagger.Component;
@ -29,6 +30,7 @@ import javax.inject.Singleton;
BigqueryModule.class, BigqueryModule.class,
ConfigModule.class, ConfigModule.class,
DatastoreServiceModule.class, DatastoreServiceModule.class,
SystemSleeperModule.class,
WhiteboxModule.class WhiteboxModule.class
}) })
interface WhiteboxComponent { interface WhiteboxComponent {

View file

@ -14,21 +14,36 @@
package com.google.domain.registry.monitoring.whitebox; package com.google.domain.registry.monitoring.whitebox;
import static com.google.domain.registry.request.RequestParameters.extractRequiredParameter;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.domain.registry.util.Sleeper; import com.google.domain.registry.request.Parameter;
import com.google.domain.registry.util.SystemSleeper;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import java.util.UUID; import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
/** /**
* Dagger module for injecting common settings for Whitebox tasks. * Dagger module for injecting common settings for Whitebox tasks.
*/ */
@Module @Module
public class WhiteboxModule { public class WhiteboxModule {
@Provides
@Parameter("tableId")
static String provideTableId(HttpServletRequest req) {
return extractRequiredParameter(req, "tableId");
}
@Provides
@Parameter("insertId")
static String provideInsertId(HttpServletRequest req) {
return extractRequiredParameter(req, "insertId");
}
@Provides @Provides
static Supplier<String> provideIdGenerator() { static Supplier<String> provideIdGenerator() {
return new Supplier<String>() { return new Supplier<String>() {
@ -38,9 +53,4 @@ public class WhiteboxModule {
} }
}; };
} }
@Provides
static Sleeper provideSleeper(SystemSleeper systemSleeper) {
return systemSleeper;
}
} }

View file

@ -29,7 +29,10 @@ public final class HttpServletUtils {
/** /**
* Returns the value of the given request's first {@code name} parameter, or throws * Returns the value of the given request's first {@code name} parameter, or throws
* {@code IllegalArgumentException} if the parameter is not present. * {@code IllegalArgumentException} if the parameter is not present.
*
* @deprecated in favor of <code>RequestParameters.extractRequiredParameter</code>
*/ */
@Deprecated
public static String getRequiredParameterValue(HttpServletRequest req, String name) { public static String getRequiredParameterValue(HttpServletRequest req, String name) {
return checkArgumentNotNull(req.getParameter(name), "Missing required parameter: %s", name); return checkArgumentNotNull(req.getParameter(name), "Missing required parameter: %s", name);
} }

View file

@ -14,6 +14,7 @@
package com.google.domain.registry.monitoring.whitebox; package com.google.domain.registry.monitoring.whitebox;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -28,10 +29,9 @@ import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse; import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse.InsertErrors; import com.google.api.services.bigquery.model.TableDataInsertAllResponse.InsertErrors;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableListMultimap;
import com.google.domain.registry.bigquery.BigqueryFactory; import com.google.domain.registry.bigquery.BigqueryFactory;
import com.google.domain.registry.testing.AppEngineRule; import com.google.domain.registry.testing.AppEngineRule;
import com.google.domain.registry.testing.InjectRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@ -42,17 +42,9 @@ import org.mockito.Matchers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.io.PrintWriter; /** Unit tests for {@link MetricsExportAction}. */
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** Unit tests for {@link MetricsTaskServlet}. */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class MetricsTaskServletTest { public class MetricsExportActionTest {
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -63,9 +55,6 @@ public class MetricsTaskServletTest {
.withTaskQueue() .withTaskQueue()
.build(); .build();
@Rule
public final InjectRule inject = new InjectRule();
@Mock @Mock
BigqueryFactory bigqueryFactory; BigqueryFactory bigqueryFactory;
@ -78,42 +67,23 @@ public class MetricsTaskServletTest {
@Mock @Mock
InsertAll insertAll; InsertAll insertAll;
@Mock private TableDataInsertAllResponse response = new TableDataInsertAllResponse();
private HttpServletRequest req; private long currentTimeMillis = 1000000000000L;
@Mock private ImmutableListMultimap<String, String> parameters =
private HttpServletResponse rsp; new ImmutableListMultimap.Builder<String, String>()
.put("startTime", String.valueOf(MILLISECONDS.toSeconds(currentTimeMillis - 100)))
.put("endTime", String.valueOf(MILLISECONDS.toSeconds(currentTimeMillis)))
.put("jobname", "test job")
.put("status", "success")
.put("tld", "test")
.build();
private final StringWriter httpOutput = new StringWriter(); MetricsExportAction action;
TableDataInsertAllResponse response = new TableDataInsertAllResponse();
long currentTimeMillis = 1000000000000L;
Map<String, Object> params = new ImmutableMap.Builder<String, Object>()
.put("tableId", "eppMetrics")
.put("insertId", "insert id")
.put("startTime", String.valueOf(TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - 100)))
.put("endTime", String.valueOf(TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis)))
.put("jobname", "test job")
.put("status", "success")
.put("tld", "test").build();
MetricsTaskServlet servlet;
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
when(req.getMethod()).thenReturn("POST"); when(bigqueryFactory.create(anyString(), anyString(), anyString())).thenReturn(bigquery);
when(req.getParameterMap()).thenReturn(params);
for (String key : params.keySet()) {
when(req.getParameter(key)).thenReturn((String) params.get(key));
}
when(rsp.getWriter()).thenReturn(new PrintWriter(httpOutput));
inject.setStaticField(MetricsTaskServlet.class, "bigqueryFactory", bigqueryFactory);
when(bigqueryFactory.create(anyString(), anyString(), anyString()))
.thenReturn(bigquery);
when(bigqueryFactory.create( when(bigqueryFactory.create(
anyString(), anyString(),
Matchers.any(HttpTransport.class), Matchers.any(HttpTransport.class),
@ -127,14 +97,19 @@ public class MetricsTaskServletTest {
anyString(), anyString(),
anyString(), anyString(),
Matchers.any(TableDataInsertAllRequest.class))).thenReturn(insertAll); Matchers.any(TableDataInsertAllRequest.class))).thenReturn(insertAll);
servlet = new MetricsTaskServlet(); action = new MetricsExportAction();
action.bigqueryFactory = bigqueryFactory;
action.insertId = "insert id";
action.parameters = parameters;
action.projectId = "project id";
action.tableId = "eppMetrics";
} }
@Test @Test
public void testSuccess_nullErrors() throws Exception { public void testSuccess_nullErrors() throws Exception {
when(insertAll.execute()).thenReturn(response); when(insertAll.execute()).thenReturn(response);
response.setInsertErrors(null); response.setInsertErrors(null);
servlet.service(req, rsp); action.run();
verify(insertAll).execute(); verify(insertAll).execute();
} }
@ -142,7 +117,7 @@ public class MetricsTaskServletTest {
public void testSuccess_emptyErrors() throws Exception { public void testSuccess_emptyErrors() throws Exception {
when(insertAll.execute()).thenReturn(response); when(insertAll.execute()).thenReturn(response);
response.setInsertErrors(ImmutableList.<InsertErrors>of()); response.setInsertErrors(ImmutableList.<InsertErrors>of());
servlet.service(req, rsp); action.run();
verify(insertAll).execute(); verify(insertAll).execute();
} }
@ -150,6 +125,6 @@ public class MetricsTaskServletTest {
public void testFailure_errors() throws Exception { public void testFailure_errors() throws Exception {
when(insertAll.execute()).thenReturn(response); when(insertAll.execute()).thenReturn(response);
response.setInsertErrors(ImmutableList.of(new InsertErrors())); response.setInsertErrors(ImmutableList.of(new InsertErrors()));
servlet.service(req, rsp); action.run();
} }
} }