|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2024 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 protoconv |
| 20 | + |
| 21 | +import ( |
| 22 | + "net" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/golang/protobuf/ptypes" |
| 26 | + "google.golang.org/grpc/codes" |
| 27 | + "google.golang.org/grpc/credentials" |
| 28 | + "google.golang.org/grpc/internal/channelz" |
| 29 | + "google.golang.org/grpc/status" |
| 30 | + "google.golang.org/protobuf/protoadapt" |
| 31 | + "google.golang.org/protobuf/types/known/anypb" |
| 32 | + |
| 33 | + wrpb "github.com/golang/protobuf/ptypes/wrappers" |
| 34 | + channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" |
| 35 | +) |
| 36 | + |
| 37 | +func securityToProto(se credentials.ChannelzSecurityValue) *channelzpb.Security { |
| 38 | + switch v := se.(type) { |
| 39 | + case *credentials.TLSChannelzSecurityValue: |
| 40 | + return &channelzpb.Security{Model: &channelzpb.Security_Tls_{Tls: &channelzpb.Security_Tls{ |
| 41 | + CipherSuite: &channelzpb.Security_Tls_StandardName{StandardName: v.StandardName}, |
| 42 | + LocalCertificate: v.LocalCertificate, |
| 43 | + RemoteCertificate: v.RemoteCertificate, |
| 44 | + }}} |
| 45 | + case *credentials.OtherChannelzSecurityValue: |
| 46 | + otherSecurity := &channelzpb.Security_OtherSecurity{ |
| 47 | + Name: v.Name, |
| 48 | + } |
| 49 | + if anyval, err := anypb.New(protoadapt.MessageV2Of(v.Value)); err == nil { |
| 50 | + otherSecurity.Value = anyval |
| 51 | + } |
| 52 | + return &channelzpb.Security{Model: &channelzpb.Security_Other{Other: otherSecurity}} |
| 53 | + } |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func addrToProto(a net.Addr) *channelzpb.Address { |
| 58 | + if a == nil { |
| 59 | + return nil |
| 60 | + } |
| 61 | + switch a.Network() { |
| 62 | + case "udp": |
| 63 | + // TODO: Address_OtherAddress{}. Need proto def for Value. |
| 64 | + case "ip": |
| 65 | + // Note zone info is discarded through the conversion. |
| 66 | + return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: a.(*net.IPAddr).IP}}} |
| 67 | + case "ip+net": |
| 68 | + // Note mask info is discarded through the conversion. |
| 69 | + return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: a.(*net.IPNet).IP}}} |
| 70 | + case "tcp": |
| 71 | + // Note zone info is discarded through the conversion. |
| 72 | + return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: a.(*net.TCPAddr).IP, Port: int32(a.(*net.TCPAddr).Port)}}} |
| 73 | + case "unix", "unixgram", "unixpacket": |
| 74 | + return &channelzpb.Address{Address: &channelzpb.Address_UdsAddress_{UdsAddress: &channelzpb.Address_UdsAddress{Filename: a.String()}}} |
| 75 | + default: |
| 76 | + } |
| 77 | + return &channelzpb.Address{} |
| 78 | +} |
| 79 | + |
| 80 | +func socketToProto(skt *channelz.Socket) *channelzpb.Socket { |
| 81 | + s := &channelzpb.Socket{} |
| 82 | + s.Ref = &channelzpb.SocketRef{SocketId: skt.ID, Name: skt.RefName} |
| 83 | + |
| 84 | + s.Data = &channelzpb.SocketData{ |
| 85 | + StreamsStarted: skt.SocketMetrics.StreamsStarted.Load(), |
| 86 | + StreamsSucceeded: skt.SocketMetrics.StreamsSucceeded.Load(), |
| 87 | + StreamsFailed: skt.SocketMetrics.StreamsFailed.Load(), |
| 88 | + MessagesSent: skt.SocketMetrics.MessagesSent.Load(), |
| 89 | + MessagesReceived: skt.SocketMetrics.MessagesReceived.Load(), |
| 90 | + KeepAlivesSent: skt.SocketMetrics.KeepAlivesSent.Load(), |
| 91 | + } |
| 92 | + if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastLocalStreamCreatedTimestamp.Load())); err == nil { |
| 93 | + s.Data.LastLocalStreamCreatedTimestamp = ts |
| 94 | + } |
| 95 | + if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastRemoteStreamCreatedTimestamp.Load())); err == nil { |
| 96 | + s.Data.LastRemoteStreamCreatedTimestamp = ts |
| 97 | + } |
| 98 | + if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastMessageSentTimestamp.Load())); err == nil { |
| 99 | + s.Data.LastMessageSentTimestamp = ts |
| 100 | + } |
| 101 | + if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastMessageReceivedTimestamp.Load())); err == nil { |
| 102 | + s.Data.LastMessageReceivedTimestamp = ts |
| 103 | + } |
| 104 | + if skt.EphemeralMetrics != nil { |
| 105 | + e := skt.EphemeralMetrics() |
| 106 | + s.Data.LocalFlowControlWindow = &wrpb.Int64Value{Value: e.LocalFlowControlWindow} |
| 107 | + s.Data.RemoteFlowControlWindow = &wrpb.Int64Value{Value: e.RemoteFlowControlWindow} |
| 108 | + } |
| 109 | + |
| 110 | + s.Data.Option = sockoptToProto(skt.SocketOptions) |
| 111 | + s.Security = securityToProto(skt.Security) |
| 112 | + s.Local = addrToProto(skt.LocalAddr) |
| 113 | + s.Remote = addrToProto(skt.RemoteAddr) |
| 114 | + s.RemoteName = skt.RemoteName |
| 115 | + return s |
| 116 | +} |
| 117 | + |
| 118 | +// GetServerSockets returns the protobuf representation of the server (listen) |
| 119 | +// sockets starting at startID (max of len), and returns end=true if no server |
| 120 | +// sockets exist with higher IDs. |
| 121 | +func GetServerSockets(serverID, startID int64, len int) (sockets []*channelzpb.SocketRef, end bool) { |
| 122 | + skts, end := channelz.GetServerSockets(serverID, startID, len) |
| 123 | + for _, m := range skts { |
| 124 | + sockets = append(sockets, &channelzpb.SocketRef{SocketId: m.ID, Name: m.RefName}) |
| 125 | + } |
| 126 | + return sockets, end |
| 127 | +} |
| 128 | + |
| 129 | +// GetSocket returns the protobuf representation of the socket with the given |
| 130 | +// ID. |
| 131 | +func GetSocket(id int64) (*channelzpb.Socket, error) { |
| 132 | + skt := channelz.GetSocket(id) |
| 133 | + if skt == nil { |
| 134 | + return nil, status.Errorf(codes.NotFound, "requested socket %d not found", id) |
| 135 | + } |
| 136 | + return socketToProto(skt), nil |
| 137 | +} |
0 commit comments