Skip to content

Commit cad26b9

Browse files
authored
[Perf] Cache attribute key validation regex (#6865)
The `validateAttribute` method is static and can be called multiple times, which makes re-compiling the key regex validation every time a waste of resources. Originally reported as #6862
1 parent 70c8e89 commit cad26b9

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/metrics/validator/PerfMetricValidator.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
import java.util.ArrayList;
2424
import java.util.List;
2525
import java.util.Locale;
26+
import java.util.regex.Matcher;
27+
import java.util.regex.Pattern;
2628

2729
/** An abstract class providing an interface to validate PerfMetric */
2830
public abstract class PerfMetricValidator {
2931

32+
// Regex to validate Attribute key
33+
private static final Pattern ATTRIBUTE_KEY_PATTERN =
34+
Pattern.compile("^(?!(firebase_|google_|ga_))[A-Za-z][A-Za-z_0-9]*");
35+
3036
/**
3137
* Creates a list of PerfMetricValidator classes based on the contents of PerfMetric
3238
*
@@ -164,7 +170,8 @@ public static void validateAttribute(@NonNull String key, @NonNull String value)
164170
Constants.MAX_ATTRIBUTE_VALUE_LENGTH));
165171
}
166172

167-
if (!key.matches("^(?!(firebase_|google_|ga_))[A-Za-z][A-Za-z_0-9]*")) {
173+
Matcher attributeKeyMatcher = ATTRIBUTE_KEY_PATTERN.matcher(key);
174+
if (!attributeKeyMatcher.matches()) {
168175
throw new IllegalArgumentException(
169176
"Attribute key must start with letter, must only contain alphanumeric characters and"
170177
+ " underscore and must not start with \"firebase_\", \"google_\" and \"ga_");

0 commit comments

Comments
 (0)