Skip to content

Commit

Permalink
Allow specifying the meterNameConsumer for HighCardinalityTagsDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-heller committed Aug 11, 2023
1 parent 5c6da7a commit e2e9029
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,24 @@ public Config withHighCardinalityTagsDetector(long threshold, Duration delay) {
new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay));
}

/**
* Creates and starts a new {@link HighCardinalityTagsDetector} for this registry.
* @param threshold The threshold to use to detect high cardinality tags (if the
* number of Meters with the same name is higher than this value, that's a high
* cardinality tag).
* @param delay The delay between the termination of one check and the
* commencement of the next.
* @param meterNameConsumer A consumer that will be called for each high
* cardinality tag detected.
* @return This configuration instance.
* @since 1.12.0
*/
public Config withHighCardinalityTagsDetector(long threshold, Duration delay,
Consumer<String> meterNameConsumer) {
return withHighCardinalityTagsDetector(
new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay, meterNameConsumer));
}

private Config withHighCardinalityTagsDetector(HighCardinalityTagsDetector newHighCardinalityTagsDetector) {
if (highCardinalityTagsDetector != null) {
highCardinalityTagsDetector.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ void shouldNotDetectNoTags() {
assertThat(highCardinalityTagsDetector.findFirst()).isEmpty();
}

@Test
void addsCustomMeterNameConsumer() {
TestCustomMeterNameConsumer customMeterNameConsumer = new TestCustomMeterNameConsumer();
this.highCardinalityTagsDetector = new HighCardinalityTagsDetector(registry, 3, Duration.ofMinutes(1),
customMeterNameConsumer);

for (int i = 0; i < 4; i++) {
Counter.builder("test.counter").tag("index", String.valueOf(i)).register(registry).increment();
}
highCardinalityTagsDetector.start();

await().atMost(Duration.ofSeconds(1))
.until(() -> "test.counter_customized".equals(customMeterNameConsumer.getName()));
}

private static class TestMeterNameConsumer implements Consumer<String> {

@Nullable
Expand All @@ -107,4 +122,21 @@ public String getName() {

}

private static class TestCustomMeterNameConsumer implements Consumer<String> {

@Nullable
private String name;

@Override
public void accept(String name) {
this.name = name + "_customized";
}

@Nullable
public String getName() {
return this.name;
}

}

}

0 comments on commit e2e9029

Please sign in to comment.