Skip to content

Commit

Permalink
aws: check for null values in custom tags (#1173)
Browse files Browse the repository at this point in the history
Avoid null values in any custom tags that are passed into the
collector.
  • Loading branch information
brharrington authored Jan 5, 2025
1 parent 52fec99 commit 10d19b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,21 @@ public SpectatorRequestMetricCollector(Registry registry, Map<String, String> cu
this.customTags = new HashMap<>();
customTags.forEach((key, value) -> {
if (ALL_DEFAULT_TAGS.contains(key)) {
registry.propagate(new IllegalArgumentException("Invalid custom tag " + key
+ " - cannot override built-in tag"));
registry.propagate(new IllegalArgumentException(
"Invalid custom tag " + key + " - cannot override built-in tag"));
} else if (value == null) {
registry.propagate(new NullPointerException(
"Custom tag value for key " + key + " is null"));
} else {
this.customTags.put(key, value);
}
});
Preconditions.checkNotNull(handlerContextKey, "handlerContextKey");
this.handlerContextKey = handlerContextKey;
if (ALL_DEFAULT_TAGS.contains(this.handlerContextKey.getName())) {
registry.propagate(new IllegalArgumentException("Invalid handler context key "
+ this.handlerContextKey.getName()
+ " - cannot override built-in tag"));
final String keyName = this.handlerContextKey.getName();
if (ALL_DEFAULT_TAGS.contains(keyName)) {
registry.propagate(new IllegalArgumentException(
"Invalid handler context key " + keyName + " - cannot override built-in tag"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ public void testCustomTags_overrideDefault() {
new SpectatorRequestMetricCollector(new DefaultRegistry(Clock.SYSTEM, config), customTags));
}

@Test
public void testCustomTagsNull() {
Map<String, String> customTags = new HashMap<>();
customTags.put("tagname", null);
collector = new SpectatorRequestMetricCollector(registry, customTags);
execRequest("http://monitoring", 503);
assertEquals(set(), valueSet("tagname"));
}

@Test
public void testHandlerContextKey() {
String contextKeyName = "myContextKey";
Expand Down

0 comments on commit 10d19b6

Please sign in to comment.