google-nomulus/java/google/registry/monitoring/whitebox/StackdriverModule.java
jianglai c702b4486c Use standard java thread factory instead of the AppEngine flavor
With Java 8 in GAE standard environment, we can now use standard java thread factory to run the metric reporter in the background in daemon mode, which would not interfere with basic scaling idle timeout as App Engine thread would.

Because the thread is not created by ThreadManager, no App Engine APIs can be called from it. We therefore use GoogleCredential instead of AppIdentityCredential as HttpRequestInitializer, and NetHttpTransport instead of UlrFetchTransport as HttpTransport.

MetricReporter is lazy injected because it depends on jsonCredential retrieved from CloudKms, which is not available in a test environment, causing FrontendServletTest and BackendServletTest to fail.

Some minor re-formatting with google-java-format on edited files.

Lastly removed moe comments in import statement, which makes the linter unhappy.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=172896227
2017-10-24 16:53:47 -04:00

94 lines
3.8 KiB
Java

// Copyright 2017 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.monitoring.whitebox;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.monitoring.v3.Monitoring;
import com.google.api.services.monitoring.v3.MonitoringScopes;
import com.google.api.services.monitoring.v3.model.MonitoredResource;
import com.google.appengine.api.modules.ModulesService;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import dagger.Module;
import dagger.Provides;
import google.registry.config.RegistryConfig.Config;
import google.registry.monitoring.metrics.MetricReporter;
import google.registry.monitoring.metrics.MetricWriter;
import google.registry.monitoring.metrics.stackdriver.StackdriverWriter;
import java.util.Set;
import org.joda.time.Duration;
/** Dagger module for Google Stackdriver service connection objects. */
@Module
public final class StackdriverModule {
// We need a fake GCE zone to appease Stackdriver's resource model.
// TODO(b/31021585): Revisit this if/when gae_instance exists.
private static final String SPOOFED_GCE_ZONE = "us-central1-f";
@Provides
static Monitoring provideMonitoring(
NetHttpTransport transport,
JsonFactory jsonFactory,
Function<Set<String>, GoogleCredential> credential,
@Config("projectId") String projectId) {
return new Monitoring.Builder(transport, jsonFactory, credential.apply(MonitoringScopes.all()))
.setApplicationName(projectId)
.build();
}
@Provides
static MetricWriter provideMetricWriter(
Monitoring monitoringClient,
@Config("projectId") String projectId,
ModulesService modulesService,
@Config("stackdriverMaxQps") int maxQps,
@Config("stackdriverMaxPointsPerRequest") int maxPointsPerRequest) {
// The MonitoredResource for GAE apps is not writable (and missing fields anyway) so we just
// use the gce_instance resource type instead.
return new StackdriverWriter(
monitoringClient,
projectId,
new MonitoredResource()
.setType("gce_instance")
.setLabels(
ImmutableMap.of(
// The "zone" field MUST be a valid GCE zone, so we fake one.
"zone",
SPOOFED_GCE_ZONE,
// Overload the GCE "instance_id" field with the GAE module name, version and
// instance_id.
"instance_id",
modulesService.getCurrentModule()
+ ":"
+ modulesService.getCurrentVersion()
+ ":"
+ modulesService.getCurrentInstanceId())),
maxQps,
maxPointsPerRequest);
}
@Provides
static MetricReporter provideMetricReporter(
MetricWriter metricWriter, @Config("metricsWriteInterval") Duration writeInterval) {
return new MetricReporter(
metricWriter,
writeInterval.getStandardSeconds(),
new ThreadFactoryBuilder().setDaemon(true).build());
}
}