1
0
mirror of https://github.com/google/nomulus synced 2026-01-05 04:56:03 +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

@@ -11,20 +11,20 @@ com.github.docker-java:docker-java-transport:3.3.6=testCompileClasspath,testRunt
com.github.kevinstern:software-and-algorithms:1.0=annotationProcessor,errorprone,testAnnotationProcessor
com.google.android:annotations:4.1.1.4=deploy_jar,runtimeClasspath,testRuntimeClasspath
com.google.api-client:google-api-client:1.35.2=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-cloud-tasks-v2:2.42.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-cloud-tasks-v2beta2:0.132.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-cloud-tasks-v2beta3:0.132.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-common-protos:2.38.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-iam-v1:1.33.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:api-common:2.30.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:gax-grpc:2.47.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:gax-httpjson:2.47.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:gax:2.47.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-api-1.0-sdk:2.0.26=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-api-stubs:2.0.26=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-remote-api:2.0.26=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-testing:2.0.26=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-tools-sdk:2.0.26=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-cloud-tasks-v2:2.43.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-cloud-tasks-v2beta2:0.133.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-cloud-tasks-v2beta3:0.133.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-common-protos:2.39.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api.grpc:proto-google-iam-v1:1.34.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:api-common:2.31.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:gax-grpc:2.48.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:gax-httpjson:2.48.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.api:gax:2.48.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-api-1.0-sdk:2.0.27=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-api-stubs:2.0.27=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-remote-api:2.0.27=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-testing:2.0.27=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.appengine:appengine-tools-sdk:2.0.27=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.auth:google-auth-library-credentials:1.23.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.auth:google-auth-library-oauth2-http:1.23.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.auto.service:auto-service-annotations:1.0.1=annotationProcessor,errorprone,testAnnotationProcessor
@@ -32,7 +32,7 @@ com.google.auto.value:auto-value-annotations:1.10.4=compileClasspath,deploy_jar,
com.google.auto.value:auto-value-annotations:1.9=annotationProcessor,errorprone,testAnnotationProcessor
com.google.auto.value:auto-value:1.10.4=annotationProcessor,deploy_jar,runtimeClasspath,testAnnotationProcessor,testRuntimeClasspath
com.google.auto:auto-common:1.2.1=annotationProcessor,errorprone,testAnnotationProcessor
com.google.cloud:google-cloud-tasks:2.42.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.cloud:google-cloud-tasks:2.43.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.code.findbugs:jsr305:3.0.2=annotationProcessor,checkstyle,compileClasspath,deploy_jar,errorprone,runtimeClasspath,testAnnotationProcessor,testCompileClasspath,testRuntimeClasspath
com.google.code.gson:gson:2.10.1=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.dagger:dagger-compiler:2.51.1=annotationProcessor,testAnnotationProcessor
@@ -54,11 +54,12 @@ com.google.googlejavaformat:google-java-format:1.5=annotationProcessor,testAnnot
com.google.guava:failureaccess:1.0.1=checkstyle,errorprone
com.google.guava:failureaccess:1.0.2=annotationProcessor,compileClasspath,deploy_jar,runtimeClasspath,testAnnotationProcessor,testCompileClasspath,testRuntimeClasspath
com.google.guava:guava-parent:32.1.1-jre=errorprone
com.google.guava:guava-testlib:33.1.0-jre=testCompileClasspath,testRuntimeClasspath
com.google.guava:guava-testlib:33.2.0-jre=testCompileClasspath,testRuntimeClasspath
com.google.guava:guava:31.0.1-jre=checkstyle
com.google.guava:guava:32.1.1-jre=errorprone
com.google.guava:guava:33.0.0-jre=annotationProcessor,testAnnotationProcessor
com.google.guava:guava:33.1.0-jre=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.guava:guava:33.1.0-jre=compileClasspath,deploy_jar,runtimeClasspath
com.google.guava:guava:33.2.0-jre=testCompileClasspath,testRuntimeClasspath
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=annotationProcessor,checkstyle,compileClasspath,deploy_jar,runtimeClasspath,testAnnotationProcessor,testCompileClasspath,testRuntimeClasspath
com.google.http-client:google-http-client-apache-v2:1.42.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.http-client:google-http-client-gson:1.44.1=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
@@ -101,8 +102,6 @@ io.grpc:grpc-xds:1.62.2=deploy_jar,runtimeClasspath,testRuntimeClasspath
io.opencensus:opencensus-api:0.31.1=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.opencensus:opencensus-contrib-http-util:0.31.1=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.opencensus:opencensus-proto:0.2.0=deploy_jar,runtimeClasspath,testRuntimeClasspath
io.opentelemetry:opentelemetry-api:1.37.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.opentelemetry:opentelemetry-context:1.37.0=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.perfmark:perfmark-api:0.27.0=deploy_jar,runtimeClasspath,testRuntimeClasspath
jakarta.inject:jakarta.inject-api:1.0.5=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
javax.activation:activation:1.1=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
@@ -113,8 +112,8 @@ javax.mail:mail:1.5.0-b01=compileClasspath,deploy_jar,runtimeClasspath,testCompi
javax.xml.bind:jaxb-api:2.4.0-b180830.0359=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
joda-time:joda-time:2.12.7=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
junit:junit:4.13.2=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.12=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.14.12=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.15=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.14.15=testCompileClasspath,testRuntimeClasspath
net.java.dev.jna:jna:5.13.0=testCompileClasspath,testRuntimeClasspath
net.ltgt.gradle.incap:incap:0.2=annotationProcessor,testAnnotationProcessor
net.sf.saxon:Saxon-HE:10.6=checkstyle
@@ -156,8 +155,8 @@ org.junit.platform:junit-platform-runner:1.11.0-M1=testCompileClasspath,testRunt
org.junit.platform:junit-platform-suite-api:1.11.0-M1=testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-suite-commons:1.11.0-M1=testRuntimeClasspath
org.junit:junit-bom:5.11.0-M1=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-core:5.11.0=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-junit-jupiter:5.11.0=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-core:5.12.0=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-junit-jupiter:5.12.0=testCompileClasspath,testRuntimeClasspath
org.objenesis:objenesis:3.3=testRuntimeClasspath
org.opentest4j:opentest4j:1.3.0=testCompileClasspath,testRuntimeClasspath
org.ow2.asm:asm-commons:9.6=jacocoAnt
@@ -167,8 +166,8 @@ org.pcollections:pcollections:3.1.4=annotationProcessor,errorprone,testAnnotatio
org.reflections:reflections:0.10.2=checkstyle
org.rnorth.duct-tape:duct-tape:1.0.8=testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:1.7.36=testCompileClasspath,testRuntimeClasspath
org.testcontainers:junit-jupiter:1.19.7=testCompileClasspath,testRuntimeClasspath
org.testcontainers:testcontainers:1.19.7=testCompileClasspath,testRuntimeClasspath
org.testcontainers:junit-jupiter:1.19.8=testCompileClasspath,testRuntimeClasspath
org.testcontainers:testcontainers:1.19.8=testCompileClasspath,testRuntimeClasspath
org.threeten:threetenbp:1.6.9=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.yaml:snakeyaml:2.2=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
empty=

View File

@@ -14,7 +14,6 @@
package google.registry.util;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.LogSite;
import com.google.common.flogger.backend.LogData;
import com.google.common.flogger.backend.system.SimpleLogRecord;
@@ -52,6 +51,9 @@ public class GcpJsonFormatter extends Formatter {
*/
private static final String SOURCE_LOCATION = "logging.googleapis.com/sourceLocation";
/** JSON field that stores the trace associated with the log entry, if any. */
private static final String TRACE = "logging.googleapis.com/trace";
/** JSON field that contains the content, this will show up as the main entry in a log. */
private static final String MESSAGE = "message";
@@ -63,6 +65,30 @@ public class GcpJsonFormatter extends Formatter {
private static final Gson gson = new Gson();
private static final ThreadLocal<String> traceId = new ThreadLocal<>();
/**
* Set the Trace ID associated with any logging done by the current thread.
*
* @param id The traceID, in the form projects/[PROJECT_ID]/traces/[TRACE_ID]
*/
public static void setCurrentTraceId(String id) {
if (id == null) {
traceId.remove();
} else {
traceId.set(id);
}
}
/**
* Get the Trace ID associated with any logging done by the current thread.
*
* @return id The traceID
*/
public static String getCurrentTraceId() {
return traceId.get();
}
@Override
public String format(LogRecord record) {
// Add an extra newline before the message for better displaying of multi-line logs. To see the
@@ -70,7 +96,6 @@ public class GcpJsonFormatter extends Formatter {
// newline makes sure that the entire message starts on its own line, so that indentation within
// the message is correct.
String message = '\n' + record.getMessage();
String severity = severityFor(record.getLevel());
// The rest is mostly lifted from java.util.logging.SimpleFormatter.
@@ -83,6 +108,7 @@ public class GcpJsonFormatter extends Formatter {
}
stacktrace = sw.toString();
}
String message = '\n' + record.getMessage() + stacktrace;
String function = "";
if (record.getSourceClassName() != null) {
@@ -113,19 +139,18 @@ public class GcpJsonFormatter extends Formatter {
if (!function.isEmpty()) {
sourceLocation.put(FUNCTION, function);
}
return gson.toJson(
ImmutableMap.of(
SEVERITY,
severity,
SOURCE_LOCATION,
sourceLocation,
// ImmutableMap.of(FILE, file, LINE, line, FUNCTION, function),
MESSAGE,
message + stacktrace))
// This trailing newline is required for the proxy because otherwise multiple logs might be
// sent to Stackdriver together (due to the async nature of the proxy), and not parsed
// correctly.
+ '\n';
Map<String, Object> json = new LinkedHashMap<>();
json.put(SEVERITY, severity);
json.put(SOURCE_LOCATION, sourceLocation);
json.put(MESSAGE, message);
if (traceId.get() != null) {
json.put(TRACE, traceId.get());
}
// This trailing newline is required for the proxy because otherwise multiple logs might be
// sent to Stackdriver together (due to the async nature of the proxy), and not parsed
// correctly.
return gson.toJson(json) + '\n';
}
/**

View File

@@ -69,6 +69,7 @@ class GcpJsonFormatterTest {
@AfterEach
void afterEach() {
jdkLogger.removeHandler(handler);
GcpJsonFormatter.setCurrentTraceId(null);
}
@Test
@@ -76,7 +77,21 @@ class GcpJsonFormatterTest {
logger.atInfo().log("Something I have to say");
handler.close();
String output = ostream.toString(StandardCharsets.US_ASCII);
assertThat(output).isEqualTo(makeJson("INFO", 76, "testSuccess", "Something I have to say"));
assertThat(output).isEqualTo(makeJson("INFO", 77, "testSuccess", "Something I have to say"));
}
@Test
void testSuccess_traceId() {
GcpJsonFormatter.setCurrentTraceId("trace_id");
logger.atInfo().log("Something I have to say");
handler.close();
String output = ostream.toString(StandardCharsets.US_ASCII);
String expected = makeJson("INFO", 86, "testSuccess_traceId", "Something I have to say");
// Remove the last two characters (}, \n) from the template and add the trace ID.
expected =
expected.substring(0, expected.length() - 2)
+ ",\"logging.googleapis.com/trace\":\"trace_id\"}\n";
assertThat(output).isEqualTo(expected);
}
@Test
@@ -85,7 +100,7 @@ class GcpJsonFormatterTest {
handler.close();
String output = ostream.toString(StandardCharsets.US_ASCII);
assertThat(output)
.isEqualTo(makeJson("ERROR", 84, "testSuccess_logLevel", "Something went terribly wrong"));
.isEqualTo(makeJson("ERROR", 99, "testSuccess_logLevel", "Something went terribly wrong"));
}
@Test
@@ -96,7 +111,7 @@ class GcpJsonFormatterTest {
String prefix =
makeJson(
"ERROR",
93,
108,
"testSuccess_withCause",
"Something went terribly wrong\\njava.lang.RuntimeException: boom!");
// Remove the last three characters (", }, \n) from the template as the actual output contains
@@ -113,7 +128,7 @@ class GcpJsonFormatterTest {
String prefix =
makeJson(
"ERROR",
110,
125,
"testSuccess_withStackTrace",
"Something is worth checking\\ncom.google.common.flogger.LogSiteStackTrace: FULL");
// Remove the last three characters (", }, \n) from the template as the actual output contains