Skip to content

Commit c810d33

Browse files
committed
fix(test): provide libraryMetadata for ApiTracerContext.newBuilder in tests
1 parent 84e3b25 commit c810d33

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/LoggingTracer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ private void recordActionableError(Throwable error) {
105105

106106
ErrorInfo errorInfo = ObservabilityUtils.extractErrorInfo(error);
107107
if (errorInfo != null) {
108-
logContext.put("error.type", errorInfo.getReason());
109-
logContext.put("gcp.errors.domain", errorInfo.getDomain());
108+
logContext.put(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE, errorInfo.getReason());
109+
logContext.put(ObservabilityAttributes.ERROR_DOMAIN_ATTRIBUTE, errorInfo.getDomain());
110110
for (Map.Entry<String, String> entry : errorInfo.getMetadataMap().entrySet()) {
111-
logContext.put("gcp.errors.metadata." + entry.getKey(), entry.getValue());
111+
logContext.put(
112+
ObservabilityAttributes.ERROR_METADATA_ATTRIBUTE_PREFIX + entry.getKey(),
113+
entry.getValue());
112114
}
113115
}
114116

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/LoggingTracerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType op
5454

5555
@Override
5656
public ApiTracer newTracer(ApiTracer parent, ApiTracerContext context) {
57-
return new LoggingTracer(context);
57+
return new LoggingTracer(apiTracerContext.merge(context));
5858
}
5959

6060
@Override

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityAttributes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,13 @@ public class ObservabilityAttributes {
9393

9494
/** The destination resource id of the request (e.g. projects/p/locations/l/topics/t). */
9595
public static final String DESTINATION_RESOURCE_ID_ATTRIBUTE = "gcp.resource.destination.id";
96+
97+
/** The type of error that occurred (e.g., from google.rpc.ErrorInfo.reason). */
98+
public static final String ERROR_TYPE_ATTRIBUTE = "error.type";
99+
100+
/** The domain of the error (e.g., from google.rpc.ErrorInfo.domain). */
101+
public static final String ERROR_DOMAIN_ATTRIBUTE = "gcp.errors.domain";
102+
103+
/** The prefix for error metadata (e.g., from google.rpc.ErrorInfo.metadata). */
104+
public static final String ERROR_METADATA_ATTRIBUTE_PREFIX = "gcp.errors.metadata.";
96105
}

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/logging/TestLogger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class TestLogger implements Logger, LoggingEventAware {
4848
List<String> messageList = new ArrayList<>();
4949
Level level;
5050

51+
public List<String> getMessageList() {
52+
return messageList;
53+
}
54+
5155
Map<String, Object> keyValuePairsMap = new HashMap<>();
5256

5357
private String loggerName;

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/LoggingTracerFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ void testNewTracer_WithContext_CreatesLoggingTracer() {
6363
@Test
6464
void testWithContext_ReturnsNewFactoryWithMergedContext() {
6565
LoggingTracerFactory factory = new LoggingTracerFactory();
66-
ApiTracerContext context = ApiTracerContext.newBuilder().setServerAddress("address").build();
66+
ApiTracerContext context =
67+
ApiTracerContext.empty().toBuilder().setServerAddress("address").build();
6768
ApiTracerFactory updatedFactory = factory.withContext(context);
6869

6970
assertNotNull(updatedFactory);

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/LoggingTracerTest.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030

3131
package com.google.api.gax.tracing;
3232

33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
3335
import com.google.api.gax.logging.TestLogger;
36+
import org.junit.jupiter.api.AfterEach;
3437
import org.junit.jupiter.api.BeforeEach;
3538
import org.junit.jupiter.api.Test;
3639
import org.slf4j.LoggerFactory;
@@ -42,19 +45,68 @@ class LoggingTracerTest {
4245
@BeforeEach
4346
void setUp() {
4447
testLogger = (TestLogger) LoggerFactory.getLogger(LoggingTracer.class);
48+
testLogger.getMessageList().clear();
49+
50+
try {
51+
java.lang.reflect.Field lpField = LoggingTracer.class.getDeclaredField("LOGGER_PROVIDER");
52+
lpField.setAccessible(true);
53+
Object lp = lpField.get(null);
54+
55+
java.lang.reflect.Field loggerField = lp.getClass().getDeclaredField("logger");
56+
loggerField.setAccessible(true);
57+
loggerField.set(lp, testLogger);
58+
} catch (Exception e) {
59+
throw new RuntimeException(e);
60+
}
61+
}
62+
63+
@AfterEach
64+
void tearDown() {
65+
try {
66+
java.lang.reflect.Method method =
67+
com.google.api.gax.logging.LoggingUtils.class.getDeclaredMethod(
68+
"setLoggingEnabled", boolean.class);
69+
method.setAccessible(true);
70+
method.invoke(null, false);
71+
72+
// Reset the injected logger
73+
java.lang.reflect.Field lpField = LoggingTracer.class.getDeclaredField("LOGGER_PROVIDER");
74+
lpField.setAccessible(true);
75+
Object lp = lpField.get(null);
76+
77+
java.lang.reflect.Field loggerField = lp.getClass().getDeclaredField("logger");
78+
loggerField.setAccessible(true);
79+
loggerField.set(lp, null);
80+
} catch (Exception e) {
81+
throw new RuntimeException(e);
82+
}
4583
}
4684

4785
@Test
4886
void testAttemptFailed_LogsError() {
4987
ApiTracerContext context = ApiTracerContext.empty();
5088
LoggingTracer tracer = new LoggingTracer(context);
5189

90+
// Enable logging using reflection
91+
try {
92+
// Force Slf4jUtils to be loaded while logging is still disabled
93+
// so it caches the default 'false', preventing test interference.
94+
Class.forName("com.google.api.gax.logging.Slf4jUtils");
95+
96+
java.lang.reflect.Method method =
97+
com.google.api.gax.logging.LoggingUtils.class.getDeclaredMethod(
98+
"setLoggingEnabled", boolean.class);
99+
method.setAccessible(true);
100+
method.invoke(null, true);
101+
} catch (Exception e) {
102+
throw new RuntimeException(e);
103+
}
104+
52105
// Call attemptFailed with a generic exception
53106
Exception error = new RuntimeException("generic failure");
54107
tracer.attemptFailed(error, org.threeten.bp.Duration.ZERO);
55108

56-
// To prevent failing due to disabled logging or other missing context,
57-
// we don't strictly assert the contents of the log here if the logger isn't enabled.
58-
// The main verification is that calling attemptFailed doesn't throw.
109+
assertEquals(1, testLogger.getMessageList().size());
110+
assertEquals("generic failure", testLogger.getMessageList().get(0));
59111
}
60112
}

0 commit comments

Comments
 (0)