11
11
import stroom .security .api .UserIdentityFactory ;
12
12
import stroom .util .NullSafe ;
13
13
import stroom .util .io .ByteCountInputStream ;
14
+ import stroom .util .io .ByteSize ;
15
+ import stroom .util .logging .DurationTimer ;
14
16
import stroom .util .logging .LambdaLogger ;
15
17
import stroom .util .logging .LambdaLoggerFactory ;
16
18
import stroom .util .logging .LogUtil ;
33
35
import java .io .InputStream ;
34
36
import java .nio .charset .StandardCharsets ;
35
37
import java .time .Duration ;
36
- import java .time .Instant ;
37
38
import java .util .AbstractMap .SimpleEntry ;
38
39
import java .util .EnumSet ;
39
40
import java .util .Map ;
@@ -87,8 +88,6 @@ public HttpSender(final LogStream logStream,
87
88
@ Override
88
89
public void send (final AttributeMap attributeMap ,
89
90
final InputStream inputStream ) throws ForwardException {
90
- final Instant startTime = Instant .now ();
91
-
92
91
if (NullSafe .isEmptyString (attributeMap .get (StandardHeaderArguments .FEED ))) {
93
92
throw new StroomStreamException (StroomStatusCode .FEED_MUST_BE_SPECIFIED , attributeMap );
94
93
}
@@ -114,7 +113,7 @@ public void send(final AttributeMap attributeMap,
114
113
115
114
// Execute and get the response.
116
115
final ResponseStatus responseStatus = post (
117
- httpPost , startTime , attributeMap , byteCountInputStream ::getCount );
116
+ httpPost , attributeMap , byteCountInputStream ::getCount );
118
117
LOGGER .debug ("responseStatus: {}" , responseStatus );
119
118
}
120
119
@@ -208,19 +207,19 @@ private void addAuthHeaders(final BasicHttpRequest request) {
208
207
}
209
208
210
209
private ResponseStatus post (final HttpPost httpPost ,
211
- final Instant startTime ,
212
210
final AttributeMap attributeMap ,
213
211
final LongSupplier contentLengthSupplier ) throws ForwardException {
214
212
// Execute and get the response.
213
+ final DurationTimer timer = DurationTimer .start ();
215
214
try {
216
215
final ResponseStatus responseStatus = httpClient .execute (httpPost , response -> {
217
216
LOGGER .debug (() -> LogUtil .message (
218
217
"'{}' - Closing stream, response header fields:\n {}" ,
219
218
forwarderName , formatHeaderEntryListForLogging (response .getHeaders ())));
220
- return logResponseToSendLog (startTime , response , attributeMap , contentLengthSupplier );
219
+ return logResponseToSendLog (timer . get () , response , attributeMap , contentLengthSupplier );
221
220
});
222
221
223
- LOGGER .debug ("'{}' - responseStatus: {}" , forwarderName , responseStatus );
222
+ LOGGER .debug ("'{}' - responseStatus: {}, duration: {} " , forwarderName , responseStatus , timer );
224
223
225
224
// There is no point retrying with these
226
225
final StroomStatusCode stroomStatusCode = responseStatus .stroomStatusCode ;
@@ -235,10 +234,15 @@ private ResponseStatus post(final HttpPost httpPost,
235
234
// Created above so we will have already logged
236
235
throw e ;
237
236
} 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 );
239
240
// 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 );
240
244
throw ForwardException .recoverable (
241
- StroomStatusCode .UNKNOWN_ERROR , attributeMap , e . getMessage () , e );
245
+ StroomStatusCode .UNKNOWN_ERROR , attributeMap , msg , e );
242
246
}
243
247
}
244
248
@@ -275,10 +279,11 @@ private String formatAttributeMapLogging(final AttributeMap attributeMap) {
275
279
.collect (Collectors .joining ("\n " ));
276
280
}
277
281
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 ,
280
285
final AttributeMap attributeMap ) {
281
- LOGGER .debug (() -> LogUtil .message ("'{}' - {}" , forwarderName , LogUtil . exceptionMessage ( e ) , e ));
286
+ LOGGER .debug (() -> LogUtil .message ("'{}' - {}" , forwarderName , exceptionMessage , e ));
282
287
logStream .log (
283
288
SEND_LOG ,
284
289
attributeMap ,
@@ -287,11 +292,11 @@ private void logErrorToSendLog(final Instant startTime,
287
292
StroomStatusCode .UNKNOWN_ERROR ,
288
293
null ,
289
294
0 ,
290
- Duration . between ( startTime , Instant . now ()) .toMillis (),
295
+ duration .toMillis (),
291
296
LogUtil .exceptionMessage (e ));
292
297
}
293
298
294
- private ResponseStatus logResponseToSendLog (final Instant startTime ,
299
+ private ResponseStatus logResponseToSendLog (final Duration duration ,
295
300
final ClassicHttpResponse response ,
296
301
final AttributeMap attributeMap ,
297
302
final LongSupplier contentLengthSupplier ) {
@@ -319,7 +324,7 @@ private ResponseStatus logResponseToSendLog(final Instant startTime,
319
324
stroomStatusCode ,
320
325
receiptId ,
321
326
contentLength ,
322
- Duration . between ( startTime , Instant . now ()) .toMillis ());
327
+ duration .toMillis ());
323
328
324
329
return responseStatus ;
325
330
// } catch (StroomStreamException e) {
0 commit comments