Skip to content

Commit 94a62cb

Browse files
committed
test(gax): add automated integration test for actionable errors logging
1 parent b6c429e commit 94a62cb

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

sdk-platform-java/gax-java/gax/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@
115115
<include>com/google/api/gax/rpc/testing/**</include>
116116
<include>com/google/api/gax/rpc/mtls/**</include>
117117
<include>com/google/api/gax/util/**</include>
118+
<include>com/google/api/gax/logging/**</include>
118119
<include>**/native-image.properties</include>
120+
<include>META-INF/services/**</include>
119121
</includes>
120122
</configuration>
121123
</execution>

sdk-platform-java/java-showcase/gapic-showcase/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@
7474
</execution>
7575
</executions>
7676
</plugin>
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-failsafe-plugin</artifactId>
80+
<configuration>
81+
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
82+
<excludes>
83+
<exclude>**/ITActionableErrorsLogging.java</exclude>
84+
</excludes>
85+
</configuration>
86+
</plugin>
7787
</plugins>
7888
</build>
7989

@@ -388,6 +398,42 @@
388398
</plugins>
389399
</build>
390400
</profile>
401+
<profile>
402+
<id>envVarTest</id>
403+
<dependencies>
404+
<dependency>
405+
<groupId>org.slf4j</groupId>
406+
<artifactId>slf4j-api</artifactId>
407+
<version>2.0.16</version>
408+
<scope>test</scope>
409+
</dependency>
410+
</dependencies>
411+
<build>
412+
<plugins>
413+
<plugin>
414+
<groupId>org.apache.maven.plugins</groupId>
415+
<artifactId>maven-compiler-plugin</artifactId>
416+
<configuration>
417+
<testExcludes combine.self="override" />
418+
<testIncludes>
419+
<testInclude>**/ITActionableErrorsLogging.java</testInclude>
420+
</testIncludes>
421+
</configuration>
422+
</plugin>
423+
<plugin>
424+
<groupId>org.apache.maven.plugins</groupId>
425+
<artifactId>maven-failsafe-plugin</artifactId>
426+
<configuration>
427+
<!-- Clear excludes so it runs -->
428+
<excludes combine.self="override" />
429+
<includes>
430+
<include>**/ITActionableErrorsLogging.java</include>
431+
</includes>
432+
</configuration>
433+
</plugin>
434+
</plugins>
435+
</build>
436+
</profile>
391437
</profiles>
392438

393439
</project>

sdk-platform-java/java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
2121

22-
import ch.qos.logback.classic.Level;
23-
import ch.qos.logback.classic.spi.ILoggingEvent;
2422
import com.google.api.client.http.LowLevelHttpRequest;
2523
import com.google.api.client.http.LowLevelHttpResponse;
2624
import com.google.api.client.testing.http.MockHttpTransport;
2725
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
2826
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
2927
import com.google.api.gax.core.NoCredentialsProvider;
28+
import com.google.api.gax.logging.TestLogger;
3029
import com.google.api.gax.rpc.ApiException;
3130
import com.google.api.gax.tracing.LoggingTracerFactory;
3231
import com.google.protobuf.Any;
@@ -38,30 +37,22 @@
3837
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
3938
import java.io.IOException;
4039
import java.util.List;
40+
import java.util.Map;
4141
import java.util.concurrent.TimeUnit;
4242
import org.junit.jupiter.api.AfterAll;
4343
import org.junit.jupiter.api.BeforeAll;
44+
import org.junit.jupiter.api.BeforeEach;
4445
import org.junit.jupiter.api.Test;
4546
import org.slf4j.LoggerFactory;
46-
import org.slf4j.event.KeyValuePair;
4747

4848
public class ITActionableErrorsLogging {
4949

5050
private static EchoClient grpcClient;
5151
private static EchoClient httpjsonClient;
52+
private TestLogger testLogger;
5253

5354
@BeforeAll
5455
static void createClients() throws Exception {
55-
try {
56-
java.lang.reflect.Method m =
57-
com.google.api.gax.logging.LoggingUtils.class.getDeclaredMethod(
58-
"setLoggingEnabled", boolean.class);
59-
m.setAccessible(true);
60-
m.invoke(null, true);
61-
} catch (Exception e) {
62-
throw new RuntimeException(e);
63-
}
64-
6556
grpcClient =
6657
TestClientInitializer.createGrpcEchoClientOpentelemetry(new LoggingTracerFactory());
6758
httpjsonClient =
@@ -76,24 +67,13 @@ static void destroyClients() throws InterruptedException {
7667
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
7768
httpjsonClient.awaitTermination(
7869
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
79-
try {
80-
java.lang.reflect.Method m =
81-
com.google.api.gax.logging.LoggingUtils.class.getDeclaredMethod(
82-
"setLoggingEnabled", boolean.class);
83-
m.setAccessible(true);
84-
m.invoke(null, false);
85-
} catch (Exception e) {
86-
throw new RuntimeException(e);
87-
}
8870
}
8971

90-
private TestAppender setupTestLogger() {
91-
TestAppender testAppender = new TestAppender();
92-
testAppender.start();
93-
org.slf4j.Logger logger = LoggerFactory.getLogger("com.google.api.gax.tracing.LoggingTracer");
94-
((ch.qos.logback.classic.Logger) logger).setLevel(Level.DEBUG);
95-
((ch.qos.logback.classic.Logger) logger).addAppender(testAppender);
96-
return testAppender;
72+
@BeforeEach
73+
void setupTestLogger() {
74+
testLogger = (TestLogger) LoggerFactory.getLogger("com.google.api.gax.tracing.LoggingTracer");
75+
testLogger.getMessageList().clear();
76+
testLogger.getKeyValuePairsMap().clear();
9777
}
9878

9979
private EchoRequest buildErrorRequest() {
@@ -114,33 +94,26 @@ private EchoRequest buildErrorRequest() {
11494

11595
@Test
11696
void testGrpc_actionableErrorLogged() {
117-
TestAppender testAppender = setupTestLogger();
118-
11997
EchoRequest request = buildErrorRequest();
12098

12199
ApiException exception = assertThrows(ApiException.class, () -> grpcClient.echo(request));
122100

123-
assertThat(testAppender.events.size()).isAtLeast(1);
124-
ILoggingEvent loggingEvent = testAppender.events.get(testAppender.events.size() - 1);
101+
assertThat(testLogger.getMessageList().size()).isAtLeast(1);
102+
String loggedMessage = testLogger.getMessageList().get(testLogger.getMessageList().size() - 1);
125103

126-
assertThat(loggingEvent.getLevel()).isEqualTo(Level.DEBUG);
127-
assertThat(loggingEvent.getMessage()).contains("This is a test error");
104+
assertThat(loggedMessage).contains("This is a test error");
128105

129-
List<KeyValuePair> kvps = loggingEvent.getKeyValuePairs();
130-
assertThat(kvps).contains(new KeyValuePair("rpc.system.name", "grpc"));
131-
assertThat(kvps).contains(new KeyValuePair("rpc.method", "google.showcase.v1beta1.Echo/Echo"));
132-
assertThat(kvps).contains(new KeyValuePair("rpc.response.status_code", "INVALID_ARGUMENT"));
133-
assertThat(kvps).contains(new KeyValuePair("error.type", "TEST_REASON"));
134-
assertThat(kvps).contains(new KeyValuePair("gcp.errors.domain", "test.googleapis.com"));
135-
assertThat(kvps).contains(new KeyValuePair("gcp.errors.metadata.test_metadata", "test_value"));
136-
137-
testAppender.stop();
106+
Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
107+
assertThat(kvps).containsEntry("rpc.system.name", "grpc");
108+
assertThat(kvps).containsEntry("rpc.method", "google.showcase.v1beta1.Echo/Echo");
109+
assertThat(kvps).containsEntry("rpc.response.status_code", "INVALID_ARGUMENT");
110+
assertThat(kvps).containsEntry("error.type", "TEST_REASON");
111+
assertThat(kvps).containsEntry("gcp.errors.domain", "test.googleapis.com");
112+
assertThat(kvps).containsEntry("gcp.errors.metadata.test_metadata", "test_value");
138113
}
139114

140115
@Test
141116
void testHttpJson_actionableErrorLogged() throws Exception {
142-
TestAppender testAppender = setupTestLogger();
143-
144117
// The gapic-showcase server currently returns text/plain for failEchoWithDetails instead of
145118
// JSON.
146119
// Additionally, sending an ErrorInfo in a request over REST fails serialization.
@@ -202,23 +175,20 @@ public LowLevelHttpResponse execute() throws IOException {
202175
ApiException exception =
203176
assertThrows(ApiException.class, () -> mockHttpJsonClient.echo(request));
204177

205-
assertThat(testAppender.events.size()).isAtLeast(1);
206-
ILoggingEvent loggingEvent = testAppender.events.get(testAppender.events.size() - 1);
178+
assertThat(testLogger.getMessageList().size()).isAtLeast(1);
179+
String loggedMessage = testLogger.getMessageList().get(testLogger.getMessageList().size() - 1);
207180

208-
assertThat(loggingEvent.getLevel()).isEqualTo(Level.DEBUG);
209-
assertThat(loggingEvent.getMessage())
210-
.contains("This is a mock JSON error generated by the server");
181+
assertThat(loggedMessage).contains("This is a mock JSON error generated by the server");
211182

212-
List<KeyValuePair> kvps = loggingEvent.getKeyValuePairs();
213-
assertThat(kvps).contains(new KeyValuePair("rpc.system.name", "http"));
214-
assertThat(kvps).contains(new KeyValuePair("http.request.method", "POST"));
215-
assertThat(kvps).contains(new KeyValuePair("url.template", "v1beta1/echo:echo"));
216-
assertThat(kvps).contains(new KeyValuePair("rpc.response.status_code", "ABORTED"));
217-
assertThat(kvps).contains(new KeyValuePair("error.type", "mock_error_reason"));
218-
assertThat(kvps).contains(new KeyValuePair("gcp.errors.domain", "mock.googleapis.com"));
219-
assertThat(kvps).contains(new KeyValuePair("gcp.errors.metadata.mock_key", "mock_value"));
183+
Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
184+
assertThat(kvps).containsEntry("rpc.system.name", "http");
185+
assertThat(kvps).containsEntry("http.request.method", "POST");
186+
assertThat(kvps).containsEntry("url.template", "v1beta1/echo:echo");
187+
assertThat(kvps).containsEntry("rpc.response.status_code", "ABORTED");
188+
assertThat(kvps).containsEntry("error.type", "mock_error_reason");
189+
assertThat(kvps).containsEntry("gcp.errors.domain", "mock.googleapis.com");
190+
assertThat(kvps).containsEntry("gcp.errors.metadata.mock_key", "mock_value");
220191

221192
mockHttpJsonClient.close();
222-
testAppender.stop();
223193
}
224194
}

0 commit comments

Comments
 (0)