Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.newrelic.agent.stats.TransactionStats;
import com.newrelic.agent.tracing.DistributedTraceServiceImpl;
import com.newrelic.agent.transport.HttpError;
import com.newrelic.agent.util.Strings;
import com.newrelic.api.agent.Insights;

import java.text.MessageFormat;
Expand Down Expand Up @@ -394,9 +395,9 @@ private static CustomInsightsEvent createValidatedEvent(String eventType, Map<St

// key or value is null, skip it with a log message and iterate to next entry in attributes.entrySet()
if (key == null || value == null) {
Agent.LOG.log(Level.WARNING, "Custom event [{0}] with invalid attributes key or value of null was reported for a transaction but ignored."
Agent.LOG.log(Level.FINEST, "Custom event [{0}] with invalid attributes key or value of null was reported for a transaction but ignored."
+ " Each key should be a String and each value should be a String, Number, or Boolean. Key: {1} / Value: {2}",
eventType, (key == null ? "[null]" : key), (value == null ? "[null]" : value.toString()));
eventType, (key == null ? "[null]" : Strings.obfuscate(key)));
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.newrelic.agent.tracing.DistributedTraceServiceImpl;
import com.newrelic.agent.transport.HttpError;
import com.newrelic.agent.util.NoOpQueue;
import com.newrelic.agent.util.Strings;
import com.newrelic.api.agent.Logs;

import java.text.MessageFormat;
Expand Down Expand Up @@ -550,11 +551,13 @@ private static LogEvent createValidatedEvent(Map<LogAttributeKey, ?> attributes,
for (Map.Entry<LogAttributeKey, ?> entry : attributes.entrySet()) {
LogAttributeKey logAttrKey = entry.getKey();
Object value = entry.getValue();
String key = logAttrKey.getKey();

// key or value is null, skip it with a log message and iterate to next entry in attributes.entrySet()
if (logAttrKey == null || logAttrKey.getKey() == null || value == null) {
Agent.LOG.log(Level.WARNING, "Log event with invalid attributes key or value of null was reported for a transaction but ignored."
+ " Each key should be a String and each value should be a String, Number, or Boolean.");
if (key == null || value == null) {
Agent.LOG.log(Level.FINEST, "Log event with invalid attributes key or value of null was reported for a transaction but ignored."
+ " Each key should be a String and each value should be a String, Number, or Boolean."
+ " Key: " + (key == null ? "[null]" : Strings.obfuscate(key)));
continue;
}

Expand Down
20 changes: 20 additions & 0 deletions newrelic-agent/src/main/java/com/newrelic/agent/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ public static String replaceDotHyphenWithUnderscore(String string) {
return updated.toString();
}

/**
* Obfuscate the supplied String. If the String is null or less than 4 characters, return the
* original String. Otherwise, return the first character + "***" + the last character.
*
* @param target The String to obfuscate
* @return the obfuscated String or the original String if it's null or less than 4 characters
*/
public static String obfuscate(String target) {
int MIN_LENGTH = 3;
if (target != null) {
if (target.length() <= MIN_LENGTH) {
return target;
}

return target.charAt(0) + "***" + target.charAt(target.length() - 1);
}

return null;
}

private static int findDotOrHyphen(String string, int start) {
if (string == null) {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public void replaceDotHyphenWithUnderscore() {
Assert.assertEquals("___string_with_dots_AND_hyphens_at_ends__",
Strings.replaceDotHyphenWithUnderscore("-.-string.with-dots-AND.hyphens.at_ends.-"));
}

@Test
public void obfuscate() {
Assert.assertNull(Strings.obfuscate(null));
Assert.assertEquals("f", Strings.obfuscate("f"));
Assert.assertEquals("fo", Strings.obfuscate("fo"));
Assert.assertEquals("foo", Strings.obfuscate("foo"));
Assert.assertEquals("b***r", Strings.obfuscate("barbar"));
Assert.assertEquals("l***g", Strings.obfuscate("long-long-string"));
}
}
Loading