diff --git a/src/main/asciidoc/new-features.adoc b/src/main/asciidoc/new-features.adoc index 336906050d..0aaa295421 100644 --- a/src/main/asciidoc/new-features.adoc +++ b/src/main/asciidoc/new-features.adoc @@ -6,6 +6,7 @@ * Kotlin Coroutines support for `SCAN`/`HSCAN`/`SSCAN`/`ZSCAN` through `ScanFlow`. * Command Listener API through `RedisClient.addListener(CommandListener)`. +* <> through `MicrometerCommandLatencyRecorder`. [[new-features.6-0-0]] == What's new in Lettuce 6.0 diff --git a/src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorder.java b/src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorder.java index 00a3de7af1..c169000287 100644 --- a/src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorder.java +++ b/src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorder.java @@ -15,18 +15,22 @@ */ package io.lettuce.core.metrics; -import io.lettuce.core.protocol.ProtocolKeyword; -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.Timer; -import io.netty.channel.local.LocalAddress; - import java.net.SocketAddress; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; +import io.lettuce.core.internal.LettuceAssert; +import io.lettuce.core.protocol.ProtocolKeyword; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Timer; +import io.netty.channel.local.LocalAddress; + /** + * Micrometer implementation of {@link CommandLatencyRecorder} + * * @author Steven Sheehy + * @since 6.1 */ public class MicrometerCommandLatencyRecorder implements CommandLatencyRecorder { @@ -42,13 +46,23 @@ public class MicrometerCommandLatencyRecorder implements CommandLatencyRecorder private final MeterRegistry meterRegistry; - private final MicrometerCommandLatencyCollectorOptions options; + private final MicrometerOptions options; private final Map completionTimers = new ConcurrentHashMap<>(); private final Map firstResponseTimers = new ConcurrentHashMap<>(); - public MicrometerCommandLatencyRecorder(MeterRegistry meterRegistry, MicrometerCommandLatencyCollectorOptions options) { + /** + * Create a new {@link MicrometerCommandLatencyRecorder} instance given {@link MeterRegistry} and {@link MicrometerOptions}. + * + * @param meterRegistry + * @param options + */ + public MicrometerCommandLatencyRecorder(MeterRegistry meterRegistry, MicrometerOptions options) { + + LettuceAssert.notNull(meterRegistry, "MeterRegistry must not be null"); + LettuceAssert.notNull(options, "MicrometerOptions must not be null"); + this.meterRegistry = meterRegistry; this.options = options; } @@ -62,6 +76,7 @@ public void recordCommandLatency(SocketAddress local, SocketAddress remote, Prot } CommandLatencyId commandLatencyId = createId(local, remote, protocolKeyword); + Timer firstResponseTimer = firstResponseTimers.computeIfAbsent(commandLatencyId, this::firstResponseTimer); firstResponseTimer.record(firstResponseLatency, TimeUnit.NANOSECONDS); @@ -79,38 +94,35 @@ private CommandLatencyId createId(SocketAddress local, SocketAddress remote, Pro } protected Timer completionTimer(CommandLatencyId commandLatencyId) { + Timer.Builder timer = Timer.builder(METRIC_COMPLETION) .description("Latency between command send and command completion (complete response received") .tag(LABEL_COMMAND, commandLatencyId.commandType().name()) .tag(LABEL_LOCAL, commandLatencyId.localAddress().toString()) - .tag(LABEL_REMOTE, commandLatencyId.remoteAddress().toString()) - .tags(options.tags()); + .tag(LABEL_REMOTE, commandLatencyId.remoteAddress().toString()).tags(options.tags()); if (options.isHistogram()) { - timer.publishPercentileHistogram() - .publishPercentiles(options.targetPercentiles()) - .minimumExpectedValue(options.minLatency()) - .maximumExpectedValue(options.maxLatency()); + timer.publishPercentileHistogram().publishPercentiles(options.targetPercentiles()) + .minimumExpectedValue(options.minLatency()).maximumExpectedValue(options.maxLatency()); } return timer.register(meterRegistry); } protected Timer firstResponseTimer(CommandLatencyId commandLatencyId) { + Timer.Builder timer = Timer.builder(METRIC_FIRST_RESPONSE) .description("Latency between command send and first response (first response received)") .tag(LABEL_COMMAND, commandLatencyId.commandType().name()) .tag(LABEL_LOCAL, commandLatencyId.localAddress().toString()) - .tag(LABEL_REMOTE, commandLatencyId.remoteAddress().toString()) - .tags(options.tags()); + .tag(LABEL_REMOTE, commandLatencyId.remoteAddress().toString()).tags(options.tags()); if (options.isHistogram()) { - timer.publishPercentileHistogram() - .publishPercentiles(options.targetPercentiles()) - .minimumExpectedValue(options.minLatency()) - .maximumExpectedValue(options.maxLatency()); + timer.publishPercentileHistogram().publishPercentiles(options.targetPercentiles()) + .minimumExpectedValue(options.minLatency()).maximumExpectedValue(options.maxLatency()); } return timer.register(meterRegistry); } + } diff --git a/src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyCollectorOptions.java b/src/main/java/io/lettuce/core/metrics/MicrometerOptions.java similarity index 57% rename from src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyCollectorOptions.java rename to src/main/java/io/lettuce/core/metrics/MicrometerOptions.java index 5c1b25eb51..c8b0bcdbb6 100644 --- a/src/main/java/io/lettuce/core/metrics/MicrometerCommandLatencyCollectorOptions.java +++ b/src/main/java/io/lettuce/core/metrics/MicrometerOptions.java @@ -15,18 +15,19 @@ */ package io.lettuce.core.metrics; +import java.time.Duration; + import io.lettuce.core.internal.LettuceAssert; import io.micrometer.core.instrument.Tags; -import java.time.Duration; -import java.util.concurrent.TimeUnit; - /** - * The Micrometer implementation of {@link CommandLatencyCollectorOptions}. + * Configuration options for {@link MicrometerCommandLatencyRecorder}. * * @author Steven Sheehy + * @author Mark Paluch + * @since 6.1 */ -public class MicrometerCommandLatencyCollectorOptions implements CommandLatencyCollectorOptions { +public class MicrometerOptions { public static final boolean DEFAULT_ENABLED = true; @@ -40,7 +41,7 @@ public class MicrometerCommandLatencyCollectorOptions implements CommandLatencyC public static final double[] DEFAULT_TARGET_PERCENTILES = new double[] { 0.50, 0.90, 0.95, 0.99, 0.999 }; - private static final MicrometerCommandLatencyCollectorOptions DISABLED = builder().disable().build(); + private static final MicrometerOptions DISABLED = builder().disable().build(); private final Builder builder; @@ -58,7 +59,8 @@ public class MicrometerCommandLatencyCollectorOptions implements CommandLatencyC private final double[] targetPercentiles; - protected MicrometerCommandLatencyCollectorOptions(Builder builder) { + protected MicrometerOptions(Builder builder) { + this.builder = builder; this.enabled = builder.enabled; this.histogram = builder.histogram; @@ -70,52 +72,47 @@ protected MicrometerCommandLatencyCollectorOptions(Builder builder) { } /** - * Create a new {@link MicrometerCommandLatencyCollectorOptions} instance using default settings. + * Create a new {@link MicrometerOptions} instance using default settings. * - * @return a new instance of {@link MicrometerCommandLatencyCollectorOptions} instance using default settings + * @return a new instance of {@link MicrometerOptions} instance using default settings */ - public static MicrometerCommandLatencyCollectorOptions create() { + public static MicrometerOptions create() { return builder().build(); } /** - * Create a {@link MicrometerCommandLatencyCollectorOptions} instance with disabled event emission. + * Create a {@link MicrometerOptions} instance with disabled event emission. * - * @return a new instance of {@link MicrometerCommandLatencyCollectorOptions} with disabled event emission + * @return a new instance of {@link MicrometerOptions} with disabled event emission */ - public static MicrometerCommandLatencyCollectorOptions disabled() { + public static MicrometerOptions disabled() { return DISABLED; } /** - * Returns a new {@link MicrometerCommandLatencyCollectorOptions.Builder} to construct - * {@link MicrometerCommandLatencyCollectorOptions}. + * Returns a new {@link MicrometerOptions.Builder} to construct {@link MicrometerOptions}. * - * @return a new {@link MicrometerCommandLatencyCollectorOptions.Builder} to construct - * {@link MicrometerCommandLatencyCollectorOptions}. + * @return a new {@link MicrometerOptions.Builder} to construct {@link MicrometerOptions}. */ - public static MicrometerCommandLatencyCollectorOptions.Builder builder() { - return new MicrometerCommandLatencyCollectorOptions.Builder(); + public static MicrometerOptions.Builder builder() { + return new MicrometerOptions.Builder(); } /** - * Returns a builder to create new {@link MicrometerCommandLatencyCollectorOptions} whose settings are replicated from the - * current {@link MicrometerCommandLatencyCollectorOptions}. + * Returns a builder to create new {@link MicrometerOptions} whose settings are replicated from the current + * {@link MicrometerOptions}. * - * @return a a {@link CommandLatencyCollectorOptions.Builder} to create new {@link MicrometerCommandLatencyCollectorOptions} - * whose settings are replicated from the current {@link MicrometerCommandLatencyCollectorOptions} - * - * @since 5.1 + * @return a a {@link CommandLatencyCollectorOptions.Builder} to create new {@link MicrometerOptions} whose settings are + * replicated from the current {@link MicrometerOptions} */ - @Override - public MicrometerCommandLatencyCollectorOptions.Builder mutate() { + public MicrometerOptions.Builder mutate() { return this.builder; } /** - * Builder for {@link MicrometerCommandLatencyCollectorOptions}. + * Builder for {@link MicrometerOptions}. */ - public static class Builder implements CommandLatencyCollectorOptions.Builder { + public static class Builder { private boolean enabled = DEFAULT_ENABLED; @@ -139,7 +136,6 @@ private Builder() { * * @return this {@link Builder}. */ - @Override public Builder disable() { this.enabled = false; return this; @@ -150,15 +146,14 @@ public Builder disable() { * * @return this {@link Builder}. */ - @Override public Builder enable() { this.enabled = true; return this; } /** - * Enable histogram buckets used to generate aggregable percentile approximations in monitoring - * systems that have query facilities to do so. + * Enable histogram buckets used to generate aggregable percentile approximations in monitoring systems that have query + * facilities to do so. * * @param histogram {@code true} if histogram buckets are recorded * @return this {@link Builder}. @@ -173,79 +168,66 @@ public Builder histogram(boolean histogram) { * host/connection point will be recorded separately which allows to inspect every connection individually. If * {@code false}, multiple connections to the same host/connection point will be recorded together. This allows a * consolidated view on one particular service. Defaults to {@code false}. See - * {@link MicrometerCommandLatencyCollectorOptions#DEFAULT_LOCAL_DISTINCTION}. + * {@link MicrometerOptions#DEFAULT_LOCAL_DISTINCTION}. * - * Warning: Enabling this could potentially cause a label cardinality explosion in the remote metric system and - * should be used with caution. + * Warning: Enabling this could potentially cause a label cardinality explosion in the remote metric system and should + * be used with caution. * * @param localDistinction {@code true} if latencies are recorded distinct on local level (per connection) * @return this {@link Builder}. */ - @Override public Builder localDistinction(boolean localDistinction) { this.localDistinction = localDistinction; return this; } /** - * Sets the maximum value that this timer is expected to observe. Sets an upper bound - * on histogram buckets that are shipped to monitoring systems that support aggregable percentile approximations. - * Only applicable when histogram is enabled. Defaults to {@code 5m}. - * See {@link MicrometerCommandLatencyCollectorOptions#DEFAULT_MAX_LATENCY}. + * Sets the maximum value that this timer is expected to observe. Sets an upper bound on histogram buckets that are + * shipped to monitoring systems that support aggregable percentile approximations. Only applicable when histogram is + * enabled. Defaults to {@code 5m}. See {@link MicrometerOptions#DEFAULT_MAX_LATENCY}. * - * @param maxLatency The maximum value that this timer is expected to observe. + * @param maxLatency The maximum value that this timer is expected to observe * @return this {@link Builder}. */ public Builder maxLatency(Duration maxLatency) { + LettuceAssert.notNull(maxLatency, "Max Latency must not be null"); this.maxLatency = maxLatency; return this; } /** - * Sets the minimum value that this timer is expected to observe. Sets a lower bound - * on histogram buckets that are shipped to monitoring systems that support aggregable percentile approximations. - * Only applicable when histogram is enabled. Defaults to {@code 1ms}. - * See {@link MicrometerCommandLatencyCollectorOptions#DEFAULT_MIN_LATENCY}. + * Sets the minimum value that this timer is expected to observe. Sets a lower bound on histogram buckets that are + * shipped to monitoring systems that support aggregable percentile approximations. Only applicable when histogram is + * enabled. Defaults to {@code 1ms}. See {@link MicrometerOptions#DEFAULT_MIN_LATENCY}. * - * @param minLatency The minimum value that this timer is expected to observe. + * @param minLatency The minimum value that this timer is expected to observe * @return this {@link Builder}. */ public Builder minLatency(Duration minLatency) { + LettuceAssert.notNull(maxLatency, "Max Latency must not be null"); this.minLatency = minLatency; return this; } - /** - * Not supported since the MeterRegistry implementation defines whether metrics are cumulative or reset - * - * @param resetLatenciesAfterEvent {@code true} if the recorded latencies should be reset once the metrics event was - * emitted - * @return this {@link Builder}. - */ - @Override - public Builder resetLatenciesAfterEvent(boolean resetLatenciesAfterEvent) { - throw new UnsupportedOperationException("resetLatenciesAfterEvent not supported for Micrometer"); - } - /** * Extra tags to add to the generated metrics. Defaults to {@code Tags.empty()}. * - * @param tags Tags to add to the metrics. + * @param tags Tags to add to the metrics * @return this {@link Builder}. */ public Builder tags(Tags tags) { + LettuceAssert.notNull(tags, "Tags must not be null"); this.tags = tags; return this; } /** * Sets the emitted percentiles. Defaults to 0.50, 0.90, 0.95, 0.99, 0.999}. Only applicable when histogram is enabled. - * See {@link MicrometerCommandLatencyCollectorOptions#DEFAULT_TARGET_PERCENTILES}. + * See {@link MicrometerOptions#DEFAULT_TARGET_PERCENTILES}. * * @param targetPercentiles the percentiles which should be emitted, must not be {@code null} * @return this {@link Builder}. */ - @Override public Builder targetPercentiles(double[] targetPercentiles) { LettuceAssert.notNull(targetPercentiles, "TargetPercentiles must not be null"); this.targetPercentiles = targetPercentiles; @@ -253,26 +235,14 @@ public Builder targetPercentiles(double[] targetPercentiles) { } /** - * Not supported since the MeterRegistry implementation defines the base unit and cannot be changed. - * - * @param targetUnit the target unit - * @return this {@link Builder}. + * @return a new instance of {@link MicrometerOptions}. */ - @Override - public Builder targetUnit(TimeUnit targetUnit) { - throw new UnsupportedOperationException("targetUnit not supported for Micrometer"); + public MicrometerOptions build() { + return new MicrometerOptions(this); } - /** - * @return a new instance of {@link MicrometerCommandLatencyCollectorOptions}. - */ - @Override - public MicrometerCommandLatencyCollectorOptions build() { - return new MicrometerCommandLatencyCollectorOptions(this); - } } - @Override public boolean isEnabled() { return enabled; } @@ -281,7 +251,6 @@ public boolean isHistogram() { return histogram; } - @Override public boolean localDistinction() { return localDistinction; } @@ -294,24 +263,14 @@ public Duration minLatency() { return minLatency; } - @Override - public boolean resetLatenciesAfterEvent() { - throw new UnsupportedOperationException("resetLatenciesAfterEvent not supported for Micrometer"); - } - public Tags tags() { return tags; } - @Override public double[] targetPercentiles() { double[] result = new double[targetPercentiles.length]; System.arraycopy(targetPercentiles, 0, result, 0, targetPercentiles.length); return result; } - @Override - public TimeUnit targetUnit() { - throw new UnsupportedOperationException("targetUnit not supported for Micrometer"); - } } diff --git a/src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorderUnitTests.java b/src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorderUnitTests.java index d5912cce0f..7989a27d94 100644 --- a/src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorderUnitTests.java +++ b/src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyRecorderUnitTests.java @@ -15,7 +15,18 @@ */ package io.lettuce.core.metrics; +import static io.lettuce.core.metrics.MicrometerCommandLatencyRecorder.*; +import static org.assertj.core.api.Assertions.*; + +import java.net.SocketAddress; + +import org.assertj.core.api.InstanceOfAssertFactories; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + import com.google.common.primitives.Doubles; + import io.lettuce.core.protocol.CommandType; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tags; @@ -24,17 +35,10 @@ import io.micrometer.core.instrument.distribution.ValueAtPercentile; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.netty.channel.local.LocalAddress; -import org.assertj.core.api.InstanceOfAssertFactories; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.net.SocketAddress; - -import static io.lettuce.core.metrics.MicrometerCommandLatencyRecorder.*; -import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link MicrometerCommandLatencyRecorder}. + * * @author Steven Sheehy */ @ExtendWith(MockitoExtension.class) @@ -48,7 +52,8 @@ class MicrometerCommandLatencyRecorderUnitTests { @Test void verifyMetrics() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.create(); + + MicrometerOptions options = MicrometerOptions.create(); MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options); commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 1, 10); @@ -57,29 +62,22 @@ void verifyMetrics() { commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.BGSAVE, 300, 1500); assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).timers()).hasSize(2); - assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).tag(LABEL_COMMAND, CommandType.BGSAVE.name()).timers()) - .hasSize(1) - .element(0) - .extracting(Timer::takeSnapshot) - .hasFieldOrPropertyWithValue("count", 3L) - .hasFieldOrPropertyWithValue("max", 300.0) - .hasFieldOrPropertyWithValue("mean", 200.0) + assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).tag(LABEL_COMMAND, CommandType.BGSAVE.name()).timers()).hasSize(1) + .element(0).extracting(Timer::takeSnapshot).hasFieldOrPropertyWithValue("count", 3L) + .hasFieldOrPropertyWithValue("max", 300.0).hasFieldOrPropertyWithValue("mean", 200.0) .hasFieldOrPropertyWithValue("total", 600.0); assertThat(meterRegistry.find(METRIC_COMPLETION).timers()).hasSize(2); - assertThat(meterRegistry.find(METRIC_COMPLETION).tag(LABEL_COMMAND, CommandType.BGSAVE.name()).timers()) - .hasSize(1) - .element(0) - .extracting(Timer::takeSnapshot) - .hasFieldOrPropertyWithValue("count", 3L) - .hasFieldOrPropertyWithValue("max", 1500.0) - .hasFieldOrPropertyWithValue("mean", 1000.0) + assertThat(meterRegistry.find(METRIC_COMPLETION).tag(LABEL_COMMAND, CommandType.BGSAVE.name()).timers()).hasSize(1) + .element(0).extracting(Timer::takeSnapshot).hasFieldOrPropertyWithValue("count", 3L) + .hasFieldOrPropertyWithValue("max", 1500.0).hasFieldOrPropertyWithValue("mean", 1000.0) .hasFieldOrPropertyWithValue("total", 3000.0); } @Test void disabled() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.disabled(); + + MicrometerOptions options = MicrometerOptions.disabled(); MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options); commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 1, 10); @@ -89,25 +87,18 @@ void disabled() { @Test void histogramEnabled() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .histogram(true) - .build(); + + MicrometerOptions options = MicrometerOptions.builder().histogram(true).build(); MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options); commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 1, 10); commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 2, 5); - assertThat(meterRegistry.find(METRIC_COMPLETION).timers()) - .hasSize(1) - .element(0) - .extracting(Timer::takeSnapshot) + assertThat(meterRegistry.find(METRIC_COMPLETION).timers()).hasSize(1).element(0).extracting(Timer::takeSnapshot) .extracting(HistogramSnapshot::percentileValues, InstanceOfAssertFactories.array(ValueAtPercentile[].class)) .extracting(ValueAtPercentile::percentile) .containsExactlyElementsOf(Doubles.asList(options.targetPercentiles())); - assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).timers()) - .hasSize(1) - .element(0) - .extracting(Timer::takeSnapshot) + assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).timers()).hasSize(1).element(0).extracting(Timer::takeSnapshot) .extracting(HistogramSnapshot::percentileValues, InstanceOfAssertFactories.array(ValueAtPercentile[].class)) .extracting(ValueAtPercentile::percentile) .containsExactlyElementsOf(Doubles.asList(options.targetPercentiles())); @@ -115,9 +106,8 @@ void histogramEnabled() { @Test void localDistinctionEnabled() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .localDistinction(true) - .build(); + + MicrometerOptions options = MicrometerOptions.builder().localDistinction(true).build(); MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options); LocalAddress localAddress2 = new LocalAddress("localhost:12345"); @@ -134,9 +124,8 @@ void localDistinctionEnabled() { @Test void localDistinctionDisabled() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .localDistinction(false) - .build(); + + MicrometerOptions options = MicrometerOptions.builder().localDistinction(false).build(); MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options); LocalAddress localAddress2 = new LocalAddress("localhost:12345"); @@ -151,10 +140,9 @@ void localDistinctionDisabled() { @Test void tags() { + Tags tags = Tags.of("app", "foo"); - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .tags(tags) - .build(); + MicrometerOptions options = MicrometerOptions.builder().tags(tags).build(); MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options); commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 1, 10); @@ -166,4 +154,5 @@ void tags() { assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).tag(LABEL_COMMAND, CommandType.AUTH.name()).timers()).hasSize(1); assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).tag(LABEL_REMOTE, REMOTE_ADDRESS.toString()).timers()).hasSize(1); } + } diff --git a/src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyCollectorOptionsUnitTests.java b/src/test/java/io/lettuce/core/metrics/MicrometerOptionsUnitTests.java similarity index 62% rename from src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyCollectorOptionsUnitTests.java rename to src/test/java/io/lettuce/core/metrics/MicrometerOptionsUnitTests.java index be1e8346a0..0be2d93f59 100644 --- a/src/test/java/io/lettuce/core/metrics/MicrometerCommandLatencyCollectorOptionsUnitTests.java +++ b/src/test/java/io/lettuce/core/metrics/MicrometerOptionsUnitTests.java @@ -15,22 +15,26 @@ */ package io.lettuce.core.metrics; -import io.micrometer.core.instrument.Tags; -import org.junit.jupiter.api.Test; +import static io.lettuce.core.metrics.MicrometerOptions.*; +import static org.assertj.core.api.Assertions.*; import java.time.Duration; -import static org.assertj.core.api.Assertions.assertThat; -import static io.lettuce.core.metrics.MicrometerCommandLatencyCollectorOptions.*; +import org.junit.jupiter.api.Test; + +import io.micrometer.core.instrument.Tags; /** + * Unit tests for {@link MicrometerOptions}. + * * @author Steven Sheehy */ -class MicrometerCommandLatencyCollectorOptionsUnitTests { +class MicrometerOptionsUnitTests { @Test void create() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.create(); + + MicrometerOptions options = MicrometerOptions.create(); assertThat(options.isEnabled()).isEqualTo(DEFAULT_ENABLED); assertThat(options.isHistogram()).isEqualTo(DEFAULT_HISTOGRAM); @@ -43,55 +47,53 @@ void create() { @Test void disabled() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.disabled(); + + MicrometerOptions options = MicrometerOptions.disabled(); assertThat(options.isEnabled()).isFalse(); } @Test void histogram() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .histogram(true) - .build(); + + MicrometerOptions options = MicrometerOptions.builder().histogram(true).build(); assertThat(options.isHistogram()).isTrue(); } @Test void localDistinction() { - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .localDistinction(true) - .build(); + + MicrometerOptions options = MicrometerOptions.builder().localDistinction(true).build(); assertThat(options.localDistinction()).isTrue(); } @Test void maxLatency() { + Duration maxLatency = Duration.ofSeconds(2L); - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .maxLatency(maxLatency) - .build(); + MicrometerOptions options = MicrometerOptions.builder().maxLatency(maxLatency).build(); assertThat(options.maxLatency()).isEqualTo(maxLatency); } @Test void minLatency() { + Duration minLatency = Duration.ofSeconds(2L); - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .minLatency(minLatency) - .build(); + MicrometerOptions options = MicrometerOptions.builder().minLatency(minLatency).build(); assertThat(options.minLatency()).isEqualTo(minLatency); } @Test void targetPercentiles() { + double[] percentiles = new double[] { 0.1, 0.2, 0.3 }; - MicrometerCommandLatencyCollectorOptions options = MicrometerCommandLatencyCollectorOptions.builder() - .targetPercentiles(percentiles).build(); + MicrometerOptions options = MicrometerOptions.builder().targetPercentiles(percentiles).build(); assertThat(options.targetPercentiles()).hasSize(3).isEqualTo(percentiles); } + }