Skip to content

Commit eb34622

Browse files
predic8rrayst
andauthored
fix: logmessage for body access over stream in OpenTelemetry (#1977)
* fix: logmessage for body access over stream in OpenTelemetry * refactor: minor * fix: catch network exceptions in getBodyAsStreamDecoded --------- Co-authored-by: Tobias Polley <[email protected]>
1 parent 3711af6 commit eb34622

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

core/src/main/java/com/predic8/membrane/core/interceptor/opentelemetry/OpenTelemetryInterceptor.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.predic8.membrane.core.interceptor.Outcome.*;
2929
import static com.predic8.membrane.core.interceptor.opentelemetry.HTTPTraceContextUtil.*;
3030
import static com.predic8.membrane.core.openapi.serviceproxy.OpenAPIInterceptor.*;
31+
import static com.predic8.membrane.core.util.ExceptionUtil.*;
3132
import static io.opentelemetry.api.common.AttributeKey.*;
3233
import static io.opentelemetry.api.common.Attributes.*;
3334
import static io.opentelemetry.api.trace.SpanKind.*;
@@ -75,10 +76,16 @@ public Outcome handleRequest(Exchange exc) {
7576
var span = getExchangeSpan(exc);
7677
setSpanHttpHeaderAttributes(exc.getRequest().getHeader(), span);
7778

78-
if (logBody) {
79+
if (!logBody)
80+
return CONTINUE;
81+
82+
// try is needed to catch network errors in getBodyAsStringDecoded()
83+
try {
7984
span.addEvent("Request", of(
8085
stringKey("Request Body"), exc.getRequest().getBodyAsStringDecoded()
8186
));
87+
} catch (Exception e) {
88+
log.debug("Can't log request body having problems to read stream. {}", concatMessageAndCauseMessages(e));
8289
}
8390

8491
return CONTINUE;
@@ -95,9 +102,15 @@ public Outcome handleResponse(Exchange exc) {
95102
}
96103

97104
if (logBody) {
98-
span.addEvent("Response", of(
99-
stringKey("Response Body"), exc.getResponse().getBodyAsStringDecoded()
100-
));
105+
// try is needed to catch network errors in getBodyAsStringDecoded()
106+
try {
107+
span.addEvent("Response", of(
108+
stringKey("Response Body"),
109+
exc.getResponse().getBodyAsStringDecoded()
110+
));
111+
} catch (Exception e) {
112+
log.debug("Can't log response body having problems to read stream. {}", concatMessageAndCauseMessages(e));
113+
}
101114
}
102115

103116
span.addEvent("Close Exchange").end();

0 commit comments

Comments
 (0)