Skip to content

Commit 896ca16

Browse files
committed
Switch to standard for loop for early exit, add optimistic lookup
1 parent f829e51 commit 896ca16

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

api/incubator/src/main/java/io/opentelemetry/api/incubator/propagation/EnvironmentGetter.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,20 @@ public String get(@Nullable Map<String, String> carrier, String key) {
8383
return null;
8484
}
8585
String normalizedKey = EnvironmentSetter.normalizeKey(key);
86-
String[] result = new String[] {null};
87-
carrier.forEach(
88-
(entryKey, entryValue) -> {
89-
if (EnvironmentSetter.normalizeKey(entryKey).equals(normalizedKey)) {
90-
result[0] = entryValue;
91-
}
92-
});
93-
return result[0];
86+
// first, perform an optimistic lookup for an exact match on the normalized key
87+
String value = carrier.get(normalizedKey);
88+
if (value != null) {
89+
return value;
90+
}
91+
// next, iterate over the carrier normalizing each entry and evaluating for a match
92+
// if memory allocation becomes an issue, can implement using iterative normalization, comparing
93+
// an entry character by character to the normalized key, normalizing along the way.
94+
for (Map.Entry<String, String> entry : carrier.entrySet()) {
95+
if (EnvironmentSetter.normalizeKey(entry.getKey()).equals(normalizedKey)) {
96+
return entry.getValue();
97+
}
98+
}
99+
return null;
94100
}
95101

96102
@Override

0 commit comments

Comments
 (0)