From 992e1ea6f466c32f98bc96fa8ce05be81ba93209 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Mon, 9 Oct 2023 14:32:21 -0700 Subject: [PATCH] Add withTag and an overload for withTags --- .../io/micrometer/core/instrument/Meter.java | 21 +++++++++++++++++++ .../core/instrument/DynamicTagsTests.java | 16 +++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java index cbd5a03ba9..f47a0577be 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java @@ -499,6 +499,27 @@ interface MeterProvider { */ T withTags(Iterable 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() { diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/DynamicTagsTests.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/DynamicTagsTests.java index ac829d8240..eaa6881802 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/DynamicTagsTests.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/DynamicTagsTests.java @@ -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); @@ -70,8 +70,8 @@ void shouldCreateTimersDynamically() { MeterProvider 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); @@ -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()) @@ -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);