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

Make logging work correctly on Jetty (#2442)

This commit is contained in:
Lai Jiang
2024-05-14 10:36:26 -04:00
committed by GitHub
parent 6a5d8ed3b5
commit 6ca3cc230f
24 changed files with 1005 additions and 1127 deletions

View File

@@ -14,17 +14,23 @@
package google.registry.module;
import static com.google.cloud.logging.TraceLoggingEnhancer.setCurrentTraceId;
import static google.registry.util.GcpJsonFormatter.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.GcpJsonFormatter;
import google.registry.util.JdkLoggerConfig;
import google.registry.util.RandomStringGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
/** Servlet that handles all requests. */
public class RegistryServlet extends ServletBase {
@@ -42,6 +48,17 @@ public class RegistryServlet extends ServletBase {
private final String projectId;
static {
// Remove all other handlers on the root logger to avoid double logging.
JdkLoggerConfig rootLoggerConfig = JdkLoggerConfig.getConfig("");
Arrays.asList(rootLoggerConfig.getHandlers()).forEach(rootLoggerConfig::removeHandler);
Handler rootHandler = new ConsoleHandler();
rootHandler.setLevel(Level.INFO);
rootHandler.setFormatter(new GcpJsonFormatter());
rootLoggerConfig.addHandler(rootHandler);
}
public RegistryServlet() {
super(requestHandler, metricReporter);
this.projectId = component.projectId();

View File

@@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
@@ -55,10 +56,13 @@ class BeamUtilsTest {
}
@Test
void testExtractField_fieldDoesntExist_returnsNull() {
void testExtractField_fieldDoesntExist_throwsException() {
schemaAndRecord.getRecord().put("aFloat", null);
assertThat(BeamUtils.extractField(schemaAndRecord.getRecord(), "aFloat")).isEqualTo("null");
assertThat(BeamUtils.extractField(schemaAndRecord.getRecord(), "missing")).isEqualTo("null");
AvroRuntimeException expected =
assertThrows(
AvroRuntimeException.class,
() -> BeamUtils.extractField(schemaAndRecord.getRecord(), "missing"));
assertThat(expected).hasMessageThat().isEqualTo("Not a valid schema field: missing");
}
@Test
@@ -68,16 +72,12 @@ class BeamUtilsTest {
@Test
void testCheckFieldsNotNull_fieldMissing_throwsException() {
IllegalStateException expected =
AvroRuntimeException expected =
assertThrows(
IllegalStateException.class,
AvroRuntimeException.class,
() ->
BeamUtils.checkFieldsNotNull(
ImmutableList.of("aString", "aFloat", "notAField"), schemaAndRecord));
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"Read unexpected null value for field(s) notAField for record "
+ "{\"aString\": \"hello world\", \"aFloat\": 2.54}");
assertThat(expected).hasMessageThat().isEqualTo("Not a valid schema field: notAField");
}
}