Repository: grpc/grpc-go
File: internal/xds/xds.go (Go)

Code before:
	"google.golang.org/grpc/resolver"
)

// handshakeClusterNameKey is the type used as the key to store cluster name in
// the Attributes field of resolver.Address.
type handshakeClusterNameKey struct{}

// SetXDSHandshakeClusterName returns a copy of addr in which the Attributes field
// is updated with the cluster name.
func SetXDSHandshakeClusterName(addr resolver.Address, clusterName string) resolver.Address {
	addr.Attributes = addr.Attributes.WithValue(handshakeClusterNameKey{}, clusterName)
	return addr
}

// GetXDSHandshakeClusterName returns cluster name stored in attr.
func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) {
	v := attr.Value(handshakeClusterNameKey{})
	name, ok := v.(string)
	return name, ok
}

// AddressToTelemetryLabels prepares a telemetry label map from resolver
// address atrributes.
func AddressToTelemetryLabels(addr resolver.Address) map[string]string {
	cluster, _ := GetXDSHandshakeClusterName(addr.Attributes)
	locality := LocalityString(GetLocalityID(addr))
	labels := make(map[string]string)
	labels["grpc.lb.locality"] = locality
	labels["grpc.lb.backend_service"] = cluster
	return labels
}

// LocalityString generates a string representation of clients.Locality in the
// format specified in gRFC A76.
func LocalityString(l clients.Locality) string {
	return fmt.Sprintf("{region=%q, zone=%q, sub_zone=%q}", l.Region, l.Zone, l.SubZone)
}

// IsLocalityEqual allows the values to be compared by Attributes.Equal.
func IsLocalityEqual(l clients.Locality, o any) bool {
	ol, ok := o.(clients.Locality)
	if !ok {
		return false
	}
	return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone
}

// LocalityFromString converts a string representation of clients.locality as
// specified in gRFC A76, into a LocalityID struct.
func LocalityFromString(s string) (ret clients.Locality, _ error) {
	_, err := fmt.Sscanf(s, "{region=%q, zone=%q, sub_zone=%q}", &ret.Region, &ret.Zone, &ret.SubZone)

Review comment (suggestion):

Who sets the locality attribute on the address? I see that there is a `SetLocality` function, but there are no callers of it.

Code after:
	"google.golang.org/grpc/resolver"
)

// handshakeClusterNameKey is the type used as the key to store cluster name in
// the Attributes field of resolver.Address.
type handshakeClusterNameKey struct{}

// SetXDSHandshakeClusterName returns a copy of addr in which the Attributes field
// is updated with the cluster name.
func SetXDSHandshakeClusterName(addr resolver.Address, clusterName string) resolver.Address {
	addr.Attributes = addr.Attributes.WithValue(handshakeClusterNameKey{}, clusterName)
	return addr
}

// GetXDSHandshakeClusterName returns cluster name stored in attr.
func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) {
	v := attr.Value(handshakeClusterNameKey{})
	name, ok := v.(string)
	return name, ok
}

// addressToTelemetryLabels prepares a telemetry label map from resolver
// address atrributes.
func addressToTelemetryLabels(addr resolver.Address) map[string]string {
	cluster, _ := GetXDSHandshakeClusterName(addr.Attributes)
	locality := LocalityString(GetLocalityID(addr))
	return map[string]string{
		"grpc.lb.locality":        locality,
		"grpc.lb.backend_service": cluster,
	}
}

// LocalityString generates a string representation of clients.Locality in the
// format specified in gRFC A76.
func LocalityString(l clients.Locality) string {
	return fmt.Sprintf("{region=%q, zone=%q, sub_zone=%q}", l.Region, l.Zone, l.SubZone)
}

// IsLocalityEqual allows the values to be compared by Attributes.Equal.
func IsLocalityEqual(l clients.Locality, o any) bool {
	ol, ok := o.(clients.Locality)
	if !ok {
		return false
	}
	return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone
}

// LocalityFromString converts a string representation of clients.locality as
// specified in gRFC A76, into a LocalityID struct.
func LocalityFromString(s string) (ret clients.Locality, _ error) {
	_, err := fmt.Sscanf(s, "{region=%q, zone=%q, sub_zone=%q}", &ret.Region, &ret.Zone, &ret.SubZone)