Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ void recordActionableError(Throwable error) {
}

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

logContext.put(
ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE,
ObservabilityUtils.extractStatus(error).toString());
if (error != null && error.getMessage() != null) {
Comment thread
blakeli0 marked this conversation as resolved.
Outdated
logContext.put("exception.message", error.getMessage());
}

ErrorInfo errorInfo = ObservabilityUtils.extractErrorInfo(error);
if (errorInfo != null) {
if (errorInfo.getReason() != null && !errorInfo.getReason().isEmpty()) {
logContext.put(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE, errorInfo.getReason());
}
if (errorInfo.getDomain() != null && !errorInfo.getDomain().isEmpty()) {
logContext.put(ObservabilityAttributes.ERROR_DOMAIN_ATTRIBUTE, errorInfo.getDomain());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ void testRecordActionableError_logsErrorInfo() {
attributesMap.get(ObservabilityAttributes.ERROR_METADATA_ATTRIBUTE_PREFIX + "test_key"));
}

@Test
void testRecordActionableError_logsExceptionDetails() {
ApiTracerContext context = ApiTracerContext.empty();
LoggingTracer tracer = new LoggingTracer(context);

Exception error = new RuntimeException("test error message");
tracer.recordActionableError(error);

Map<String, ?> attributesMap = getAttributesMap();

assertTrue(attributesMap != null && !attributesMap.isEmpty());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can this be broken into two assert: assertNotNull(attributeMap) and assertFalse(attributeMaps.isEmpty()).

IIRC, the error message for assertTrue isn't as clear when indicating which condition failed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I was following the existing pattern but I don't think these assertions are needed anymore. Because attributeMap can never be null and we are already asserting a key later so not empty is also not necessary.
Removed this assertion altogether.

assertEquals(
"java.lang.RuntimeException",
attributesMap.get(ObservabilityAttributes.EXCEPTION_TYPE_ATTRIBUTE));
assertEquals("test error message", attributesMap.get("exception.message"));
}

@Test
void testRecordActionableError_logsHttpStatus() {
ApiTracerContext context =
ApiTracerContext.empty().toBuilder().setTransport(ApiTracerContext.Transport.HTTP).build();
LoggingTracer tracer = new LoggingTracer(context);

Exception error =
ApiExceptionFactory.createException(
"test error message",
new RuntimeException("cause"),
FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT),
false);

tracer.recordActionableError(error);

Map<String, ?> attributesMap = getAttributesMap();

assertTrue(attributesMap != null && !attributesMap.isEmpty());
assertEquals(
"INVALID_ARGUMENT",
attributesMap.get(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE));
assertEquals(400L, attributesMap.get(ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE));
}

private Map<String, ?> getAttributesMap() {
if (!testLogger.getMDCMap().isEmpty()) {
return testLogger.getMDCMap();
Expand Down
Loading