xref: /aosp_15_r20/external/grpc-grpc/src/cpp/server/channelz/channelz_service_plugin.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2018 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 #include <grpc/support/port_platform.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include <grpcpp/ext/channelz_service_plugin.h>
25 #include <grpcpp/impl/server_builder_plugin.h>
26 #include <grpcpp/impl/server_initializer.h>
27 #include <grpcpp/server_builder.h>
28 
29 #include "src/cpp/server/channelz/channelz_service.h"
30 
31 namespace grpc {
32 namespace channelz {
33 namespace experimental {
34 
35 class ChannelzServicePlugin : public grpc::ServerBuilderPlugin {
36  public:
ChannelzServicePlugin()37   ChannelzServicePlugin() : channelz_service_(new grpc::ChannelzService()) {}
38 
name()39   std::string name() override { return "channelz_service"; }
40 
InitServer(grpc::ServerInitializer * si)41   void InitServer(grpc::ServerInitializer* si) override {
42     si->RegisterService(channelz_service_);
43   }
44 
Finish(grpc::ServerInitializer *)45   void Finish(grpc::ServerInitializer* /*si*/) override {}
46 
ChangeArguments(const std::string &,void *)47   void ChangeArguments(const std::string& /*name*/, void* /*value*/) override {}
48 
has_sync_methods() const49   bool has_sync_methods() const override {
50     if (channelz_service_) {
51       return channelz_service_->has_synchronous_methods();
52     }
53     return false;
54   }
55 
has_async_methods() const56   bool has_async_methods() const override {
57     if (channelz_service_) {
58       return channelz_service_->has_async_methods();
59     }
60     return false;
61   }
62 
63  private:
64   std::shared_ptr<grpc::ChannelzService> channelz_service_;
65 };
66 
67 static std::unique_ptr<grpc::ServerBuilderPlugin>
CreateChannelzServicePlugin()68 CreateChannelzServicePlugin() {
69   return std::unique_ptr<grpc::ServerBuilderPlugin>(
70       new ChannelzServicePlugin());
71 }
72 
InitChannelzService()73 void InitChannelzService() {
74   static struct Initializer {
75     Initializer() {
76       grpc::ServerBuilder::InternalAddPluginFactory(
77           &grpc::channelz::experimental::CreateChannelzServicePlugin);
78     }
79   } initialize;
80 }
81 
82 }  // namespace experimental
83 }  // namespace channelz
84 }  // namespace grpc
85