Skip to content

Commit ed93f27

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

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

@@ -274,6 +284,7 @@
274284
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
275285
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLoggingDisabled.java</testExclude>
276286
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging1x.java</testExclude>
287+
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
277288
</testExcludes>
278289
</configuration>
279290
</plugin>
@@ -318,6 +329,7 @@
318329
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
319330
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLoggingDisabled.java</testExclude>
320331
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging.java</testExclude>
332+
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
321333
</testExcludes>
322334
</configuration>
323335
</plugin>
@@ -363,6 +375,7 @@
363375
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
364376
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging1x.java</testExclude>
365377
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging.java</testExclude>
378+
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
366379
</testExcludes>
367380
</configuration>
368381
</plugin>
@@ -388,6 +401,44 @@
388401
</plugins>
389402
</build>
390403
</profile>
404+
<profile>
405+
<id>envVarTest</id>
406+
<dependencies>
407+
<dependency>
408+
<groupId>org.slf4j</groupId>
409+
<artifactId>slf4j-api</artifactId>
410+
<scope>test</scope>
411+
</dependency>
412+
</dependencies>
413+
<build>
414+
<plugins>
415+
<plugin>
416+
<groupId>org.apache.maven.plugins</groupId>
417+
<artifactId>maven-compiler-plugin</artifactId>
418+
<configuration>
419+
<testExcludes combine.self="override" />
420+
<testIncludes>
421+
<testInclude>**/ITActionableErrorsLogging.java</testInclude>
422+
</testIncludes>
423+
</configuration>
424+
</plugin>
425+
<plugin>
426+
<groupId>org.apache.maven.plugins</groupId>
427+
<artifactId>maven-failsafe-plugin</artifactId>
428+
<configuration>
429+
<!-- Clear excludes so it runs -->
430+
<excludes combine.self="override" />
431+
<includes>
432+
<include>**/ITActionableErrorsLogging.java</include>
433+
</includes>
434+
<additionalClasspathElements>
435+
<additionalClasspathElement>${project.basedir}/src/test/slf4j-test-provider</additionalClasspathElement>
436+
</additionalClasspathElements>
437+
</configuration>
438+
</plugin>
439+
</plugins>
440+
</build>
441+
</profile>
391442
</profiles>
392443

393444
</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)