Skip to content

Commit 10b75a3

Browse files
garyrussellartembilan
authored andcommitted
GH-2400: ReplyingKT Improve CorrelationId Logging
Resolves #2400 Use a hex string instead of a `BigInteger` in `toString`. Also, if the bytes are the length of a UUID, use the standard representation thereof. **cherry-pick to 2.9.x**
1 parent 5fb9b8e commit 10b75a3

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

spring-kafka/src/main/java/org/springframework/kafka/requestreply/CorrelationKey.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,23 +16,25 @@
1616

1717
package org.springframework.kafka.requestreply;
1818

19-
import java.math.BigInteger;
2019
import java.util.Arrays;
2120

2221
import org.springframework.util.Assert;
2322

2423
/**
2524
* Wrapper for byte[] that can be used as a hash key. We could have used BigInteger
26-
* instead but this wrapper is much less expensive. We do use a BigInteger in
27-
* {@link #toString()} though.
25+
* instead but this wrapper is much less expensive.
2826
*
2927
* @author Gary Russell
3028
* @since 2.1.3
3129
*/
3230
public final class CorrelationKey {
3331

32+
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
33+
3434
private final byte[] correlationId;
3535

36+
private String asString;
37+
3638
private volatile Integer hashCode;
3739

3840
public CorrelationKey(byte[] correlationId) { // NOSONAR array reference
@@ -74,9 +76,27 @@ public boolean equals(Object obj) {
7476
return true;
7577
}
7678

79+
private static String bytesToHex(byte[] bytes) {
80+
boolean uuid = bytes.length == 16;
81+
char[] hexChars = new char[bytes.length * 2 + (uuid ? 4 : 0)];
82+
int i = 0;
83+
for (int j = 0; j < bytes.length; j++) {
84+
int v = bytes[j] & 0xFF;
85+
hexChars[i++] = HEX_ARRAY[v >>> 4];
86+
hexChars[i++] = HEX_ARRAY[v & 0x0F];
87+
if (uuid && (j == 3 || j == 5 || j == 7 || j == 9)) {
88+
hexChars[i++] = '-';
89+
}
90+
}
91+
return new String(hexChars);
92+
}
93+
7794
@Override
7895
public String toString() {
79-
return "[" + new BigInteger(this.correlationId) + "]";
96+
if (this.asString == null) {
97+
this.asString = bytesToHex(this.correlationId);
98+
}
99+
return this.asString;
80100
}
81101

82102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.requestreply;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
/**
24+
* @author Gary Russell
25+
* @since 2.8.10
26+
*
27+
*/
28+
public class CorrelationKeyTests {
29+
30+
@Test
31+
void asString() {
32+
CorrelationKey key = new CorrelationKey(new byte[16]);
33+
assertThat(key.toString()).isEqualTo("00000000-0000-0000-0000-000000000000");
34+
key = new CorrelationKey(new byte[10]);
35+
assertThat(key.toString()).isEqualTo("00000000000000000000");
36+
}
37+
38+
}

0 commit comments

Comments
 (0)