Automated g4 rollback of changelist 212879670.

*** Reason for rollback ***

Automated tools sometimes don't have default credentials, and can't set them up. We should redo this CL once we figure out the credential thing.

*** Original change description ***

Add metrics for the command used in the registry CLI tool

Puts the metric in <project>/tools/commands_called

It counts the use of the tool, with the following labels:
- environment
- tool (nomulus/gtech)
- command called (class name)
- success true/false
- from the shell true/false

***

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=214048616
This commit is contained in:
guyben 2018-09-21 14:54:16 -07:00 committed by jianglai
parent 89c942b0d9
commit c89cb6a3f3
6 changed files with 4 additions and 145 deletions

View file

@ -69,7 +69,6 @@ java_library(
"@com_google_api_client", "@com_google_api_client",
"@com_google_apis_google_api_services_bigquery", "@com_google_apis_google_api_services_bigquery",
"@com_google_apis_google_api_services_dns", "@com_google_apis_google_api_services_dns",
"@com_google_apis_google_api_services_monitoring",
"@com_google_appengine_api_1_0_sdk", "@com_google_appengine_api_1_0_sdk",
"@com_google_appengine_remote_api", "@com_google_appengine_remote_api",
"@com_google_appengine_remote_api//:link", "@com_google_appengine_remote_api//:link",
@ -81,8 +80,6 @@ java_library(
"@com_google_guava", "@com_google_guava",
"@com_google_http_client", "@com_google_http_client",
"@com_google_http_client_jackson2", "@com_google_http_client_jackson2",
"@com_google_monitoring_client_metrics",
"@com_google_monitoring_client_stackdriver",
"@com_google_oauth_client", "@com_google_oauth_client",
"@com_google_oauth_client_java6", "@com_google_oauth_client_java6",
"@com_google_oauth_client_jetty", "@com_google_oauth_client_jetty",

View file

@ -1,53 +0,0 @@
// Copyright 2018 The Nomulus 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.tools;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.monitoring.v3.Monitoring;
import com.google.api.services.monitoring.v3.model.MonitoredResource;
import com.google.monitoring.metrics.MetricWriter;
import com.google.monitoring.metrics.stackdriver.StackdriverWriter;
import dagger.Module;
import dagger.Provides;
import google.registry.config.CredentialModule.DefaultCredential;
import google.registry.config.RegistryConfig.Config;
/** Dagger module for metrics on the client tool. */
@Module
public final class MetricToolModule {
@Provides
static Monitoring provideMonitoring(
@DefaultCredential GoogleCredential credential, @Config("projectId") String projectId) {
return new Monitoring.Builder(
credential.getTransport(), credential.getJsonFactory(), credential)
.setApplicationName(projectId)
.build();
}
@Provides
static MetricWriter provideMetricWriter(
Monitoring monitoringClient,
@Config("projectId") String projectId,
@Config("stackdriverMaxQps") int maxQps,
@Config("stackdriverMaxPointsPerRequest") int maxPointsPerRequest) {
return new StackdriverWriter(
monitoringClient,
projectId,
new MonitoredResource().setType("global"),
maxQps,
maxPointsPerRequest);
}
}

View file

@ -26,19 +26,10 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.beust.jcommander.ParametersDelegate; import com.beust.jcommander.ParametersDelegate;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.monitoring.metrics.IncrementableMetric;
import com.google.monitoring.metrics.LabelDescriptor;
import com.google.monitoring.metrics.Metric;
import com.google.monitoring.metrics.MetricPoint;
import com.google.monitoring.metrics.MetricRegistryImpl;
import com.google.monitoring.metrics.MetricWriter;
import google.registry.model.ofy.ObjectifyService; import google.registry.model.ofy.ObjectifyService;
import google.registry.tools.params.ParameterFactory; import google.registry.tools.params.ParameterFactory;
import java.io.IOException;
import java.security.Security; import java.security.Security;
import java.util.Map; import java.util.Map;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
@ -62,9 +53,6 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
description = "Returns all command names.") description = "Returns all command names.")
private boolean showAllCommands; private boolean showAllCommands;
@VisibleForTesting
boolean uploadMetrics = true;
// Do not make this final - compile-time constant inlining may interfere with JCommander. // Do not make this final - compile-time constant inlining may interfere with JCommander.
@ParametersDelegate @ParametersDelegate
private AppEngineConnectionFlags appEngineConnectionFlags = private AppEngineConnectionFlags appEngineConnectionFlags =
@ -85,24 +73,6 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
// "shell". // "shell".
private boolean isFirstUse = true; private boolean isFirstUse = true;
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS_FOR_COMMANDS =
ImmutableSet.of(
LabelDescriptor.create("program", "The program used - e.g. nomulus or gtech_tool"),
LabelDescriptor.create("environment", "The environment used - e.g. sandbox"),
LabelDescriptor.create("command", "The command used"),
LabelDescriptor.create("success", "Whether the command succeeded"),
LabelDescriptor.create("shell", "Whether the command was called from the nomulus shell"));
private static final IncrementableMetric commandsCalledCount =
MetricRegistryImpl.getDefault()
.newIncrementableMetric(
"/tools/commands_called",
"Count of tool commands called",
"count",
LABEL_DESCRIPTORS_FOR_COMMANDS);
private MetricWriter metricWriter = null;
Map<String, ? extends Class<? extends Command>> commands; Map<String, ? extends Class<? extends Command>> commands;
String programName; String programName;
@ -124,13 +94,9 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
// http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ104 // http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ104
@Override @Override
public void run(String[] args) throws Exception { public void run(String[] args) throws Exception {
boolean inShell = !isFirstUse;
isFirstUse = false;
// Create the JCommander instance. // Create the JCommander instance.
// If we're in the shell, we don't want to update the RegistryCli's parameters (so we give a JCommander jcommander = new JCommander(this);
// dummy object to update)
JCommander jcommander = new JCommander(inShell ? new Object() : this);
jcommander.addConverterFactory(new ParameterFactory()); jcommander.addConverterFactory(new ParameterFactory());
jcommander.setProgramName(programName); jcommander.setProgramName(programName);
@ -149,8 +115,8 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
// Create the "help" and "shell" commands (these are special in that they don't have a default // Create the "help" and "shell" commands (these are special in that they don't have a default
// constructor). // constructor).
jcommander.addCommand("help", new HelpCommand(jcommander)); jcommander.addCommand("help", new HelpCommand(jcommander));
if (!inShell) { if (isFirstUse) {
// If we aren't inside a shell, then we want to add the shell command. isFirstUse = false;
ShellCommand shellCommand = new ShellCommand(this); ShellCommand shellCommand = new ShellCommand(this);
// We have to build the completions based on the jcommander *before* we add the "shell" // We have to build the completions based on the jcommander *before* we add the "shell"
// command - to avoid completion for the "shell" command itself. // command - to avoid completion for the "shell" command itself.
@ -192,31 +158,17 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
jcommander.getCommands().get(jcommander.getParsedCommand()).getObjects()); jcommander.getCommands().get(jcommander.getParsedCommand()).getObjects());
loggingParams.configureLogging(); // Must be called after parameters are parsed. loggingParams.configureLogging(); // Must be called after parameters are parsed.
boolean success = false;
try { try {
runCommand(command); runCommand(command);
success = true;
} catch (AuthModule.LoginRequiredException ex) { } catch (AuthModule.LoginRequiredException ex) {
System.err.println("==================================================================="); System.err.println("===================================================================");
System.err.println("You must login using 'nomulus login' prior to running this command."); System.err.println("You must login using 'nomulus login' prior to running this command.");
System.err.println("==================================================================="); System.err.println("===================================================================");
} finally {
commandsCalledCount.increment(
programName,
environment.toString(),
command.getClass().getSimpleName(),
String.valueOf(success),
String.valueOf(inShell));
exportMetrics();
} }
} }
@Override @Override
public void close() { public void close() {
exportMetrics();
if (metricWriter != null) {
metricWriter = null;
}
if (installer != null) { if (installer != null) {
installer.uninstall(); installer.uninstall();
installer = null; installer = null;
@ -233,14 +185,6 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
private void runCommand(Command command) throws Exception { private void runCommand(Command command) throws Exception {
injectReflectively(RegistryToolComponent.class, component, command); injectReflectively(RegistryToolComponent.class, component, command);
if (metricWriter == null && uploadMetrics) {
try {
metricWriter = component.metricWriter();
} catch (Exception e) {
System.err.format("Failed to get metricWriter. Got error:\n%s\n\n", e);
uploadMetrics = false;
}
}
if (command instanceof CommandWithConnection) { if (command instanceof CommandWithConnection) {
((CommandWithConnection) command).setConnection(getConnection()); ((CommandWithConnection) command).setConnection(getConnection());
@ -272,25 +216,6 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
command.run(); command.run();
} }
private void exportMetrics() {
if (metricWriter == null) {
return;
}
try {
for (Metric<?> metric : MetricRegistryImpl.getDefault().getRegisteredMetrics()) {
for (MetricPoint<?> point : metric.getTimestampedValues()) {
metricWriter.write(point);
}
}
metricWriter.flush();
} catch (IOException e) {
System.err.format("Failed to export metrics. Got error:\n%s\n\n", e);
System.err.println("Maybe you need to login? Try calling:");
System.err.println(" gcloud auth application-default login");
}
}
@VisibleForTesting
void setEnvironment(RegistryToolEnvironment environment) { void setEnvironment(RegistryToolEnvironment environment) {
this.environment = environment; this.environment = environment;
} }

View file

@ -14,7 +14,6 @@
package google.registry.tools; package google.registry.tools;
import com.google.monitoring.metrics.MetricWriter;
import dagger.Component; import dagger.Component;
import google.registry.bigquery.BigqueryModule; import google.registry.bigquery.BigqueryModule;
import google.registry.config.CredentialModule; import google.registry.config.CredentialModule;
@ -29,7 +28,6 @@ import google.registry.request.Modules.AppIdentityCredentialModule;
import google.registry.request.Modules.DatastoreServiceModule; import google.registry.request.Modules.DatastoreServiceModule;
import google.registry.request.Modules.GoogleCredentialModule; import google.registry.request.Modules.GoogleCredentialModule;
import google.registry.request.Modules.Jackson2Module; import google.registry.request.Modules.Jackson2Module;
import google.registry.request.Modules.NetHttpTransportModule;
import google.registry.request.Modules.URLFetchServiceModule; import google.registry.request.Modules.URLFetchServiceModule;
import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UrlFetchTransportModule;
import google.registry.request.Modules.UseAppIdentityCredentialForGoogleApisModule; import google.registry.request.Modules.UseAppIdentityCredentialForGoogleApisModule;
@ -67,7 +65,6 @@ import javax.inject.Singleton;
Jackson2Module.class, Jackson2Module.class,
KeyModule.class, KeyModule.class,
KmsModule.class, KmsModule.class,
NetHttpTransportModule.class,
RdeModule.class, RdeModule.class,
RegistryToolModule.class, RegistryToolModule.class,
SystemClockModule.class, SystemClockModule.class,
@ -79,7 +76,6 @@ import javax.inject.Singleton;
UserServiceModule.class, UserServiceModule.class,
VoidDnsWriterModule.class, VoidDnsWriterModule.class,
WhoisModule.class, WhoisModule.class,
MetricToolModule.class,
}) })
interface RegistryToolComponent { interface RegistryToolComponent {
void inject(CheckDomainClaimsCommand command); void inject(CheckDomainClaimsCommand command);
@ -118,6 +114,4 @@ interface RegistryToolComponent {
void inject(WhoisQueryCommand command); void inject(WhoisQueryCommand command);
AppEngineConnection appEngineConnection(); AppEngineConnection appEngineConnection();
MetricWriter metricWriter();
} }

View file

@ -2,7 +2,4 @@ handlers = java.util.logging.ConsoleHandler
.level = INFO .level = INFO
com.google.wrappers.base.GoogleInit.level = WARNING com.google.wrappers.base.GoogleInit.level = WARNING
com.google.monitoring.metrics.MetricRegistryImpl.level = WARNING com.google.monitoring.metrics.MetricRegistryImpl.level = WARNING
com.google.monitoring.metrics.MetricReporter.level = WARNING
com.google.monitoring.metrics.MetricExporter.level = WARNING
com.google.monitoring.metrics.stackdriver.StackdriverWriter.level = WARNING

View file

@ -152,7 +152,6 @@ public class ShellCommandTest {
public void testMultipleCommandInvocations() throws Exception { public void testMultipleCommandInvocations() throws Exception {
try (RegistryCli cli = try (RegistryCli cli =
new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class))) { new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class))) {
cli.uploadMetrics = false;
RegistryToolEnvironment.UNITTEST.setup(); RegistryToolEnvironment.UNITTEST.setup();
cli.setEnvironment(RegistryToolEnvironment.UNITTEST); cli.setEnvironment(RegistryToolEnvironment.UNITTEST);
cli.run(new String[] {"test_command", "-x", "xval", "arg1", "arg2"}); cli.run(new String[] {"test_command", "-x", "xval", "arg1", "arg2"});
@ -170,7 +169,7 @@ public class ShellCommandTest {
public void testNonExistentCommand() { public void testNonExistentCommand() {
try (RegistryCli cli = try (RegistryCli cli =
new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class))) { new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class))) {
cli.uploadMetrics = false;
cli.setEnvironment(RegistryToolEnvironment.UNITTEST); cli.setEnvironment(RegistryToolEnvironment.UNITTEST);
assertThrows(MissingCommandException.class, () -> cli.run(new String[] {"bad_command"})); assertThrows(MissingCommandException.class, () -> cli.run(new String[] {"bad_command"}));
} }