Skip to content

Commit

Permalink
rls: Add the target label to RLS counter metrics (#11142)
Browse files Browse the repository at this point in the history
  • Loading branch information
temawi committed May 1, 2024
1 parent 35a171b commit a1d1932
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
30 changes: 15 additions & 15 deletions rls/src/main/java/io/grpc/rls/CachingRlsLbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Ticker;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
Expand Down Expand Up @@ -61,6 +60,8 @@
import io.grpc.util.ForwardingLoadBalancerHelper;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,16 +128,16 @@ final class CachingRlsLbClient {
= MetricInstrumentRegistry.getDefaultRegistry();
DEFAULT_TARGET_PICKS_COUNTER = metricInstrumentRegistry.registerLongCounter(
"grpc.lb.rls.default_target_picks", "Number of LB picks sent to the default target", "pick",
Lists.newArrayList("grpc.target", "grpc.lb.rls.server_target",
"grpc.lb.rls.data_plane_target", "grpc.lb.pick_result"), Lists.newArrayList(), true);
Arrays.asList("grpc.target", "grpc.lb.rls.server_target",
"grpc.lb.rls.data_plane_target", "grpc.lb.pick_result"), Collections.emptyList(), true);
TARGET_PICKS_COUNTER = metricInstrumentRegistry.registerLongCounter("grpc.lb.rls.target_picks",
"Number of LB picks sent to each RLS target", "pick",
Lists.newArrayList("grpc.target", "grpc.lb.rls.server_target",
"grpc.lb.rls.data_plane_target", "grpc.lb.pick_result"), Lists.newArrayList(), true);
Arrays.asList("grpc.target", "grpc.lb.rls.server_target",
"grpc.lb.rls.data_plane_target", "grpc.lb.pick_result"), Collections.emptyList(), true);
FAILED_PICKS_COUNTER = metricInstrumentRegistry.registerLongCounter("grpc.lb.rls.failed_picks",
"Number of LB picks failed due to either a failed RLS request or the RLS channel being "
+ "throttled", "pick", Lists.newArrayList("grpc.target", "grpc.lb.rls.server_target"),
Lists.newArrayList(), true);
+ "throttled", "pick", Arrays.asList("grpc.target", "grpc.lb.rls.server_target"),
Collections.emptyList(), true);
}

private CachingRlsLbClient(Builder builder) {
Expand Down Expand Up @@ -968,11 +969,11 @@ public PickResult pickSubchannel(PickSubchannelArgs args) {
// Happy path
logger.log(ChannelLogLevel.DEBUG, "Returning PickResult");
PickResult pickResult = picker.pickSubchannel(args);
// TODO: include the "grpc.target" label once target is available here.
if (pickResult.hasResult()) {
helper.getMetricRecorder().addLongCounter(TARGET_PICKS_COUNTER, 1,
Lists.newArrayList("", lookupService, childPolicyWrapper.getTarget(),
determineMetricsPickResult(pickResult)), Lists.newArrayList());
Arrays.asList(helper.getChannelTarget(), lookupService,
childPolicyWrapper.getTarget(), determineMetricsPickResult(pickResult)),
Collections.emptyList());
}
return pickResult;
} else if (response.hasError()) {
Expand All @@ -982,9 +983,8 @@ public PickResult pickSubchannel(PickSubchannelArgs args) {
return useFallback(args);
}
logger.log(ChannelLogLevel.DEBUG, "No RLS fallback, returning PickResult with an error");
// TODO: include the "grpc.target" label once target is available here.
helper.getMetricRecorder().addLongCounter(FAILED_PICKS_COUNTER, 1,
Lists.newArrayList("", lookupService), Lists.newArrayList());
Arrays.asList(helper.getChannelTarget(), lookupService), Collections.emptyList());
return PickResult.withError(
convertRlsServerStatus(response.getStatus(),
lbPolicyConfig.getRouteLookupConfig().lookupService()));
Expand All @@ -1007,10 +1007,10 @@ private PickResult useFallback(PickSubchannelArgs args) {
}
PickResult pickResult = picker.pickSubchannel(args);
if (pickResult.hasResult()) {
// TODO: include the grpc.target label once target is available here.
helper.getMetricRecorder().addLongCounter(DEFAULT_TARGET_PICKS_COUNTER, 1,
Lists.newArrayList("", lookupService, fallbackChildPolicyWrapper.getTarget(),
determineMetricsPickResult(pickResult)), Lists.newArrayList());
Arrays.asList(helper.getChannelTarget(), lookupService,
fallbackChildPolicyWrapper.getTarget(), determineMetricsPickResult(pickResult)),
Collections.emptyList());
}
return pickResult;
}
Expand Down
5 changes: 5 additions & 0 deletions rls/src/test/java/io/grpc/rls/CachingRlsLbClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,11 @@ public ChannelLogger getChannelLogger() {
public MetricRecorder getMetricRecorder() {
return mockMetricRecorder;
}

@Override
public String getChannelTarget() {
return "channelTarget";
}
}

private static final class FakeThrottler implements Throttler {
Expand Down
14 changes: 12 additions & 2 deletions rls/src/test/java/io/grpc/rls/RlsLoadBalancerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void uncaughtException(Thread t, Throwable e) {
private final FakeRlsServerImpl fakeRlsServerImpl = new FakeRlsServerImpl();
private final Deque<FakeSubchannel> subchannels = new LinkedList<>();
private final FakeThrottler fakeThrottler = new FakeThrottler();
private final String channelTarget = "channelTarget";
@Mock
private Marshaller<Object> mockMarshaller;
@Captor
Expand Down Expand Up @@ -296,6 +297,7 @@ public void lb_working_withoutDefaultTarget_noRlsResponse() throws Exception {
PickResult res = picker.pickSubchannel(searchSubchannelArgs); // create subchannel
assertThat(res.getStatus().getCode()).isEqualTo(Code.UNAVAILABLE);
inOrder.verify(helper).getMetricRecorder();
inOrder.verify(helper).getChannelTarget();
inOrder.verifyNoMoreInteractions();
verifyFailedPicksCounterAdd(1, 1);
verifyNoMoreInteractions(mockMetricRecorder);
Expand All @@ -319,6 +321,7 @@ public void lb_working_withDefaultTarget_noRlsResponse() throws Exception {
assertThat(fallbackSubchannel).isNotNull();
assertThat(subchannelIsReady(fallbackSubchannel)).isTrue();
inOrder.verify(helper).getMetricRecorder();
inOrder.verify(helper).getChannelTarget();
inOrder.verifyNoMoreInteractions();
verifyLongCounterAdd("grpc.lb.rls.default_target_picks", 1, 1, "defaultTarget", "complete");
verifyNoMoreInteractions(mockMetricRecorder);
Expand Down Expand Up @@ -393,6 +396,7 @@ public void lb_working_withoutDefaultTarget() throws Exception {
FakeSubchannel searchSubchannel =
(FakeSubchannel) markReadyAndGetPickResult(inOrder, searchSubchannelArgs).getSubchannel();
inOrder.verify(helper).getMetricRecorder();
inOrder.verify(helper).getChannelTarget();
inOrder.verifyNoMoreInteractions();
assertThat(subchannelIsReady(searchSubchannel)).isTrue();
assertThat(subchannels.getLast()).isSameInstanceAs(searchSubchannel);
Expand Down Expand Up @@ -468,6 +472,7 @@ public void lb_nameResolutionFailed() throws Exception {
verifyLongCounterAdd("grpc.lb.rls.target_picks", 1, 1, "wilderness", "complete");

inOrder.verify(helper).getMetricRecorder();
inOrder.verify(helper).getChannelTarget();
inOrder.verifyNoMoreInteractions();

rlsLb.handleNameResolutionError(Status.UNAVAILABLE);
Expand Down Expand Up @@ -559,7 +564,7 @@ public boolean matches(LongCounterMetricInstrument longCounterInstrument) {
return longCounterInstrument.getName().equals(name);
}
}), eq(value),
eq(Lists.newArrayList("", "localhost:8972", dataPlaneTargetLabel, pickResult)),
eq(Lists.newArrayList(channelTarget, "localhost:8972", dataPlaneTargetLabel, pickResult)),
eq(Lists.newArrayList()));
}

Expand All @@ -573,7 +578,7 @@ public boolean matches(LongCounterMetricInstrument longCounterInstrument) {
return longCounterInstrument.getName().equals("grpc.lb.rls.failed_picks");
}
}), eq(value),
eq(Lists.newArrayList("", "localhost:8972")),
eq(Lists.newArrayList(channelTarget, "localhost:8972")),
eq(Lists.newArrayList()));
}

Expand Down Expand Up @@ -675,6 +680,11 @@ public ChannelLogger getChannelLogger() {
public MetricRecorder getMetricRecorder() {
return mockMetricRecorder;
}

@Override
public String getChannelTarget() {
return channelTarget;
}
}

private static final class FakeRlsServerImpl
Expand Down

0 comments on commit a1d1932

Please sign in to comment.