From 8432138a07c00faa11be2f41b746be7dd6ba83da Mon Sep 17 00:00:00 2001 From: Himanshu Date: Fri, 14 Apr 2017 17:34:52 -0500 Subject: [PATCH] log query in requestLog when client has disconnected and response write fails (#4169) --- .../java/io/druid/server/QueryResource.java | 89 ++++++++++++------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/server/src/main/java/io/druid/server/QueryResource.java b/server/src/main/java/io/druid/server/QueryResource.java index 2bd7d192cbb..609c7cd53bb 100644 --- a/server/src/main/java/io/druid/server/QueryResource.java +++ b/server/src/main/java/io/druid/server/QueryResource.java @@ -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.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 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); + } } } },