• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16from google.protobuf import json_format
17import grpc
18from grpc._cython import cygrpc
19import grpc_channelz.v1.channelz_pb2 as _channelz_pb2
20import grpc_channelz.v1.channelz_pb2_grpc as _channelz_pb2_grpc
21
22
23class ChannelzServicer(_channelz_pb2_grpc.ChannelzServicer):
24    """Servicer handling RPCs for service statuses."""
25
26    @staticmethod
27    def GetTopChannels(request, context):
28        try:
29            return json_format.Parse(
30                cygrpc.channelz_get_top_channels(request.start_channel_id),
31                _channelz_pb2.GetTopChannelsResponse(),
32            )
33        except (ValueError, json_format.ParseError) as e:
34            context.set_code(grpc.StatusCode.INTERNAL)
35            context.set_details(str(e))
36
37    @staticmethod
38    def GetServers(request, context):
39        try:
40            return json_format.Parse(
41                cygrpc.channelz_get_servers(request.start_server_id),
42                _channelz_pb2.GetServersResponse(),
43            )
44        except (ValueError, json_format.ParseError) as e:
45            context.set_code(grpc.StatusCode.INTERNAL)
46            context.set_details(str(e))
47
48    @staticmethod
49    def GetServer(request, context):
50        try:
51            return json_format.Parse(
52                cygrpc.channelz_get_server(request.server_id),
53                _channelz_pb2.GetServerResponse(),
54            )
55        except ValueError as e:
56            context.set_code(grpc.StatusCode.NOT_FOUND)
57            context.set_details(str(e))
58        except json_format.ParseError as e:
59            context.set_code(grpc.StatusCode.INTERNAL)
60            context.set_details(str(e))
61
62    @staticmethod
63    def GetServerSockets(request, context):
64        try:
65            return json_format.Parse(
66                cygrpc.channelz_get_server_sockets(
67                    request.server_id,
68                    request.start_socket_id,
69                    request.max_results,
70                ),
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