Skip to content

Commit c958a7f

Browse files
committed
test(gax): add automated integration test for actionable errors logging
1 parent 29b7079 commit c958a7f

File tree

5 files changed

+92
-64
lines changed

5 files changed

+92
-64
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
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>
119120
</includes>
120121
</configuration>

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

Lines changed: 51 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

@@ -292,6 +302,7 @@
292302
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
293303
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLoggingDisabled.java</testExclude>
294304
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging1x.java</testExclude>
305+
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
295306
</testExcludes>
296307
</configuration>
297308
</plugin>
@@ -336,6 +347,7 @@
336347
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
337348
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLoggingDisabled.java</testExclude>
338349
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging.java</testExclude>
350+
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
339351
</testExcludes>
340352
</configuration>
341353
</plugin>
@@ -381,6 +393,7 @@
381393
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
382394
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging1x.java</testExclude>
383395
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging.java</testExclude>
396+
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
384397
</testExcludes>
385398
</configuration>
386399
</plugin>
@@ -406,6 +419,44 @@
406419
</plugins>
407420
</build>
408421
</profile>
422+
<profile>
423+
<id>envVarTest</id>
424+
<dependencies>
425+
<dependency>
426+
<groupId>org.slf4j</groupId>
427+
<artifactId>slf4j-api</artifactId>
428+
<scope>test</scope>
429+
</dependency>
430+
</dependencies>
431+
<build>
432+
<plugins>
433+
<plugin>
434+
<groupId>org.apache.maven.plugins</groupId>
435+
<artifactId>maven-compiler-plugin</artifactId>
436+
<configuration>
437+
<testExcludes combine.self="override" />
438+
<testIncludes>
439+
<testInclude>**/ITActionableErrorsLogging.java</testInclude>
440+
</testIncludes>
441+
</configuration>
442+
</plugin>
443+
<plugin>
444+
<groupId>org.apache.maven.plugins</groupId>
445+
<artifactId>maven-failsafe-plugin</artifactId>
446+
<configuration>
447+
<!-- Clear excludes so it runs -->
448+
<excludes combine.self="override" />
449+
<includes>
450+
<include>**/ITActionableErrorsLogging.java</include>
451+
</includes>
452+
<additionalClasspathElements>
453+
<additionalClasspathElement>${project.basedir}/src/test/slf4j-test-provider</additionalClasspathElement>
454+
</additionalClasspathElements>
455+
</configuration>
456+
</plugin>
457+
</plugins>
458+
</build>
459+
</profile>
409460
</profiles>
410461

411462
</project>

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

Lines changed: 34 additions & 64 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;
@@ -37,31 +36,22 @@
3736
import com.google.showcase.v1beta1.EchoSettings;
3837
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
3938
import java.io.IOException;
40-
import java.util.List;
39+
import java.util.Map;
4140
import java.util.concurrent.TimeUnit;
4241
import org.junit.jupiter.api.AfterAll;
4342
import org.junit.jupiter.api.BeforeAll;
43+
import org.junit.jupiter.api.BeforeEach;
4444
import org.junit.jupiter.api.Test;
4545
import org.slf4j.LoggerFactory;
46-
import org.slf4j.event.KeyValuePair;
4746

4847
public class ITActionableErrorsLogging {
4948

5049
private static EchoClient grpcClient;
5150
private static EchoClient httpjsonClient;
51+
private TestLogger testLogger;
5252

5353
@BeforeAll
5454
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-
6555
grpcClient =
6656
TestClientInitializer.createGrpcEchoClientOpentelemetry(new LoggingTracerFactory());
6757
httpjsonClient =
@@ -76,24 +66,13 @@ static void destroyClients() throws InterruptedException {
7666
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
7767
httpjsonClient.awaitTermination(
7868
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-
}
8869
}
8970

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;
71+
@BeforeEach
72+
void setupTestLogger() {
73+
testLogger = (TestLogger) LoggerFactory.getLogger("com.google.api.gax.tracing.LoggingTracer");
74+
testLogger.getMessageList().clear();
75+
testLogger.getKeyValuePairsMap().clear();
9776
}
9877

9978
private EchoRequest buildErrorRequest() {
@@ -114,33 +93,26 @@ private EchoRequest buildErrorRequest() {
11493

11594
@Test
11695
void testGrpc_actionableErrorLogged() {
117-
TestAppender testAppender = setupTestLogger();
118-
11996
EchoRequest request = buildErrorRequest();
12097

121-
ApiException exception = assertThrows(ApiException.class, () -> grpcClient.echo(request));
122-
123-
assertThat(testAppender.events.size()).isAtLeast(1);
124-
ILoggingEvent loggingEvent = testAppender.events.get(testAppender.events.size() - 1);
98+
assertThrows(ApiException.class, () -> grpcClient.echo(request));
12599

126-
assertThat(loggingEvent.getLevel()).isEqualTo(Level.DEBUG);
127-
assertThat(loggingEvent.getMessage()).contains("This is a test error");
100+
assertThat(testLogger.getMessageList().size()).isAtLeast(1);
101+
String loggedMessage = testLogger.getMessageList().get(testLogger.getMessageList().size() - 1);
128102

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"));
103+
assertThat(loggedMessage).contains("This is a test error");
136104

137-
testAppender.stop();
105+
Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
106+
assertThat(kvps).containsEntry("rpc.system.name", "grpc");
107+
assertThat(kvps).containsEntry("rpc.method", "google.showcase.v1beta1.Echo/Echo");
108+
assertThat(kvps).containsEntry("rpc.response.status_code", "INVALID_ARGUMENT");
109+
assertThat(kvps).containsEntry("error.type", "TEST_REASON");
110+
assertThat(kvps).containsEntry("gcp.errors.domain", "test.googleapis.com");
111+
assertThat(kvps).containsEntry("gcp.errors.metadata.test_metadata", "test_value");
138112
}
139113

140114
@Test
141115
void testHttpJson_actionableErrorLogged() throws Exception {
142-
TestAppender testAppender = setupTestLogger();
143-
144116
// The gapic-showcase server currently returns text/plain for failEchoWithDetails instead of
145117
// JSON.
146118
// Additionally, sending an ErrorInfo in a request over REST fails serialization.
@@ -199,26 +171,24 @@ public LowLevelHttpResponse execute() throws IOException {
199171

200172
EchoRequest request = EchoRequest.newBuilder().build();
201173

202-
ApiException exception =
203-
assertThrows(ApiException.class, () -> mockHttpJsonClient.echo(request));
174+
assertThrows(ApiException.class, () -> mockHttpJsonClient.echo(request));
204175

205-
assertThat(testAppender.events.size()).isAtLeast(1);
206-
ILoggingEvent loggingEvent = testAppender.events.get(testAppender.events.size() - 1);
176+
assertThat(testLogger.getMessageList().size()).isAtLeast(1);
177+
String loggedMessage = testLogger.getMessageList().get(testLogger.getMessageList().size() - 1);
207178

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

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"));
181+
Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
182+
assertThat(kvps).containsEntry("rpc.system.name", "http");
183+
assertThat(kvps).containsEntry("http.request.method", "POST");
184+
assertThat(kvps).containsEntry("url.template", "v1beta1/echo:echo");
185+
assertThat(kvps).containsEntry("rpc.response.status_code", "ABORTED");
186+
assertThat(kvps).containsEntry("error.type", "mock_error_reason");
187+
assertThat(kvps).containsEntry("gcp.errors.domain", "mock.googleapis.com");
188+
assertThat(kvps).containsEntry("gcp.errors.metadata.mock_key", "mock_value");
220189

221190
mockHttpJsonClient.close();
222-
testAppender.stop();
191+
mockHttpJsonClient.awaitTermination(
192+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
223193
}
224194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.google.api.gax.logging.TestServiceProvider

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<type>pom</type>
3636
<scope>import</scope>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-api</artifactId>
41+
<version>2.0.16</version>
42+
</dependency>
3843
<dependency>
3944
<groupId>com.google.api.grpc</groupId>
4045
<artifactId>proto-gapic-showcase-v1beta1</artifactId>

0 commit comments

Comments
 (0)