channelz: fix missing Target on SubChannel type (#7189)

This commit is contained in:
Doug Fawley 2024-05-03 10:50:09 -07:00 committed by GitHub
parent 273fe145d0
commit a87e923c4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View File

@ -842,6 +842,9 @@ func (cc *ClientConn) newAddrConnLocked(addrs []resolver.Address, opts balancer.
stateChan: make(chan struct{}),
}
ac.ctx, ac.cancel = context.WithCancel(cc.ctx)
// Start with our address set to the first address; this may be updated if
// we connect to different addresses.
ac.channelz.ChannelMetrics.Target.Store(&addrs[0].Addr)
channelz.AddTraceEvent(logger, ac.channelz, 0, &channelz.TraceEvent{
Desc: "Subchannel created",
@ -1304,6 +1307,7 @@ func (ac *addrConn) resetTransport() {
func (ac *addrConn) tryAllAddrs(ctx context.Context, addrs []resolver.Address, connectDeadline time.Time) error {
var firstConnErr error
for _, addr := range addrs {
ac.channelz.ChannelMetrics.Target.Store(&addr.Addr)
if ctx.Err() != nil {
return errConnClosing
}

View File

@ -158,6 +158,10 @@ func (s) TestCZGetSubChannel(t *testing.T) {
if sc == nil {
return false, fmt.Errorf("subchannel with id %v is nil", scid)
}
target := sc.ChannelMetrics.Target.Load()
if target == nil || !strings.HasPrefix(*target, "localhost") {
t.Fatalf("subchannel target must never be set incorrectly; got: %v, want <HasPrefix('localhost')>", target)
}
state := sc.ChannelMetrics.State.Load()
if state == nil || *state != connectivity.Ready {
return false, fmt.Errorf("Got subchannel state=%v; want %q", state, connectivity.Ready)