Skip to content

Commit

Permalink
Add withTag and an overload for withTags
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Oct 9, 2023
1 parent 3d02c6d commit 992e1ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,27 @@ interface MeterProvider<T extends Meter> {
*/
T withTags(Iterable<? extends Tag> tags);

/**
* Registers (creates a new or gets an existing one if already exists) Meters
* using the provided tags.
* @param tags Tags to attach to the Meter about to be registered
* @return A new or existing Meter
*/
default T withTags(String... tags) {
return withTags(Tags.of(tags));
}

/**
* Registers (creates a new or gets an existing one if already exists) Meters
* using the provided tags.
* @param key the tag key to add
* @param value the tag value to add
* @return A new or existing Meter
*/
default T withTag(String key, String value) {
return withTags(Tags.of(key, value));
}

}

default void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void shouldCreateCountersDynamically() {
.withRegistry(registry);

counterProvider.withTags(Tags.of("dynamic", "1")).increment();
counterProvider.withTags(Tags.of("dynamic", "2")).increment();
counterProvider.withTags(Tags.of("dynamic", "1")).increment();
counterProvider.withTags("dynamic", "2").increment();
counterProvider.withTag("dynamic", "1").increment();

assertThat(registry.getMeters()).hasSize(2);
assertThat(registry.find("test.counter").tags("static", "abc", "dynamic", "1").counters()).hasSize(1);
Expand All @@ -70,8 +70,8 @@ void shouldCreateTimersDynamically() {
MeterProvider<Timer> timerProvider = Timer.builder("test.timer").tag("static", "abc").withRegistry(registry);

timerProvider.withTags(Tags.of("dynamic", "1")).record(Duration.ofMillis(100));
timerProvider.withTags(Tags.of("dynamic", "2")).record(Duration.ofMillis(200));
timerProvider.withTags(Tags.of("dynamic", "1")).record(Duration.ofMillis(100));
timerProvider.withTags("dynamic", "2").record(Duration.ofMillis(200));
timerProvider.withTag("dynamic", "1").record(Duration.ofMillis(100));

assertThat(registry.getMeters()).hasSize(2);
assertThat(registry.find("test.timer").tags("static", "abc", "dynamic", "1").timers()).hasSize(1);
Expand All @@ -95,8 +95,8 @@ void shouldCreateLongTaskTimersDynamically() {
.withRegistry(registry);

timeProvider.withTags(Tags.of("dynamic", "1")).start().stop();
timeProvider.withTags(Tags.of("dynamic", "2")).start().stop();
timeProvider.withTags(Tags.of("dynamic", "1")).start().stop();
timeProvider.withTags("dynamic", "2").start().stop();
timeProvider.withTag("dynamic", "1").start().stop();

assertThat(registry.getMeters()).hasSize(2);
assertThat(registry.find("test.active.timer").tags("static", "abc", "dynamic", "1").longTaskTimers())
Expand Down Expand Up @@ -125,8 +125,8 @@ void shouldCreateDistributionSummariesDynamically() {
.withRegistry(registry);

distributionProvider.withTags(Tags.of("dynamic", "1")).record(1);
distributionProvider.withTags(Tags.of("dynamic", "2")).record(2);
distributionProvider.withTags(Tags.of("dynamic", "1")).record(1);
distributionProvider.withTags("dynamic", "2").record(2);
distributionProvider.withTag("dynamic", "1").record(1);

assertThat(registry.getMeters()).hasSize(2);
assertThat(registry.find("test.distribution").tags("static", "abc", "dynamic", "1").summaries()).hasSize(1);
Expand Down

0 comments on commit 992e1ea

Please sign in to comment.