diff --git a/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java b/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java index c1682aa91..5f96c291d 100644 --- a/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java +++ b/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java @@ -378,14 +378,14 @@ public ByteBuffer toTCPBuffer(ByteBuffer buffer) { protected ByteBuffer[] sliceDatagrams(ByteBuffer source, int datagrams, ByteBuffer target) { int messageLength = source.limit(); - byte[] msgId = generateMsgId(); + long msgId = generateMsgId(); // Reuse length of datagrams array since this is supposed to be the correct number of datagrams ByteBuffer[] slices = new ByteBuffer[datagrams]; for (int idx = 0; idx < datagrams; idx++) { int start = target.position(); - target.put(GELF_CHUNKED_ID).put(msgId).put((byte) idx).put((byte) datagrams); + target.put(GELF_CHUNKED_ID).putLong(msgId).put((byte) idx).put((byte) datagrams); int from = idx * maximumMessageSize; int to = from + maximumMessageSize; @@ -403,7 +403,7 @@ protected ByteBuffer[] sliceDatagrams(ByteBuffer source, int datagrams, ByteBuff return slices; } - byte[] generateMsgId() { + long generateMsgId() { // Considerations about generating the message ID: The GELF documentation suggests to // "[g]enerate [the id] from millisecond timestamp + hostname for example": // https://docs.graylog.org/en/3.1/pages/gelf.html#chunking @@ -428,17 +428,15 @@ byte[] generateMsgId() { // Thus, we just need six seconds which will require two bytes. Then we can spend six bytes // on a random number. - return ByteBuffer.allocate(8).putLong(getRandomLong()) - // Overwrite the last two bytes with the timestamp. - .putShort(6, getCurrentTimeMillis()).array(); + return (getCurrentTimeMillis() & 0xFFFF000000000000L) | (getRandomLong() & 0x0000FFFFFFFFFFFFL); } long getRandomLong() { return rand.nextLong(); } - short getCurrentTimeMillis() { - return (short) System.currentTimeMillis(); + long getCurrentTimeMillis() { + return System.currentTimeMillis(); } private byte[] gzipMessage(byte[] message) { diff --git a/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java b/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java index e8fe75417..c18693187 100644 --- a/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java +++ b/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java @@ -2,8 +2,6 @@ import static biz.paluch.logging.gelf.GelfMessageBuilder.newInstance; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -189,15 +187,12 @@ long getRandomLong() { } @Override - short getCurrentTimeMillis() { - return 0x0603; + long getCurrentTimeMillis() { + return 0x90C06030090C0603L; } }; - byte[] expectedBytes = { (byte) 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x06, 0x03 }; - byte[] msgId = gelfMessage.generateMsgId(); - assertEquals(8, msgId.length); // Just to be explicit. - assertArrayEquals(expectedBytes, msgId); + assertThat(gelfMessage.generateMsgId()).isEqualTo(0x90C0201008040201L); } String toString(ByteBuffer allocate) { @@ -271,8 +266,8 @@ private GelfMessage createGelfMessage() { GelfMessage gelfMessage = new GelfMessage() { @Override - byte[] generateMsgId() { - return new byte[] { (byte) 128, 64, 32, 16, 8, 4, 2, 1 }; + long generateMsgId() { + return 0x80406030090C0603L; } }; diff --git a/src/test/java/biz/paluch/logging/gelf/intern/PoolingGelfMessageIntegrationTests.java b/src/test/java/biz/paluch/logging/gelf/intern/PoolingGelfMessageIntegrationTests.java index 87d719038..efcc6eff9 100644 --- a/src/test/java/biz/paluch/logging/gelf/intern/PoolingGelfMessageIntegrationTests.java +++ b/src/test/java/biz/paluch/logging/gelf/intern/PoolingGelfMessageIntegrationTests.java @@ -114,8 +114,8 @@ private GelfMessage createGelfMessage() { GelfMessage gelfMessage = new GelfMessage() { @Override - byte[] generateMsgId() { - return new byte[] { (byte) 128, 64, 32, 16, 8, 4, 2, 1 }; + long generateMsgId() { + return 0x80406030090C0603L; } }; @@ -135,8 +135,8 @@ private PoolingGelfMessage createPooledGelfMessage() { PoolingGelfMessage gelfMessage = new PoolingGelfMessage(PoolHolder.threadLocal()) { @Override - byte[] generateMsgId() { - return new byte[] { (byte) 128, 64, 32, 16, 8, 4, 2, 1 }; + long generateMsgId() { + return 0x80406030090C0603L; } };