1# Copyright 2020 The gRPC Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Channelz debug service implementation in gRPC Python.""" 15 16import grpc 17from grpc._cython import cygrpc 18 19import grpc_channelz.v1.channelz_pb2 as _channelz_pb2 20import grpc_channelz.v1.channelz_pb2_grpc as _channelz_pb2_grpc 21 22from google.protobuf import json_format 23 24 25class ChannelzServicer(_channelz_pb2_grpc.ChannelzServicer): 26 """Servicer handling RPCs for service statuses.""" 27 28 @staticmethod 29 def GetTopChannels(request, context): 30 try: 31 return json_format.Parse( 32 cygrpc.channelz_get_top_channels(request.start_channel_id), 33 _channelz_pb2.GetTopChannelsResponse(), 34 ) 35 except (ValueError, json_format.ParseError) as e: 36 context.set_code(grpc.StatusCode.INTERNAL) 37 context.set_details(str(e)) 38 39 @staticmethod 40 def GetServers(request, context): 41 try: 42 return json_format.Parse( 43 cygrpc.channelz_get_servers(request.start_server_id), 44 _channelz_pb2.GetServersResponse(), 45 ) 46 except (ValueError, json_format.ParseError) as e: 47 context.set_code(grpc.StatusCode.INTERNAL) 48 context.set_details(str(e)) 49 50 @staticmethod 51 def GetServer(request, context): 52 try: 53 return json_format.Parse( 54 cygrpc.channelz_get_server(request.server_id), 55 _channelz_pb2.GetServerResponse(), 56 ) 57 except ValueError as e: 58 context.set_code(grpc.StatusCode.NOT_FOUND) 59 context.set_details(str(e)) 60 except json_format.ParseError as e: 61 context.set_code(grpc.StatusCode.INTERNAL) 62 context.set_details(str(e)) 63 64 @staticmethod 65 def GetServerSockets(request, context): 66 try: 67 return json_format.Parse( 68 cygrpc.channelz_get_server_sockets(request.server_id, 69 request.start_socket_id, 70 request.max_results), 71 _channelz_pb2.GetServerSocketsResponse(), 72 ) 73 except ValueError as e: 74 context.set_code(grpc.StatusCode.NOT_FOUND) 75 context.set_details(str(e)) 76 except json_format.ParseError as e: 77 context.set_code(grpc.StatusCode.INTERNAL) 78 context.set_details(str(e)) 79 80 @staticmethod 81 def GetChannel(request, context): 82 try: 83 return json_format.Parse( 84 cygrpc.channelz_get_channel(request.channel_id), 85 _channelz_pb2.GetChannelResponse(), 86 ) 87 except ValueError as e: 88 context.set_code(grpc.StatusCode.NOT_FOUND) 89 context.set_details(str(e)) 90 except json_format.ParseError as e: 91 context.set_code(grpc.StatusCode.INTERNAL) 92 context.set_details(str(e)) 93 94 @staticmethod 95 def GetSubchannel(request, context): 96 try: 97 return json_format.Parse( 98 cygrpc.channelz_get_subchannel(request.subchannel_id), 99 _channelz_pb2.GetSubchannelResponse(), 100 ) 101 except ValueError as e: 102 context.set_code(grpc.StatusCode.NOT_FOUND) 103 context.set_details(str(e)) 104 except json_format.ParseError as e: 105 context.set_code(grpc.StatusCode.INTERNAL) 106 context.set_details(str(e)) 107 108 @staticmethod 109 def GetSocket(request, context): 110 try: 111 return json_format.Parse( 112 cygrpc.channelz_get_socket(request.socket_id), 113 _channelz_pb2.GetSocketResponse(), 114 ) 115 except ValueError as e: 116 context.set_code(grpc.StatusCode.NOT_FOUND) 117 context.set_details(str(e)) 118 except json_format.ParseError as e: 119 context.set_code(grpc.StatusCode.INTERNAL) 120 context.set_details(str(e)) 121