Skip to content

Commit

Permalink
Add Command filter to MicrometerCommandLatencyRecorder #2406
Browse files Browse the repository at this point in the history
We can now filter particular commands from being recorded to Micrometer.

Original pull request: #2407
  • Loading branch information
andre237 authored and mp911de committed Aug 15, 2023
1 parent 45a5143 commit e66c148
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public MicrometerCommandLatencyRecorder(MeterRegistry meterRegistry, MicrometerO
public void recordCommandLatency(SocketAddress local, SocketAddress remote, ProtocolKeyword protocolKeyword,
long firstResponseLatency, long completionLatency) {

if (!isEnabled()) {
if (!isEnabled() || !isCommandEnabled(protocolKeyword)) {
return;
}

Expand All @@ -89,6 +89,11 @@ public boolean isEnabled() {
return options.isEnabled();
}

private boolean isCommandEnabled(ProtocolKeyword protocolKeyword) {
return options.getEnabledCommands().isEmpty() ||
options.getEnabledCommands().stream().anyMatch(command -> command.name().equals(protocolKeyword.name()));
}

private CommandLatencyId createId(SocketAddress local, SocketAddress remote, ProtocolKeyword commandType) {
return CommandLatencyId.create(options.localDistinction() ? local : LocalAddress.ANY, remote, commandType);
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/lettuce/core/metrics/MicrometerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package io.lettuce.core.metrics;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.protocol.CommandType;
import io.micrometer.core.instrument.Tags;

/**
Expand All @@ -41,8 +45,11 @@ public class MicrometerOptions {

public static final double[] DEFAULT_TARGET_PERCENTILES = new double[] { 0.50, 0.90, 0.95, 0.99, 0.999 };

private static final List<CommandType> DEFAULT_ENABLED_COMMANDS = new ArrayList<>();

private static final MicrometerOptions DISABLED = builder().disable().build();


private final Builder builder;

private final boolean enabled;
Expand All @@ -59,6 +66,8 @@ public class MicrometerOptions {

private final double[] targetPercentiles;

private final List<CommandType> enabledCommands;

protected MicrometerOptions(Builder builder) {

this.builder = builder;
Expand All @@ -69,6 +78,7 @@ protected MicrometerOptions(Builder builder) {
this.minLatency = builder.minLatency;
this.tags = builder.tags;
this.targetPercentiles = builder.targetPercentiles;
this.enabledCommands = builder.enabledCommands;
}

/**
Expand Down Expand Up @@ -128,6 +138,8 @@ public static class Builder {

private double[] targetPercentiles = DEFAULT_TARGET_PERCENTILES;

private List<CommandType> enabledCommands = DEFAULT_ENABLED_COMMANDS;

private Builder() {
}

Expand Down Expand Up @@ -234,6 +246,18 @@ public Builder targetPercentiles(double[] targetPercentiles) {
return this;
}

/**
* Sets which commands are enabled for latency recording. Defaults to an empty list, which means all commands will be recorded
* See {@link MicrometerOptions#DEFAULT_ENABLED_COMMANDS}.
*
* @param commands list of Redis commands that are enabled for latency recording
* @return this {@link Builder}.
*/
public Builder enabledCommands(CommandType... commands) {
this.enabledCommands = Arrays.asList(commands);
return this;
}

/**
* @return a new instance of {@link MicrometerOptions}.
*/
Expand Down Expand Up @@ -273,4 +297,7 @@ public double[] targetPercentiles() {
return result;
}

public List<CommandType> getEnabledCommands() {
return enabledCommands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,31 @@ void tags() {
assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).tag(LABEL_REMOTE, REMOTE_ADDRESS.toString()).timers()).hasSize(1);
}

@Test
void enabledCommandsEmpty() {
MicrometerOptions options = MicrometerOptions.builder().build();
MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options);

commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 1, 10);
commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.CLUSTER, 1, 10);

assertThat(meterRegistry.find(METRIC_COMPLETION).timers()).hasSize(2);
assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).timers()).hasSize(2);
}

@Test
void enabledCommandsNotEmpty() {
MicrometerOptions options = MicrometerOptions.builder().enabledCommands(CommandType.CLUSTER).build();
MicrometerCommandLatencyRecorder commandLatencyRecorder = new MicrometerCommandLatencyRecorder(meterRegistry, options);

commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.AUTH, 1, 10);
commandLatencyRecorder.recordCommandLatency(LOCAL_ADDRESS, REMOTE_ADDRESS, CommandType.CLUSTER, 1, 10);

assertThat(meterRegistry.find(METRIC_COMPLETION).timers()).hasSize(1);
assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).timers()).hasSize(1);

assertThat(meterRegistry.find(METRIC_COMPLETION).tag(LABEL_COMMAND, CommandType.CLUSTER.name()).timers()).hasSize(1);
assertThat(meterRegistry.find(METRIC_FIRST_RESPONSE).tag(LABEL_COMMAND, CommandType.CLUSTER.name()).timers()).hasSize(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.time.Duration;

import io.lettuce.core.protocol.CommandType;
import org.junit.jupiter.api.Test;

import io.micrometer.core.instrument.Tags;
Expand Down Expand Up @@ -96,4 +97,12 @@ void targetPercentiles() {
assertThat(options.targetPercentiles()).hasSize(3).isEqualTo(percentiles);
}

@Test
void enabledCommands() {
CommandType[] enabledCommands = {CommandType.HSET, CommandType.HGET, CommandType.EXPIRE};
MicrometerOptions options = MicrometerOptions.builder().enabledCommands(enabledCommands).build();

assertThat(options.getEnabledCommands()).hasSize(3).containsExactly(enabledCommands);
}

}

0 comments on commit e66c148

Please sign in to comment.