Skip to content

Commit 926c32b

Browse files
committed
otel: plumb optional labels other than disconnect_error
1 parent 5af32b4 commit 926c32b

File tree

11 files changed

+53
-40
lines changed

11 files changed

+53
-40
lines changed

api/src/main/java/io/grpc/LoadBalancer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ public abstract class LoadBalancer {
135135
public static final Attributes.Key<Boolean> IS_PETIOLE_POLICY =
136136
Attributes.Key.create("io.grpc.IS_PETIOLE_POLICY");
137137

138+
/**
139+
* The name of the locality that this EquivalentAddressGroup is in.
140+
*/
141+
public static final Attributes.Key<String> ATTR_LOCALITY_NAME =
142+
Attributes.Key.create("io.grpc.lb.locality");
143+
138144
/**
139145
* A picker that always returns an erring pick.
140146
*

core/src/main/java/io/grpc/internal/InternalSubchannel.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
import io.grpc.Metadata;
5050
import io.grpc.MethodDescriptor;
5151
import io.grpc.MetricRecorder;
52+
import io.grpc.NameResolver;
5253
import io.grpc.Status;
54+
import io.grpc.SecurityLevel;
5355
import io.grpc.SynchronizationContext;
5456
import io.grpc.SynchronizationContext.ScheduledHandle;
5557
import java.net.SocketAddress;
@@ -163,9 +165,6 @@ protected void handleNotInUse() {
163165
private volatile Attributes connectedAddressAttributes;
164166
private final SubchannelMetrics subchannelMetrics;
165167
private final String target;
166-
private final String backendService;
167-
private final String locality;
168-
private final String securityLevel;
169168

170169
InternalSubchannel(LoadBalancer.CreateSubchannelArgs args, String authority, String userAgent,
171170
BackoffPolicy.Provider backoffPolicyProvider,
@@ -175,7 +174,7 @@ protected void handleNotInUse() {
175174
Callback callback, InternalChannelz channelz, CallTracer callsTracer,
176175
ChannelTracer channelTracer, InternalLogId logId,
177176
ChannelLogger channelLogger, List<ClientTransportFilter> transportFilters,
178-
String target, String backendService, String locality, String securityLevel,
177+
String target,
179178
MetricRecorder metricRecorder) {
180179
List<EquivalentAddressGroup> addressGroups = args.getAddresses();
181180
Preconditions.checkNotNull(addressGroups, "addressGroups");
@@ -201,9 +200,6 @@ protected void handleNotInUse() {
201200
this.transportFilters = transportFilters;
202201
this.reconnectDisabled = args.getOption(LoadBalancer.DISABLE_SUBCHANNEL_RECONNECT_KEY);
203202
this.target = target;
204-
this.backendService = backendService;
205-
this.locality = locality;
206-
this.securityLevel = securityLevel;
207203
this.subchannelMetrics = new SubchannelMetrics(metricRecorder);
208204
}
209205

@@ -592,8 +588,13 @@ public Attributes filterTransport(Attributes attributes) {
592588
@Override
593589
public void transportReady() {
594590
channelLogger.log(ChannelLogLevel.INFO, "READY");
595-
subchannelMetrics.recordConnectionAttemptSucceeded(
596-
buildLabelSet(null, extractSecurityLevel()));
591+
subchannelMetrics.recordConnectionAttemptSucceeded(buildLabelSet(
592+
addressIndex.getCurrentEagAttributes().get(NameResolver.ATTR_BACKEND_SERVICE),
593+
addressIndex.getCurrentEagAttributes().get(LoadBalancer.ATTR_LOCALITY_NAME),
594+
null,
595+
extractSecurityLevel(
596+
addressIndex.getCurrentEagAttributes().get(GrpcAttributes.ATTR_SECURITY_LEVEL))
597+
));
597598
syncContext.execute(new Runnable() {
598599
@Override
599600
public void run() {
@@ -623,7 +624,11 @@ public void transportShutdown(final Status s) {
623624
channelLogger.log(
624625
ChannelLogLevel.INFO, "{0} SHUTDOWN with {1}", transport.getLogId(), printShortStatus(s));
625626
shutdownInitiated = true;
626-
subchannelMetrics.recordConnectionAttemptFailed(buildLabelSet("Peer Pressure", null));
627+
subchannelMetrics.recordConnectionAttemptFailed(buildLabelSet(
628+
addressIndex.getCurrentEagAttributes().get(NameResolver.ATTR_BACKEND_SERVICE),
629+
addressIndex.getCurrentEagAttributes().get(LoadBalancer.ATTR_LOCALITY_NAME),
630+
null, null
631+
));
627632
syncContext.execute(new Runnable() {
628633
@Override
629634
public void run() {
@@ -664,8 +669,13 @@ public void transportTerminated() {
664669
for (ClientTransportFilter filter : transportFilters) {
665670
filter.transportTerminated(transport.getAttributes());
666671
}
667-
subchannelMetrics.recordDisconnection(buildLabelSet("Peer Pressure",
668-
null));
672+
subchannelMetrics.recordDisconnection(buildLabelSet(
673+
addressIndex.getCurrentEagAttributes().get(NameResolver.ATTR_BACKEND_SERVICE),
674+
addressIndex.getCurrentEagAttributes().get(LoadBalancer.ATTR_LOCALITY_NAME),
675+
"Peer Pressure",
676+
extractSecurityLevel(
677+
addressIndex.getCurrentEagAttributes().get(GrpcAttributes.ATTR_SECURITY_LEVEL))
678+
));
669679
syncContext.execute(new Runnable() {
670680
@Override
671681
public void run() {
@@ -677,8 +687,20 @@ public void run() {
677687
});
678688
}
679689

680-
private String extractSecurityLevel() {
681-
return "Hold the door!";
690+
private String extractSecurityLevel(SecurityLevel securityLevel) {
691+
if (securityLevel == null) {
692+
return "none";
693+
}
694+
switch (securityLevel) {
695+
case NONE:
696+
return "none";
697+
case INTEGRITY:
698+
return "integrity_only";
699+
case PRIVACY_AND_INTEGRITY:
700+
return "privacy_and_integrity";
701+
default:
702+
throw new IllegalArgumentException("Unknown SecurityLevel: " + securityLevel);
703+
}
682704
}
683705
}
684706

@@ -839,13 +861,14 @@ private String printShortStatus(Status status) {
839861
return buffer.toString();
840862
}
841863

842-
private OtelMetricsAttributes buildLabelSet(String disconnectError, String secLevel) {
864+
private OtelMetricsAttributes buildLabelSet(String backendService, String locality,
865+
String disconnectError, String securityLevel) {
843866
return new OtelMetricsAttributes(
844867
target,
845868
backendService,
846869
locality,
847870
disconnectError,
848-
secLevel != null ? secLevel : securityLevel
871+
securityLevel
849872
);
850873
}
851874

core/src/main/java/io/grpc/internal/ManagedChannelImpl.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void exitIdleMode() {
415415
LbHelperImpl lbHelper = new LbHelperImpl();
416416
lbHelper.lb = loadBalancerFactory.newLoadBalancer(lbHelper);
417417
// Delay setting lbHelper until fully initialized, since loadBalancerFactory is user code and
418-
// may throw. We don't want to confuse our state, even if we will enter panic mode.
418+
// may throw. We don't want to confuse our state, even if we enter panic mode.
419419
this.lbHelper = lbHelper;
420420

421421
channelStateManager.gotoState(CONNECTING);
@@ -1466,9 +1466,6 @@ void onStateChange(InternalSubchannel is, ConnectivityStateInfo newState) {
14661466
subchannelLogger,
14671467
transportFilters,
14681468
target,
1469-
"",
1470-
"",
1471-
"",
14721469
lbHelper.getMetricRecorder());
14731470
oobChannelTracer.reportEvent(new ChannelTrace.Event.Builder()
14741471
.setDescription("Child Subchannel created")
@@ -1901,9 +1898,6 @@ void onNotInUse(InternalSubchannel is) {
19011898
subchannelLogId,
19021899
subchannelLogger,
19031900
transportFilters, target,
1904-
"",
1905-
"",
1906-
"",
19071901
lbHelper.getMetricRecorder());
19081902

19091903
channelTracer.reportEvent(new ChannelTrace.Event.Builder()

core/src/main/java/io/grpc/internal/PickFirstLeafLoadBalancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
9292
return Status.FAILED_PRECONDITION.withDescription("Already shut down");
9393
}
9494

95-
// Cache whether or not this is a petiole policy, which is based off of an address attribute
95+
// Check weather or not this is a petiole policy, which is based off of an address attribute
9696
Boolean isPetiolePolicy = resolvedAddresses.getAttributes().get(IS_PETIOLE_POLICY);
9797
this.notAPetiolePolicy = isPetiolePolicy == null || !isPetiolePolicy;
9898

core/src/test/java/io/grpc/internal/InternalSubchannelTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,9 +1449,6 @@ private void createInternalSubchannel(boolean reconnectDisabled,
14491449
new ChannelLoggerImpl(subchannelTracer, fakeClock.getTimeProvider()),
14501450
Collections.emptyList(),
14511451
"",
1452-
"",
1453-
"",
1454-
"",
14551452
new MetricRecorder() {}
14561453
);
14571454
}

xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private List<EquivalentAddressGroup> withAdditionalAttributes(
305305

306306
private ClusterLocality createClusterLocalityFromAttributes(Attributes addressAttributes) {
307307
Locality locality = addressAttributes.get(XdsAttributes.ATTR_LOCALITY);
308-
String localityName = addressAttributes.get(XdsAttributes.ATTR_LOCALITY_NAME);
308+
String localityName = addressAttributes.get(LoadBalancer.ATTR_LOCALITY_NAME);
309309

310310
// Endpoint addresses resolved by ClusterResolverLoadBalancer should always contain
311311
// attributes with its locality, including endpoints in LOGICAL_DNS clusters.

xds/src/main/java/io/grpc/xds/ClusterResolverLoadBalancer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public void run() {
428428
Attributes attr =
429429
endpoint.eag().getAttributes().toBuilder()
430430
.set(XdsAttributes.ATTR_LOCALITY, locality)
431-
.set(XdsAttributes.ATTR_LOCALITY_NAME, localityName)
431+
.set(LoadBalancer.ATTR_LOCALITY_NAME, localityName)
432432
.set(XdsAttributes.ATTR_LOCALITY_WEIGHT,
433433
localityLbInfo.localityWeight())
434434
.set(XdsAttributes.ATTR_SERVER_WEIGHT, weight)
@@ -679,7 +679,7 @@ public Status onResult2(final ResolutionResult resolutionResult) {
679679
String localityName = localityName(LOGICAL_DNS_CLUSTER_LOCALITY);
680680
Attributes attr = eag.getAttributes().toBuilder()
681681
.set(XdsAttributes.ATTR_LOCALITY, LOGICAL_DNS_CLUSTER_LOCALITY)
682-
.set(XdsAttributes.ATTR_LOCALITY_NAME, localityName)
682+
.set(LoadBalancer.ATTR_LOCALITY_NAME, localityName)
683683
.set(XdsAttributes.ATTR_ADDRESS_NAME, dnsHostName)
684684
.build();
685685
eag = new EquivalentAddressGroup(eag.getAddresses(), attr);

xds/src/main/java/io/grpc/xds/WrrLocalityLoadBalancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
7474
Map<String, Integer> localityWeights = new HashMap<>();
7575
for (EquivalentAddressGroup eag : resolvedAddresses.getAddresses()) {
7676
Attributes eagAttrs = eag.getAttributes();
77-
String locality = eagAttrs.get(XdsAttributes.ATTR_LOCALITY_NAME);
77+
String locality = eagAttrs.get(LoadBalancer.ATTR_LOCALITY_NAME);
7878
Integer localityWeight = eagAttrs.get(XdsAttributes.ATTR_LOCALITY_WEIGHT);
7979

8080
if (locality == null) {

xds/src/main/java/io/grpc/xds/XdsAttributes.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ final class XdsAttributes {
8181
static final Attributes.Key<Locality> ATTR_LOCALITY =
8282
Attributes.Key.create("io.grpc.xds.XdsAttributes.locality");
8383

84-
/**
85-
* The name of the locality that this EquivalentAddressGroup is in.
86-
*/
87-
@EquivalentAddressGroup.Attr
88-
static final Attributes.Key<String> ATTR_LOCALITY_NAME =
89-
Attributes.Key.create("io.grpc.xds.XdsAttributes.localityName");
90-
9184
/**
9285
* Endpoint weight for load balancing purposes.
9386
*/

xds/src/test/java/io/grpc/xds/ClusterImplLoadBalancerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ public String toString() {
10171017
Attributes.Builder attributes = Attributes.newBuilder()
10181018
.set(XdsAttributes.ATTR_LOCALITY, locality)
10191019
// Unique but arbitrary string
1020-
.set(XdsAttributes.ATTR_LOCALITY_NAME, locality.toString());
1020+
.set(LoadBalancer.ATTR_LOCALITY_NAME, locality.toString());
10211021
if (authorityHostname != null) {
10221022
attributes.set(XdsAttributes.ATTR_ADDRESS_NAME, authorityHostname);
10231023
}

0 commit comments

Comments
 (0)