Skip to content

Commit

Permalink
Removed MD5 for creating Message-IDs and rewrote MessageIdSupplier lo…
Browse files Browse the repository at this point in the history
…gic (#52)
  • Loading branch information
osiegmar committed Aug 23, 2020
1 parent 0e7182d commit 65e93bf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add poolMaxIdleTime configuration option to TCP appenders
[\#49](https://github.com/osiegmar/logback-gelf/pull/49)

### Changed
- Removed MD5 for creating Message-IDs and rewrote MessageIdSupplier logic (#52)
[\#52](https://github.com/osiegmar/logback-gelf/issues/52)

## [3.0.0] - 2020-03-15
### Added
- Allow encoder subclasses to customize the message before it is converted to String.
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/siegmar/logbackgelf/GelfUdpChunker.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GelfUdpChunker {
this.maxChunkPayloadSize = mcs - HEADER_LENGTH;
}

private static ByteBuffer buildChunk(final byte[] messageId, final byte[] message,
private static ByteBuffer buildChunk(final long messageId, final byte[] message,
final byte chunkCount, final byte chunkNo,
final int maxChunkPayloadSize) {

Expand All @@ -106,7 +106,7 @@ private static ByteBuffer buildChunk(final byte[] messageId, final byte[] messag
byteBuffer.put(CHUNKED_GELF_HEADER);

// Message ID 8 bytes
byteBuffer.put(messageId);
byteBuffer.putLong(messageId);

// Sequence number 1 byte
byteBuffer.put(chunkNo);
Expand All @@ -131,7 +131,7 @@ private final class ChunkIterator implements Iterator<ByteBuffer> {
private final byte[] message;
private final int chunkSize;
private final byte chunkCount;
private final byte[] messageId;
private final long messageId;

private byte chunkIdx;

Expand All @@ -156,7 +156,7 @@ private ChunkIterator(final byte[] message) {
this.chunkSize = localChunkSize;
this.chunkCount = (byte) localChunkCount;

messageId = localChunkCount > 1 ? messageIdSupplier.get() : null;
messageId = localChunkCount > 1 ? messageIdSupplier.get() : 0;
}

private int calcChunkCount(final byte[] msg, final int cs) {
Expand Down
54 changes: 14 additions & 40 deletions src/main/java/de/siegmar/logbackgelf/MessageIdSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,29 @@

package de.siegmar.logbackgelf;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Random;

/**
* Supplier implementation for GELF message IDs as used for UDP chunks. Unfortunately the GELF
* protocol limits the message id length to 8 bytes thus an UUID cannot be used (16 bytes).
*/
public class MessageIdSupplier {

protected static final int MESSAGE_ID_LENGTH = 8;
protected static final int LONG_LENGTH = 8;
private byte[] machinePart;

public MessageIdSupplier() {
try {
machinePart = InetAddress.getLocalHost().getAddress();
} catch (final UnknownHostException e) {
machinePart = new byte[LONG_LENGTH];
new Random().nextBytes(machinePart);
}
}

protected byte[] getMachinePart() {
return Arrays.copyOf(machinePart, machinePart.length);
}

protected void setMachinePart(final byte[] machinePart) {
this.machinePart = Arrays.copyOf(machinePart, machinePart.length);
}
// static random to keep Spotbugs happy
private static final Random RANDOM = new Random();
private int machinePart = RANDOM.nextInt();

public byte[] get() {
return Arrays.copyOf(buildMessageId(), MESSAGE_ID_LENGTH);
public int getMachinePart() {
return machinePart;
}

protected byte[] buildMessageId() {
final ByteBuffer bb = ByteBuffer.allocate(machinePart.length + LONG_LENGTH);
bb.put(machinePart);
bb.putLong(System.nanoTime());
bb.flip();
return md5(bb.array());
public void setMachinePart(final int machinePart) {
this.machinePart = machinePart;
}

protected static byte[] md5(final byte[] data) {
try {
return MessageDigest.getInstance("MD5").digest(data);
} catch (final NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
@SuppressWarnings("checkstyle:magicnumber")
public Long get() {
return (long) machinePart << 32 | System.nanoTime() & 0xffffffffL;
}

}
13 changes: 4 additions & 9 deletions src/test/java/de/siegmar/logbackgelf/MessageIdSupplierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@

package de.siegmar.logbackgelf;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.jupiter.api.Test;

public class MessageIdSupplierTest {

private static final int GELF_UDP_MESSAGE_ID_LENGTH = 8;
private MessageIdSupplier messageIdSupplier = new MessageIdSupplier();
private final MessageIdSupplier messageIdSupplier = new MessageIdSupplier();

@Test
public void test() {
final byte[] bytes = messageIdSupplier.get();
assertEquals(GELF_UDP_MESSAGE_ID_LENGTH, bytes.length);
assertFalse(Arrays.equals(bytes, messageIdSupplier.get()));
final Long messageId = messageIdSupplier.get();
assertNotEquals(messageId, messageIdSupplier.get());
}

}

0 comments on commit 65e93bf

Please sign in to comment.