Skip to content

Commit 0148485

Browse files
committed
feat: Enhance LoggingTracer with exception details and reuse getResponseAttributes
1 parent 7d35ea9 commit 0148485

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,14 @@ void recordActionableError(Throwable error) {
7777
}
7878

7979
Map<String, Object> logContext = new HashMap<>(apiTracerContext.getAttemptAttributes());
80+
logContext.putAll(ObservabilityUtils.getResponseAttributes(error, apiTracerContext.transport()));
8081

81-
logContext.put(
82-
ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE,
83-
ObservabilityUtils.extractStatus(error).toString());
82+
if (error != null && error.getMessage() != null) {
83+
logContext.put("exception.message", error.getMessage());
84+
}
8485

8586
ErrorInfo errorInfo = ObservabilityUtils.extractErrorInfo(error);
8687
if (errorInfo != null) {
87-
if (errorInfo.getReason() != null && !errorInfo.getReason().isEmpty()) {
88-
logContext.put(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE, errorInfo.getReason());
89-
}
9088
if (errorInfo.getDomain() != null && !errorInfo.getDomain().isEmpty()) {
9189
logContext.put(ObservabilityAttributes.ERROR_DOMAIN_ATTRIBUTE, errorInfo.getDomain());
9290
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,49 @@ void testRecordActionableError_logsErrorInfo() {
182182
attributesMap.get(ObservabilityAttributes.ERROR_METADATA_ATTRIBUTE_PREFIX + "test_key"));
183183
}
184184

185+
@Test
186+
void testRecordActionableError_logsExceptionDetails() {
187+
ApiTracerContext context = ApiTracerContext.empty();
188+
LoggingTracer tracer = new LoggingTracer(context);
189+
190+
Exception error = new RuntimeException("test error message");
191+
tracer.recordActionableError(error);
192+
193+
Map<String, ?> attributesMap = getAttributesMap();
194+
195+
assertTrue(attributesMap != null && !attributesMap.isEmpty());
196+
assertEquals(
197+
"java.lang.RuntimeException",
198+
attributesMap.get(ObservabilityAttributes.EXCEPTION_TYPE_ATTRIBUTE));
199+
assertEquals("test error message", attributesMap.get("exception.message"));
200+
}
201+
202+
@Test
203+
void testRecordActionableError_logsHttpStatus() {
204+
ApiTracerContext context =
205+
ApiTracerContext.empty().toBuilder().setTransport(ApiTracerContext.Transport.HTTP).build();
206+
LoggingTracer tracer = new LoggingTracer(context);
207+
208+
Exception error =
209+
ApiExceptionFactory.createException(
210+
"test error message",
211+
new RuntimeException("cause"),
212+
FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT),
213+
false);
214+
215+
tracer.recordActionableError(error);
216+
217+
Map<String, ?> attributesMap = getAttributesMap();
218+
219+
assertTrue(attributesMap != null && !attributesMap.isEmpty());
220+
assertEquals(
221+
"INVALID_ARGUMENT",
222+
attributesMap.get(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE));
223+
assertEquals(
224+
400L,
225+
attributesMap.get(ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE));
226+
}
227+
185228
private Map<String, ?> getAttributesMap() {
186229
if (!testLogger.getMDCMap().isEmpty()) {
187230
return testLogger.getMDCMap();

0 commit comments

Comments
 (0)