Repository: grpc/grpc-go
File: clientconn.go (Go)

Code before:
		ac.mu.Unlock()
		return
	}
	// Success; reset backoff.
	ac.mu.Lock()
	connectionAttemptsSucceededMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.localityLabel)
	openConnectionsMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.securityLevel(), ac.localityLabel)
	ac.backoffIdx = 0
	ac.mu.Unlock()
}

// updateTelemetryLabelsLocked calculates and caches the telemetry labels based on the
// current addresses.
func (ac *addrConn) updateTelemetryLabelsLocked() {
	// Reset defaults
	ac.localityLabel = ""
	ac.backendServiceLabel = ""
	labelsFunc, ok := internal.AddressToTelemetryLabels.(func(resolver.Address) map[string]string)
	if !ok || len(ac.addrs) == 0 {
		return
	}
	labels := labelsFunc(ac.addrs[0])
	ac.localityLabel = labels["grpc.lb.locality"]
	ac.backendServiceLabel = labels["grpc.lb.backend_service"]
}

type securityLevelKey struct{}

func (ac *addrConn) securityLevel() string {
	var secLevel string
	// During disconnection, ac.transport is nil. Fall back to the security level
	// stored in the current address during connection.
	if ac.transport == nil {
		secLevel, _ = ac.curAddr.Attributes.Value(securityLevelKey{}).(string)
		return secLevel
	}
	authInfo := ac.transport.AuthInfo()
	if ci, ok := authInfo.(interface {
		GetCommonAuthInfo() credentials.CommonAuthInfo
	}); ok {
		secLevel = ci.GetCommonAuthInfo().SecurityLevel.String()
		// Store the security level in the current address' attributes so
		// that it remains available for disconnection metrics after the
		// transport is closed.
		ac.curAddr.Attributes = ac.curAddr.Attributes.WithValue(securityLevelKey{}, secLevel)
	}
	return secLevel
}

// tryAllAddrs tries to create a connection to the addresses, and stop when at
// the first successful one. It returns an error if no address was successfully

Review comment (suggestion):
I wonder why the `len(ac.addrs) == 0` check is required. I see that `updateTelemetryLabelsLocked` is called from two places:
- NewSubConn
- UpdateAddresses

`NewSubConn` actually verifies that we cannot have an empty address list. There is no such verification on the `UpdateAddresses` code path though. Let's start an internal discussion on this to see if we should actually allow the latter to set the addresses to an empty list.

Code after:
			timer.Stop()
		case <-acCtx.Done():
			timer.Stop()
			return
		}

		ac.mu.Lock()
		if acCtx.Err() == nil {
			ac.updateConnectivityState(connectivity.Idle, err)
		}
		ac.mu.Unlock()
		return
	}
	// Success; reset backoff.
	ac.mu.Lock()
	connectionAttemptsSucceededMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.localityLabel)
	openConnectionsMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.securityLevelLocked(), ac.localityLabel)
	ac.backoffIdx = 0
	ac.mu.Unlock()
}

// updateTelemetryLabelsLocked calculates and caches the telemetry labels based on the
// first address in addrConn.
func (ac *addrConn) updateTelemetryLabelsLocked() {
	labelsFunc, ok := internal.AddressToTelemetryLabels.(func(resolver.Address) map[string]string)
	if !ok || len(ac.addrs) == 0 {
		// Reset defaults
		ac.localityLabel = ""
		ac.backendServiceLabel = ""
		return
	}
	labels := labelsFunc(ac.addrs[0])
	ac.localityLabel = labels["grpc.lb.locality"]
	ac.backendServiceLabel = labels["grpc.lb.backend_service"]
}

type securityLevelKey struct{}

func (ac *addrConn) securityLevelLocked() string {
	var secLevel string
	// During disconnection, ac.transport is nil. Fall back to the security level
	// stored in the current address during connection.
	if ac.transport == nil {
		secLevel, _ = ac.curAddr.Attributes.Value(securityLevelKey{}).(string)
		return secLevel
	}
	authInfo := ac.transport.Peer().AuthInfo
	if ci, ok := authInfo.(interface {
		GetCommonAuthInfo() credentials.CommonAuthInfo
	}); ok {
		secLevel = ci.GetCommonAuthInfo().SecurityLevel.String()