Merge pull request #103 from metamx/request-logs

make request logs machine-readable
This commit is contained in:
cheddar 2013-03-07 13:35:48 -08:00
commit a1cd3c3905
4 changed files with 14 additions and 7 deletions

View File

@ -248,7 +248,7 @@ public abstract class QueryableNode<T extends QueryableNode> extends Registering
{ {
if (requestLogger == null) { if (requestLogger == null) {
try { try {
setRequestLogger(Initialization.makeRequestLogger(getScheduledExecutorFactory(), getProps())); setRequestLogger(Initialization.makeRequestLogger(getJsonMapper(), getScheduledExecutorFactory(), getProps()));
} }
catch (IOException e) { catch (IOException e) {
throw Throwables.propagate(e); throw Throwables.propagate(e);

View File

@ -19,6 +19,7 @@
package com.metamx.druid.http; package com.metamx.druid.http;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.metamx.common.concurrent.ScheduledExecutors; import com.metamx.common.concurrent.ScheduledExecutors;
@ -38,6 +39,7 @@ import java.util.concurrent.ScheduledExecutorService;
*/ */
public class FileRequestLogger implements RequestLogger public class FileRequestLogger implements RequestLogger
{ {
private final ObjectMapper objectMapper;
private final ScheduledExecutorService exec; private final ScheduledExecutorService exec;
private final File baseDir; private final File baseDir;
@ -46,9 +48,11 @@ public class FileRequestLogger implements RequestLogger
private volatile DateTime currentDay; private volatile DateTime currentDay;
private volatile FileWriter fileWriter; private volatile FileWriter fileWriter;
public FileRequestLogger(ScheduledExecutorService exec, File baseDir)
public FileRequestLogger(ObjectMapper objectMapper, ScheduledExecutorService exec, File baseDir)
{ {
this.exec = exec; this.exec = exec;
this.objectMapper = objectMapper;
this.baseDir = baseDir; this.baseDir = baseDir;
} }
@ -110,7 +114,7 @@ public class FileRequestLogger implements RequestLogger
{ {
synchronized (lock) { synchronized (lock) {
fileWriter.write( fileWriter.write(
String.format("%s%n", requestLogLine.getLine()) String.format("%s%n", requestLogLine.getLine(objectMapper))
); );
fileWriter.flush(); fileWriter.flush();
} }

View File

@ -19,6 +19,8 @@
package com.metamx.druid.http; package com.metamx.druid.http;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.metamx.druid.Query; import com.metamx.druid.Query;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -40,14 +42,14 @@ public class RequestLogLine
this.query = query; this.query = query;
} }
public String getLine() public String getLine(ObjectMapper objectMapper) throws JsonProcessingException
{ {
return JOINER.join( return JOINER.join(
Arrays.asList( Arrays.asList(
timestamp, timestamp,
remoteAddr, remoteAddr,
query objectMapper.writeValueAsString(query)
) )
); );
} }
} }

View File

@ -348,9 +348,10 @@ public class Initialization
return serviceProvider; return serviceProvider;
} }
public static RequestLogger makeRequestLogger(ScheduledExecutorFactory factory, Properties props) throws IOException public static RequestLogger makeRequestLogger(ObjectMapper objectMapper, ScheduledExecutorFactory factory, Properties props) throws IOException
{ {
return new FileRequestLogger( return new FileRequestLogger(
objectMapper,
factory.create(1, "RequestLogger-%s"), factory.create(1, "RequestLogger-%s"),
new File(PropUtils.getProperty(props, "druid.request.logging.dir")) new File(PropUtils.getProperty(props, "druid.request.logging.dir"))
); );