Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk-platform-java/gax-java/gax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<include>com/google/api/gax/rpc/testing/**</include>
<include>com/google/api/gax/rpc/mtls/**</include>
<include>com/google/api/gax/util/**</include>
<include>com/google/api/gax/logging/**</include>
<include>**/native-image.properties</include>
</includes>
</configuration>
Expand Down
58 changes: 58 additions & 0 deletions sdk-platform-java/java-showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
<excludes>
<exclude>**/ITActionableErrorsLogging.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -292,6 +302,7 @@
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLoggingDisabled.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging1x.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
</testExcludes>
</configuration>
</plugin>
Expand Down Expand Up @@ -336,6 +347,7 @@
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLoggingDisabled.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
</testExcludes>
</configuration>
</plugin>
Expand Down Expand Up @@ -381,6 +393,7 @@
<testExclude>**/com/google/showcase/v1beta1/it/*.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging1x.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITLogging.java</testExclude>
<testExclude>**/com/google/showcase/v1beta1/it/logging/ITActionableErrorsLogging.java</testExclude>
</testExcludes>
</configuration>
</plugin>
Expand All @@ -406,6 +419,51 @@
</plugins>
</build>
</profile>
<profile>
<id>envVarTest</id>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<testExcludes combine.self="override" />
<testIncludes>
<testInclude>**/ITActionableErrorsLogging.java</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<!-- Clear excludes so it runs -->
<excludes combine.self="override" />
<includes>
<include>**/ITActionableErrorsLogging.java</include>
</includes>
<additionalClasspathElements>
<additionalClasspathElement>${project.basedir}/src/test/slf4j-test-provider</additionalClasspathElement>
</additionalClasspathElements>
<classpathDependencyExcludes>
<classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
<classpathDependencyExclude>ch.qos.logback:logback-core</classpathDependencyExclude>
</classpathDependencyExcludes>
<environmentVariables>
<GOOGLE_SDK_JAVA_LOGGING>true</GOOGLE_SDK_JAVA_LOGGING>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.showcase.v1beta1.it.logging;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.logging.TestLogger;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.tracing.LoggingTracerFactory;
import com.google.protobuf.Any;
import com.google.rpc.ErrorInfo;
import com.google.rpc.Status;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoSettings;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

public class ITActionableErrorsLogging {

private static EchoClient grpcClient;
private static EchoClient httpjsonClient;
private TestLogger testLogger;

@BeforeAll
static void createClients() throws Exception {
grpcClient =
TestClientInitializer.createGrpcEchoClientOpentelemetry(new LoggingTracerFactory());
httpjsonClient =
TestClientInitializer.createHttpJsonEchoClientOpentelemetry(new LoggingTracerFactory());
}

@AfterAll
static void destroyClients() throws InterruptedException {
grpcClient.close();
httpjsonClient.close();

grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpjsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@BeforeEach
void setupTestLogger() {
testLogger = (TestLogger) LoggerFactory.getLogger("com.google.api.gax.tracing.LoggingTracer");
testLogger.getMessageList().clear();
testLogger.getKeyValuePairsMap().clear();
}

private EchoRequest buildErrorRequest() {
ErrorInfo errorInfo =
ErrorInfo.newBuilder()
.setReason("TEST_REASON")
.setDomain("test.googleapis.com")
.putMetadata("test_metadata", "test_value")
.build();
Status status =
Status.newBuilder()
.setCode(3) // INVALID_ARGUMENT
.setMessage("This is a test error")
.addDetails(Any.pack(errorInfo))
.build();
return EchoRequest.newBuilder().setError(status).build();
}

@Test
void testHttpJson_logEmittedForLowLevelRequestFailure() throws Exception {
MockHttpTransport mockTransport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(409); // ABORTED
response.setContentType("application/json");
String jsonError =
"{\n"
+ " \"error\": {\n"
+ " \"code\": 409,\n"
+ " \"message\": \"This is a mock JSON error generated by the server\",\n"
+ " \"status\": \"ABORTED\",\n"
+ " \"details\": [\n"
+ " {\n"
+ " \"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\",\n"
+ " \"reason\": \"mock_error_reason\",\n"
+ " \"domain\": \"mock.googleapis.com\",\n"
+ " \"metadata\": {\"mock_key\": \"mock_value\"}\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
response.setContent(jsonError);
return response;
}
};
}
};

EchoSettings httpJsonEchoSettings =
EchoSettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(mockTransport)
.setEndpoint(TestClientInitializer.DEFAULT_HTTPJSON_ENDPOINT)
.build())
.build();

com.google.showcase.v1beta1.stub.EchoStubSettings echoStubSettings =
(com.google.showcase.v1beta1.stub.EchoStubSettings)
httpJsonEchoSettings.getStubSettings().toBuilder()
.setTracerFactory(new LoggingTracerFactory())
.build();
com.google.showcase.v1beta1.stub.EchoStub stub = echoStubSettings.createStub();
EchoClient mockHttpJsonClient = EchoClient.create(stub);

EchoRequest request = EchoRequest.newBuilder().build();
assertThrows(ApiException.class, () -> mockHttpJsonClient.echo(request));

assertThat(testLogger.getMessageList().size()).isAtLeast(1);
String loggedMessage = testLogger.getMessageList().get(testLogger.getMessageList().size() - 1);

assertThat(loggedMessage).contains("This is a mock JSON error generated by the server");

Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
assertThat(kvps).containsEntry("rpc.system.name", "http");
assertThat(kvps).containsEntry("http.request.method", "POST");
assertThat(kvps).containsEntry("url.template", "v1beta1/echo:echo");
assertThat(kvps).containsEntry("rpc.response.status_code", "ABORTED");
assertThat(kvps).containsEntry("error.type", "mock_error_reason");
assertThat(kvps).containsEntry("gcp.errors.domain", "mock.googleapis.com");
assertThat(kvps).containsEntry("gcp.errors.metadata.mock_key", "mock_value");

mockHttpJsonClient.close();
mockHttpJsonClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
void testHttpJson_noLogEmittedForSuccess() {
EchoRequest request = EchoRequest.newBuilder().setContent("Success").build();
httpjsonClient.echo(request);
assertThat(testLogger.getMessageList().size()).isEqualTo(0);
}

@Test
void testHttpJson_clientLevelFailureAttributes() throws Exception {
com.google.showcase.v1beta1.stub.EchoStubSettings.Builder stubSettingsBuilder =
com.google.showcase.v1beta1.stub.EchoStubSettings.newHttpJsonBuilder();
stubSettingsBuilder.echoSettings().setRetrySettings(
com.google.api.gax.retrying.RetrySettings.newBuilder()
.setInitialRpcTimeoutDuration(java.time.Duration.ofMillis(0))
.setTotalTimeoutDuration(java.time.Duration.ofMillis(0))
.setMaxAttempts(1)
.build());
stubSettingsBuilder.setTracerFactory(new LoggingTracerFactory());
stubSettingsBuilder.setEndpoint(TestClientInitializer.DEFAULT_HTTPJSON_ENDPOINT);

try (com.google.showcase.v1beta1.stub.EchoStub stub = stubSettingsBuilder.build().createStub();
EchoClient client = EchoClient.create(stub)) {
assertThrows(ApiException.class, () -> client.echo(EchoRequest.newBuilder().build()));
assertThat(testLogger.getMessageList().size()).isAtLeast(1);
Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
assertThat(kvps).containsEntry("rpc.system.name", "http");
}
}

@Test
void testGrpc_logEmittedForLowLevelRequestFailure() {
EchoRequest request = buildErrorRequest();
assertThrows(ApiException.class, () -> grpcClient.echo(request));

assertThat(testLogger.getMessageList().size()).isAtLeast(1);
String loggedMessage = testLogger.getMessageList().get(testLogger.getMessageList().size() - 1);
assertThat(loggedMessage).contains("This is a test error");

Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
assertThat(kvps).containsEntry("rpc.system.name", "grpc");
assertThat(kvps).containsEntry("rpc.method", "google.showcase.v1beta1.Echo/Echo");
assertThat(kvps).containsEntry("rpc.response.status_code", "INVALID_ARGUMENT");
assertThat(kvps).containsEntry("error.type", "TEST_REASON");
assertThat(kvps).containsEntry("gcp.errors.domain", "test.googleapis.com");
assertThat(kvps).containsEntry("gcp.errors.metadata.test_metadata", "test_value");
}

@Test
void testGrpc_noLogEmittedForSuccess() {
EchoRequest request = EchoRequest.newBuilder().setContent("Success").build();
grpcClient.echo(request);
assertThat(testLogger.getMessageList().size()).isEqualTo(0);
}

@Test
void testGrpc_clientLevelFailureAttributes() throws Exception {
com.google.showcase.v1beta1.stub.EchoStubSettings.Builder stubSettingsBuilder =
com.google.showcase.v1beta1.stub.EchoStubSettings.newBuilder();
stubSettingsBuilder.echoSettings().setRetrySettings(
com.google.api.gax.retrying.RetrySettings.newBuilder()
.setInitialRpcTimeoutDuration(java.time.Duration.ofMillis(0))
.setTotalTimeoutDuration(java.time.Duration.ofMillis(0))
.setMaxAttempts(1)
.build());
stubSettingsBuilder.setTracerFactory(new LoggingTracerFactory());
stubSettingsBuilder.setEndpoint(TestClientInitializer.DEFAULT_GRPC_ENDPOINT);

try (com.google.showcase.v1beta1.stub.EchoStub stub = stubSettingsBuilder.build().createStub();
EchoClient client = EchoClient.create(stub)) {
assertThrows(ApiException.class, () -> client.echo(EchoRequest.newBuilder().build()));
assertThat(testLogger.getMessageList().size()).isAtLeast(1);
Map<String, Object> kvps = testLogger.getKeyValuePairsMap();
assertThat(kvps).containsEntry("rpc.system.name", "grpc");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.google.api.gax.logging.TestServiceProvider
5 changes: 5 additions & 0 deletions sdk-platform-java/java-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-gapic-showcase-v1beta1</artifactId>
Expand Down
Loading