Skip to content

Commit 49489b3

Browse files
committed
Minor optimzations to code flow
1 parent 05d8792 commit 49489b3

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

newrelic-agent/src/main/java/com/newrelic/agent/util/LicenseKeyUtil.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import com.newrelic.agent.Agent;
1111

12-
import java.util.Collections;
12+
import java.util.Arrays;
1313
import java.util.regex.Matcher;
1414
import java.util.regex.Pattern;
1515

@@ -33,6 +33,11 @@ public static String obfuscateLicenseKey(String originalString) {
3333
return originalString;
3434
}
3535

36+
// Avoid regex overhead if string doesn't contain "license_key"
37+
if (!originalString.contains("license_key")) {
38+
return originalString;
39+
}
40+
3641
Matcher matcher = LICENSE_KEY_PATTERN.matcher(originalString);
3742
if (!matcher.find()) {
3843
return originalString;
@@ -60,10 +65,15 @@ private static String partialObfuscation(String licenseKey) {
6065
// Otherwise, keep the first 10 characters of the key and replace the
6166
// remaining key characters with "*"
6267
if (keyLength > KEY_LENGTH_CUTOFF) {
63-
return licenseKey.substring(0, KEY_LENGTH_CUTOFF) +
64-
String.join("", Collections.nCopies(keyLength - KEY_LENGTH_CUTOFF, "*"));
68+
return licenseKey.substring(0, KEY_LENGTH_CUTOFF) + createAsterisks(keyLength - KEY_LENGTH_CUTOFF);
6569
} else {
66-
return String.join("", Collections.nCopies(keyLength, "*"));
70+
return createAsterisks(keyLength);
6771
}
6872
}
73+
74+
private static String createAsterisks(int count) {
75+
char[] asterisks = new char[count];
76+
Arrays.fill(asterisks, '*');
77+
return new String(asterisks);
78+
}
6979
}

newrelic-agent/src/test/java/com/newrelic/agent/util/LicenseKeyUtilTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public void testObfuscateLicenseKey() {
3737
serviceManager.setConfigService(configService);
3838

3939
// When
40+
long startTime = System.nanoTime();
4041
String actualRequestUrl = LicenseKeyUtil.obfuscateLicenseKey(originalRequestUrl);
42+
System.out.println("Time (ns): " + (System.nanoTime() - startTime));
4143
String actualJsonPayload = LicenseKeyUtil.obfuscateLicenseKey(originalJsonPayload);
4244

4345
// Then

0 commit comments

Comments
 (0)