Replace timestamp initialization 'now' to an explicit timestamp. (elastic/x-pack-elasticsearch#1383)

fixes build errors. Still ensures that the timestamp is set to 'now' if the parsed logfile misses it.

Original commit: elastic/x-pack-elasticsearch@cf60e8d76b
This commit is contained in:
Hendrik Muhs 2017-05-10 16:17:30 +02:00 committed by GitHub
parent d34192ff6f
commit c43ae014b4
2 changed files with 28 additions and 6 deletions

View File

@ -37,7 +37,7 @@ public class CppLogMessage extends ToXContentToBytes implements Writeable {
public static final ParseField LINE_FIELD = new ParseField("line");
public static final ObjectParser<CppLogMessage, Void> PARSER = new ObjectParser<>(
LOGGER_FIELD.getPreferredName(), CppLogMessage::new);
LOGGER_FIELD.getPreferredName(), () -> new CppLogMessage(Instant.now()));
static {
PARSER.declareString(CppLogMessage::setLogger, LOGGER_FIELD);
@ -68,8 +68,8 @@ public class CppLogMessage extends ToXContentToBytes implements Writeable {
private String file = "";
private long line = 0;
public CppLogMessage() {
timestamp = Instant.now();
public CppLogMessage(Instant timestamp) {
this.timestamp = timestamp;
}
public CppLogMessage(StreamInput in) throws IOException {

View File

@ -6,15 +6,22 @@
package org.elasticsearch.xpack.ml.job.process.logging;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.ml.support.AbstractSerializingTestCase;
import java.io.IOException;
import java.time.Instant;
public class CppLogMessageTests extends AbstractSerializingTestCase<CppLogMessage> {
public void testDefaultConstructor() {
CppLogMessage msg = new CppLogMessage();
CppLogMessage msg = new CppLogMessage(Instant.ofEpochSecond(1494422876L));
assertEquals("", msg.getLogger());
assertTrue(msg.getTimestamp().toString(), msg.getTimestamp().toEpochMilli() > 0);
assertEquals(Instant.ofEpochSecond(1494422876L), msg.getTimestamp());
assertEquals("", msg.getLevel());
assertEquals(0, msg.getPid());
assertEquals("", msg.getThread());
@ -25,9 +32,24 @@ public class CppLogMessageTests extends AbstractSerializingTestCase<CppLogMessag
assertEquals(0, msg.getLine());
}
public void testParseWithMissingTimestamp() throws IOException {
XContent xContent = XContentFactory.xContent(XContentType.JSON);
Instant before = Instant.now();
String input = "{\"logger\":\"controller\",\"level\":\"INFO\","
+ "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\","
+ "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n";
XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, input);
CppLogMessage msg = CppLogMessage.PARSER.apply(parser, null);
Instant after = Instant.now();
assertTrue(before.isBefore(msg.getTimestamp()) || before.equals(msg.getTimestamp()));
assertTrue(after.isAfter(msg.getTimestamp()) || after.equals(msg.getTimestamp()));
}
@Override
protected CppLogMessage createTestInstance() {
CppLogMessage msg = new CppLogMessage();
CppLogMessage msg = new CppLogMessage(Instant.ofEpochSecond(1494422876L));
msg.setLogger("autodetect");
msg.setLevel("INFO");
msg.setPid(12345);