log query in requestLog when client has disconnected and response write fails (#4169)

This commit is contained in:
Himanshu 2017-04-14 17:34:52 -05:00 committed by Nishant Bangarwa
parent 5c9198347b
commit 8432138a07
1 changed files with 55 additions and 34 deletions

View File

@ -259,48 +259,69 @@ public class QueryResource implements QueryCountStatsProvider
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException
{
boolean success = false;
String exceptionStr = "";
CountingOutputStream os = new CountingOutputStream(outputStream);
try {
// json serializer will always close the yielder
CountingOutputStream os = new CountingOutputStream(outputStream);
jsonWriter.writeValue(os, yielder);
os.flush(); // Some types of OutputStream suppress flush errors in the .close() method.
os.close();
successfulQueryCount.incrementAndGet();
final long queryTimeNs = System.nanoTime() - startNs;
QueryMetrics queryMetrics = DruidMetrics.makeRequestMetrics(
queryMetricsFactory,
theToolChest,
theQuery,
req.getRemoteAddr()
);
queryMetrics.success(true);
queryMetrics.reportQueryTime(queryTimeNs).emit(emitter);
DruidMetrics.makeRequestMetrics(
queryMetricsFactory,
theToolChest,
theQuery,
req.getRemoteAddr()
).reportQueryBytes(os.getCount()).emit(emitter);
success = true;
} catch (Exception ex) {
exceptionStr = ex.toString();
throw Throwables.propagate(ex);
} finally {
try {
if (success) {
successfulQueryCount.incrementAndGet();
} else {
failedQueryCount.incrementAndGet();
}
requestLogger.log(
new RequestLogLine(
new DateTime(TimeUnit.NANOSECONDS.toMillis(startNs)),
req.getRemoteAddr(),
theQuery,
new QueryStats(
ImmutableMap.<String, Object>of(
"query/time", TimeUnit.NANOSECONDS.toMillis(queryTimeNs),
"query/bytes", os.getCount(),
"success", true
)
)
)
);
}
finally {
Thread.currentThread().setName(currThreadName);
final long queryTimeNs = System.nanoTime() - startNs;
QueryMetrics queryMetrics = DruidMetrics.makeRequestMetrics(
queryMetricsFactory,
theToolChest,
theQuery,
req.getRemoteAddr()
);
queryMetrics.success(success);
queryMetrics.reportQueryTime(queryTimeNs).emit(emitter);
DruidMetrics.makeRequestMetrics(
queryMetricsFactory,
theToolChest,
theQuery,
req.getRemoteAddr()
).reportQueryBytes(os.getCount()).emit(emitter);
ImmutableMap.Builder<String, Object> statsMapBuilder = ImmutableMap.builder();
statsMapBuilder.put("query/time", TimeUnit.NANOSECONDS.toMillis(queryTimeNs));
statsMapBuilder.put("query/bytes", os.getCount());
statsMapBuilder.put("success", success);
if (!success) {
statsMapBuilder.put("exception", exceptionStr);
}
requestLogger.log(
new RequestLogLine(
new DateTime(TimeUnit.NANOSECONDS.toMillis(startNs)),
req.getRemoteAddr(),
theQuery,
new QueryStats(
statsMapBuilder.build()
)
)
);
} catch (Exception ex) {
log.error(ex, "Unable to log query [%s]!", theQuery);
} finally {
Thread.currentThread().setName(currThreadName);
}
}
}
},