| 1 | //===--- Monitor.cpp - Request server monitoring information through CLI --===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MonitoringService.grpc.pb.h" |
| 10 | #include "MonitoringService.pb.h" |
| 11 | |
| 12 | #include "support/Logger.h" |
| 13 | #include "clang/Basic/Version.h" |
| 14 | #include "llvm/Support/CommandLine.h" |
| 15 | #include "llvm/Support/Signals.h" |
| 16 | |
| 17 | #include <chrono> |
| 18 | #include <google/protobuf/util/json_util.h> |
| 19 | #include <grpc++/grpc++.h> |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace clangd { |
| 23 | namespace remote { |
| 24 | namespace { |
| 25 | |
| 26 | static constexpr char Overview[] = R"( |
| 27 | This tool requests monitoring information (uptime, index freshness) from the |
| 28 | server and prints it to stdout. |
| 29 | )" ; |
| 30 | |
| 31 | llvm::cl::opt<std::string> |
| 32 | ServerAddress("server-address" , llvm::cl::Positional, |
| 33 | llvm::cl::desc("Address of the invoked server." ), |
| 34 | llvm::cl::Required); |
| 35 | |
| 36 | } // namespace |
| 37 | } // namespace remote |
| 38 | } // namespace clangd |
| 39 | } // namespace clang |
| 40 | |
| 41 | int main(int argc, char *argv[]) { |
| 42 | using namespace clang::clangd::remote; |
| 43 | llvm::cl::ParseCommandLineOptions(argc, argv, Overview); |
| 44 | llvm::sys::PrintStackTraceOnErrorSignal(Argv0: argv[0]); |
| 45 | |
| 46 | const auto Channel = |
| 47 | grpc::CreateChannel(target: ServerAddress, creds: grpc::InsecureChannelCredentials()); |
| 48 | const auto Stub = clang::clangd::remote::v1::Monitor::NewStub(Channel); |
| 49 | grpc::ClientContext Context; |
| 50 | Context.set_deadline(std::chrono::system_clock::now() + |
| 51 | std::chrono::seconds(10)); |
| 52 | Context.AddMetadata(meta_key: "version" , meta_value: clang::getClangToolFullVersion(ToolName: "clangd" )); |
| 53 | const clang::clangd::remote::v1::MonitoringInfoRequest Request; |
| 54 | clang::clangd::remote::v1::MonitoringInfoReply Response; |
| 55 | const auto Status = Stub->MonitoringInfo(&Context, Request, &Response); |
| 56 | if (!Status.ok()) { |
| 57 | clang::clangd::elog("Can not request monitoring information ({0}): {1}\n" , |
| 58 | Status.error_code(), Status.error_message()); |
| 59 | return -1; |
| 60 | } |
| 61 | std::string Output; |
| 62 | google::protobuf::util::JsonPrintOptions Options; |
| 63 | Options.add_whitespace = true; |
| 64 | Options.always_print_primitive_fields = true; |
| 65 | Options.preserve_proto_field_names = true; |
| 66 | const auto JsonStatus = |
| 67 | google::protobuf::util::MessageToJsonString(Response, &Output, Options); |
| 68 | if (!JsonStatus.ok()) { |
| 69 | clang::clangd::elog("Can not convert response ({0}) to JSON ({1}): {2}\n" , |
| 70 | Response.DebugString(), |
| 71 | static_cast<int>(JsonStatus.code()), |
| 72 | JsonStatus.message().as_string()); |
| 73 | return -1; |
| 74 | } |
| 75 | llvm::outs() << Output; |
| 76 | } |
| 77 | |