1
0
mirror of https://github.com/google/nomulus synced 2026-01-08 23:23:32 +00:00

Add log traces to Nomulus service on GKE (#2427)

* Add log traces to Nomulus service on GKE

Add request-scope log traces to Nomulus on GKE which, unlike
AppEngine and Cloud Run etc, does not generate traces for hosted
applications. This change only affects the GKE image. It does not affect
the AppEngine services.

Log traces are added to Nomulus-generated logs in request-processing
threads. Forked threads are not covered yet. The single relevant use
case (TimeLimiter) will be addressed in a followup PR.

The main change is in the logging configuration:

*  Use gcp-cloud-logging's LoggingHandler

*  Add gcp-cloud-logging's TraceLoggingEnhancer to the handler.

*  Set a thread-local trace id through the TraceLoggingEnhancer in
   ServletBase on request's entry and clear it on completion.

Also removed an unused class (`RequestLogId`).

* CR

* CR
This commit is contained in:
Weimin Yu
2024-05-07 15:15:46 -04:00
committed by GitHub
parent 54c896cbb9
commit ca072b4861
30 changed files with 706 additions and 482 deletions

View File

@@ -23,6 +23,7 @@ import google.registry.batch.BatchModule;
import google.registry.bigquery.BigqueryModule;
import google.registry.config.CloudTasksUtilsModule;
import google.registry.config.CredentialModule;
import google.registry.config.RegistryConfig.Config;
import google.registry.config.RegistryConfig.ConfigModule;
import google.registry.dns.writer.VoidDnsWriterModule;
import google.registry.export.DriveModule;
@@ -96,6 +97,9 @@ interface RegistryComponent {
Lazy<MetricReporter> metricReporter();
@Config("projectId")
String projectId();
@Module
class RegistryModule {
@Provides

View File

@@ -14,17 +14,51 @@
package google.registry.module;
import static com.google.cloud.logging.TraceLoggingEnhancer.setCurrentTraceId;
import static google.registry.util.RandomStringGenerator.insecureRandomStringGenerator;
import static google.registry.util.StringGenerator.Alphabets.HEX_DIGITS_ONLY;
import com.google.monitoring.metrics.MetricReporter;
import dagger.Lazy;
import google.registry.request.RequestHandler;
import google.registry.util.RandomStringGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/** Servlet that handles all requests. */
public class RegistryServlet extends ServletBase {
// Length of a log trace_id, arbitrarily chosen.
private static final int LOG_TRACE_ID_LENGTH = 32;
// GCP log trace pattern. Fill in project_id and trace id
private static final String LOG_TRACE_PATTERN = "projects/%s/traces/%s";
private static final RandomStringGenerator LOG_TRACE_ID_GENERATOR =
insecureRandomStringGenerator(HEX_DIGITS_ONLY);
private static final RegistryComponent component = DaggerRegistryComponent.create();
private static final RequestHandler<RequestComponent> requestHandler = component.requestHandler();
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
private final String projectId;
public RegistryServlet() {
super(requestHandler, metricReporter);
this.projectId = component.projectId();
}
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
setCurrentTraceId(traceId());
try {
super.service(req, rsp);
} finally {
setCurrentTraceId(null);
}
}
String traceId() {
return String.format(
LOG_TRACE_PATTERN, projectId, LOG_TRACE_ID_GENERATOR.createString(LOG_TRACE_ID_LENGTH));
}
}

View File

@@ -1,31 +0,0 @@
// 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.request;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import javax.inject.Qualifier;
/**
* Dagger qualifier for the AppEngine request_log_id.
*
* <p>This is the unique log identifier of the current request.
*/
@Retention(RUNTIME)
@Qualifier
@Documented
public @interface RequestLogId {}