Skip to content

Commit a73725f

Browse files
authored
channelz: include channelz identifier in logs (#5192)
1 parent 02f384d commit a73725f

24 files changed

+640
-411
lines changed

balancer/balancer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"net"
2828
"strings"
2929

30+
"google.golang.org/grpc/channelz"
3031
"google.golang.org/grpc/connectivity"
3132
"google.golang.org/grpc/credentials"
3233
"google.golang.org/grpc/internal"
@@ -192,7 +193,7 @@ type BuildOptions struct {
192193
// server can ignore this field.
193194
Authority string
194195
// ChannelzParentID is the parent ClientConn's channelz ID.
195-
ChannelzParentID int64
196+
ChannelzParentID *channelz.Identifier
196197
// CustomUserAgent is the custom user agent set on the parent ClientConn.
197198
// The balancer should set the same custom user agent if it creates a
198199
// ClientConn.

balancer/grpclb/grpclb_remote_balancer.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"google.golang.org/grpc/connectivity"
3636
"google.golang.org/grpc/credentials/insecure"
3737
"google.golang.org/grpc/internal/backoff"
38-
"google.golang.org/grpc/internal/channelz"
3938
imetadata "google.golang.org/grpc/internal/metadata"
4039
"google.golang.org/grpc/keepalive"
4140
"google.golang.org/grpc/metadata"
@@ -240,9 +239,7 @@ func (lb *lbBalancer) newRemoteBalancerCCWrapper() {
240239
// Explicitly set pickfirst as the balancer.
241240
dopts = append(dopts, grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy":"pick_first"}`))
242241
dopts = append(dopts, grpc.WithResolvers(lb.manualResolver))
243-
if channelz.IsOn() {
244-
dopts = append(dopts, grpc.WithChannelzParentID(lb.opt.ChannelzParentID))
245-
}
242+
dopts = append(dopts, grpc.WithChannelzParentID(lb.opt.ChannelzParentID))
246243

247244
// Enable Keepalive for grpclb client.
248245
dopts = append(dopts, grpc.WithKeepaliveParams(keepalive.ClientParameters{

balancer_conn_wrappers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer
184184
}
185185
ac, err := ccb.cc.newAddrConn(addrs, opts)
186186
if err != nil {
187+
channelz.Warningf(logger, ccb.cc.channelzID, "acBalancerWrapper: NewSubConn: failed to newAddrConn: %v", err)
187188
return nil, err
188189
}
189190
acbw := &acBalancerWrapper{ac: ac}

call_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"time"
3232

3333
"google.golang.org/grpc/codes"
34+
"google.golang.org/grpc/internal/channelz"
3435
"google.golang.org/grpc/internal/transport"
3536
"google.golang.org/grpc/status"
3637
)
@@ -123,12 +124,16 @@ type server struct {
123124
startedErr chan error // sent nil or an error after server starts
124125
mu sync.Mutex
125126
conns map[transport.ServerTransport]bool
127+
channelzID *channelz.Identifier
126128
}
127129

128130
type ctxKey string
129131

130132
func newTestServer() *server {
131-
return &server{startedErr: make(chan error, 1)}
133+
return &server{
134+
startedErr: make(chan error, 1),
135+
channelzID: channelz.NewIdentifierForTesting(channelz.RefServer, time.Now().Unix(), nil),
136+
}
132137
}
133138

134139
// start starts server. Other goroutines should block on s.startedErr for further operations.
@@ -158,10 +163,12 @@ func (s *server) start(t *testing.T, port int, maxStreams uint32) {
158163
return
159164
}
160165
config := &transport.ServerConfig{
161-
MaxStreams: maxStreams,
166+
MaxStreams: maxStreams,
167+
ChannelzParentID: s.channelzID,
162168
}
163169
st, err := transport.NewServerTransport(conn, config)
164170
if err != nil {
171+
t.Errorf("failed to create server transport: %v", err)
165172
continue
166173
}
167174
s.mu.Lock()

channelz/channelz.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* Copyright 2020 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
// Package channelz exports internals of the channelz implementation as required
20+
// by other gRPC packages.
21+
//
22+
// The implementation of the channelz spec as defined in
23+
// https://github.com/grpc/proposal/blob/master/A14-channelz.md, is provided by
24+
// the `internal/channelz` package.
25+
//
26+
// Experimental
27+
//
28+
// Notice: All APIs in this package are experimental and may be removed in a
29+
// later release.
30+
package channelz
31+
32+
import "google.golang.org/grpc/internal/channelz"
33+
34+
// Identifier is an opaque identifier which uniquely identifies an entity in the
35+
// channelz database.
36+
type Identifier = channelz.Identifier

channelz/service/service_sktopt_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ package service
2828

2929
import (
3030
"context"
31-
"reflect"
3231
"strconv"
3332
"testing"
3433

3534
"github.com/golang/protobuf/ptypes"
3635
durpb "github.com/golang/protobuf/ptypes/duration"
36+
"github.com/google/go-cmp/cmp"
37+
"github.com/google/go-cmp/cmp/cmpopts"
3738
"golang.org/x/sys/unix"
3839
channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
3940
"google.golang.org/grpc/internal/channelz"
41+
"google.golang.org/protobuf/testing/protocmp"
4042
)
4143

4244
func init() {
@@ -139,20 +141,27 @@ func (s) TestGetSocketOptions(t *testing.T) {
139141
},
140142
}
141143
svr := newCZServer()
142-
ids := make([]int64, len(ss))
144+
ids := make([]*channelz.Identifier, len(ss))
143145
svrID := channelz.RegisterServer(&dummyServer{}, "")
144146
defer channelz.RemoveEntry(svrID)
145147
for i, s := range ss {
146-
ids[i] = channelz.RegisterNormalSocket(s, svrID, strconv.Itoa(i))
148+
ids[i], _ = channelz.RegisterNormalSocket(s, svrID, strconv.Itoa(i))
147149
defer channelz.RemoveEntry(ids[i])
148150
}
149151
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
150152
defer cancel()
151153
for i, s := range ss {
152-
resp, _ := svr.GetSocket(ctx, &channelzpb.GetSocketRequest{SocketId: ids[i]})
153-
metrics := resp.GetSocket()
154-
if !reflect.DeepEqual(metrics.GetRef(), &channelzpb.SocketRef{SocketId: ids[i], Name: strconv.Itoa(i)}) || !reflect.DeepEqual(socketProtoToStruct(metrics), s) {
155-
t.Fatalf("resp.GetSocket() want: metrics.GetRef() = %#v and %#v, got: metrics.GetRef() = %#v and %#v", &channelzpb.SocketRef{SocketId: ids[i], Name: strconv.Itoa(i)}, s, metrics.GetRef(), socketProtoToStruct(metrics))
154+
resp, _ := svr.GetSocket(ctx, &channelzpb.GetSocketRequest{SocketId: ids[i].Int()})
155+
got, want := resp.GetSocket().GetRef(), &channelzpb.SocketRef{SocketId: ids[i].Int(), Name: strconv.Itoa(i)}
156+
if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(channelzpb.SocketRef{})) {
157+
t.Fatalf("resp.GetSocket() returned metrics.GetRef() = %#v, want %#v", got, want)
158+
}
159+
socket, err := socketProtoToStruct(resp.GetSocket())
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
if diff := cmp.Diff(s, socket, protocmp.Transform(), cmp.AllowUnexported(dummySocket{})); diff != "" {
164+
t.Fatalf("unexpected socket, diff (-want +got):\n%s", diff)
156165
}
157166
}
158167
}

0 commit comments

Comments
 (0)