Skip to content

Commit 39abc06

Browse files
committed
Merge branch '7.8' of github.com-at055612:gchq/stroom into 7.8
2 parents 709da2e + 31ab964 commit 39abc06

File tree

4 files changed

+85
-16
lines changed

4 files changed

+85
-16
lines changed

stroom-proxy/stroom-proxy-app/src/main/java/stroom/proxy/app/handler/HttpSender.java

+20-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import stroom.security.api.UserIdentityFactory;
1212
import stroom.util.NullSafe;
1313
import stroom.util.io.ByteCountInputStream;
14+
import stroom.util.io.ByteSize;
15+
import stroom.util.logging.DurationTimer;
1416
import stroom.util.logging.LambdaLogger;
1517
import stroom.util.logging.LambdaLoggerFactory;
1618
import stroom.util.logging.LogUtil;
@@ -33,7 +35,6 @@
3335
import java.io.InputStream;
3436
import java.nio.charset.StandardCharsets;
3537
import java.time.Duration;
36-
import java.time.Instant;
3738
import java.util.AbstractMap.SimpleEntry;
3839
import java.util.EnumSet;
3940
import java.util.Map;
@@ -87,8 +88,6 @@ public HttpSender(final LogStream logStream,
8788
@Override
8889
public void send(final AttributeMap attributeMap,
8990
final InputStream inputStream) throws ForwardException {
90-
final Instant startTime = Instant.now();
91-
9291
if (NullSafe.isEmptyString(attributeMap.get(StandardHeaderArguments.FEED))) {
9392
throw new StroomStreamException(StroomStatusCode.FEED_MUST_BE_SPECIFIED, attributeMap);
9493
}
@@ -114,7 +113,7 @@ public void send(final AttributeMap attributeMap,
114113

115114
// Execute and get the response.
116115
final ResponseStatus responseStatus = post(
117-
httpPost, startTime, attributeMap, byteCountInputStream::getCount);
116+
httpPost, attributeMap, byteCountInputStream::getCount);
118117
LOGGER.debug("responseStatus: {}", responseStatus);
119118
}
120119

@@ -208,19 +207,19 @@ private void addAuthHeaders(final BasicHttpRequest request) {
208207
}
209208

210209
private ResponseStatus post(final HttpPost httpPost,
211-
final Instant startTime,
212210
final AttributeMap attributeMap,
213211
final LongSupplier contentLengthSupplier) throws ForwardException {
214212
// Execute and get the response.
213+
final DurationTimer timer = DurationTimer.start();
215214
try {
216215
final ResponseStatus responseStatus = httpClient.execute(httpPost, response -> {
217216
LOGGER.debug(() -> LogUtil.message(
218217
"'{}' - Closing stream, response header fields:\n{}",
219218
forwarderName, formatHeaderEntryListForLogging(response.getHeaders())));
220-
return logResponseToSendLog(startTime, response, attributeMap, contentLengthSupplier);
219+
return logResponseToSendLog(timer.get(), response, attributeMap, contentLengthSupplier);
221220
});
222221

223-
LOGGER.debug("'{}' - responseStatus: {}", forwarderName, responseStatus);
222+
LOGGER.debug("'{}' - responseStatus: {}, duration: {}", forwarderName, responseStatus, timer);
224223

225224
// There is no point retrying with these
226225
final StroomStatusCode stroomStatusCode = responseStatus.stroomStatusCode;
@@ -235,10 +234,15 @@ private ResponseStatus post(final HttpPost httpPost,
235234
// Created above so we will have already logged
236235
throw e;
237236
} catch (final Exception e) {
238-
logErrorToSendLog(startTime, e, attributeMap);
237+
final Duration duration = timer.get();
238+
final long byteCount = LogUtil.swallowExceptions(contentLengthSupplier)
239+
.orElse(0);
239240
// Have to assume that any exception is recoverable
241+
final String msg = LogUtil.message("Error during HTTP POST, data sent: {}, duration: {}, error: {}",
242+
ByteSize.ofBytes(byteCount), duration, LogUtil.exceptionMessage(e));
243+
logErrorToSendLog(duration, e, msg, attributeMap);
240244
throw ForwardException.recoverable(
241-
StroomStatusCode.UNKNOWN_ERROR, attributeMap, e.getMessage(), e);
245+
StroomStatusCode.UNKNOWN_ERROR, attributeMap, msg, e);
242246
}
243247
}
244248

@@ -275,10 +279,11 @@ private String formatAttributeMapLogging(final AttributeMap attributeMap) {
275279
.collect(Collectors.joining("\n"));
276280
}
277281

278-
private void logErrorToSendLog(final Instant startTime,
279-
final Throwable e,
282+
private void logErrorToSendLog(final Duration duration,
283+
final Exception e,
284+
final String exceptionMessage,
280285
final AttributeMap attributeMap) {
281-
LOGGER.debug(() -> LogUtil.message("'{}' - {}", forwarderName, LogUtil.exceptionMessage(e), e));
286+
LOGGER.debug(() -> LogUtil.message("'{}' - {}", forwarderName, exceptionMessage, e));
282287
logStream.log(
283288
SEND_LOG,
284289
attributeMap,
@@ -287,11 +292,11 @@ private void logErrorToSendLog(final Instant startTime,
287292
StroomStatusCode.UNKNOWN_ERROR,
288293
null,
289294
0,
290-
Duration.between(startTime, Instant.now()).toMillis(),
295+
duration.toMillis(),
291296
LogUtil.exceptionMessage(e));
292297
}
293298

294-
private ResponseStatus logResponseToSendLog(final Instant startTime,
299+
private ResponseStatus logResponseToSendLog(final Duration duration,
295300
final ClassicHttpResponse response,
296301
final AttributeMap attributeMap,
297302
final LongSupplier contentLengthSupplier) {
@@ -319,7 +324,7 @@ private ResponseStatus logResponseToSendLog(final Instant startTime,
319324
stroomStatusCode,
320325
receiptId,
321326
contentLength,
322-
Duration.between(startTime, Instant.now()).toMillis());
327+
duration.toMillis());
323328

324329
return responseStatus;
325330
// } catch (StroomStreamException e) {

stroom-proxy/stroom-proxy-app/src/main/java/stroom/proxy/app/handler/RetryingForwardDestination.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private boolean forwardDir(final Path dir) {
330330
+ "' to " + getDestinationType() + " forward destination '"
331331
+ destinationName + "' "
332332
+ "(attempts: " + attempts + ", retryAge: " + retryAge + "): "
333-
+ LogUtil.exceptionMessage(getCause(e)) + ". "
333+
+ e.getMessage() + ". "
334334
+ (e instanceof ForwardException fe
335335
? "Feed: '" + fe.getFeedName() + "'. HTTP code: " + fe.getHttpResponseCode() + ". "
336336
: "")

stroom-util/src/main/java/stroom/util/logging/LogUtil.java

+45
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818
import java.time.Instant;
1919
import java.util.Collection;
2020
import java.util.Objects;
21+
import java.util.Optional;
22+
import java.util.OptionalInt;
23+
import java.util.OptionalLong;
2124
import java.util.function.Consumer;
2225
import java.util.function.Function;
26+
import java.util.function.IntSupplier;
27+
import java.util.function.LongSupplier;
28+
import java.util.function.Supplier;
2329
import java.util.stream.Collectors;
2430

2531
public final class LogUtil {
@@ -417,4 +423,43 @@ public static void logStackTrace(final String message,
417423
}
418424
}
419425
}
426+
427+
/**
428+
* Return the value supplied by supplier. Any exceptions will be swallowed and logged to debug only.
429+
* Useful for getting values for logging that may throw.
430+
*/
431+
public static <T> Optional<T> swallowExceptions(final Supplier<T> supplier) {
432+
try {
433+
return Optional.ofNullable(supplier.get());
434+
} catch (Exception e) {
435+
LOGGER.debug("Error swallowed", e);
436+
return Optional.empty();
437+
}
438+
}
439+
440+
/**
441+
* Return the value supplied by supplier. Any exceptions will be swallowed and logged to debug only.
442+
* Useful for getting values for logging that may throw.
443+
*/
444+
public static OptionalLong swallowExceptions(final LongSupplier supplier) {
445+
try {
446+
return OptionalLong.of(supplier.getAsLong());
447+
} catch (Exception e) {
448+
LOGGER.debug("Error swallowed", e);
449+
return OptionalLong.empty();
450+
}
451+
}
452+
453+
/**
454+
* Return the value supplied by supplier. Any exceptions will be swallowed and logged to debug only.
455+
* Useful for getting values for logging that may throw.
456+
*/
457+
public static OptionalInt swallowExceptions(final IntSupplier supplier) {
458+
try {
459+
return OptionalInt.of(supplier.getAsInt());
460+
} catch (Exception e) {
461+
LOGGER.debug("Error swallowed", e);
462+
return OptionalInt.empty();
463+
}
464+
}
420465
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
* Improve log message when proxy HTTP forwarding fails. Now includes the time taken and the bytes sent.
2+
3+
4+
```sh
5+
# ONLY the top line will be included as a change entry in the CHANGELOG.
6+
# The entry should be in GitHub flavour markdown and should be written on a SINGLE
7+
# line with no hard breaks. You can have multiple change files for a single GitHub issue.
8+
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than
9+
# 'Fixed nasty bug'.
10+
#
11+
# Examples of acceptable entries are:
12+
#
13+
#
14+
# * Issue **123** : Fix bug with an associated GitHub issue in this repository
15+
#
16+
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository
17+
#
18+
# * Fix bug with no associated GitHub issue.
19+
```

0 commit comments

Comments
 (0)