Skip to content

Commit

Permalink
message timestamp as a BigInteger value #117
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Dvorak committed Jan 8, 2018
1 parent 0b9d01b commit 0ac7178
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/org/graylog2/GelfMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,13 @@ public void setFullMessage(String fullMessage) {
this.fullMessage = fullMessage;
}

public String getTimestamp() {
return new BigDecimal(javaTimestamp).divide(TIME_DIVISOR).toPlainString();
/**
* http://docs.graylog.org/en/2.4/pages/gelf.html#gelf-payload-specification
*
* @return Seconds since UNIX epoch with decimal places for milliseconds;
**/
public BigDecimal getTimestamp() {
return new BigDecimal(javaTimestamp).divide(TIME_DIVISOR);
}

public Long getJavaTimestamp() {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/graylog2/GelfMessageTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.graylog2;

import junit.framework.Assert;
import org.json.simple.JSONValue;
import org.json.simple.JSONObject;
import org.junit.Test;
Expand Down Expand Up @@ -134,4 +135,14 @@ public void concatByteArrayTest() {
assertThat("Two empty bytes concatenates correctly", test3, is(new byte[]{}));
}

@Test
public void numericTimestampTest() {
GelfMessage message = new GelfMessage("Short", "Long", 1L, "WARNING");
JSONObject object = (JSONObject) JSONValue.parse(message.toJson());
assertThat((Double) object.get("timestamp"), is(0.001d));

GelfMessage message2 = new GelfMessage("Short", "Long", 1515403544687L, "WARNING");
JSONObject object2 = (JSONObject) JSONValue.parse(message2.toJson());
assertThat( (Double) object2.get("timestamp"), is(1515403544.687d));
}
}

0 comments on commit 0ac7178

Please sign in to comment.