1 //===--- Monitor.cpp - Request server monitoring information through CLI --===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MonitoringService.grpc.pb.h"
10 #include "MonitoringService.pb.h"
12 #include "support/Logger.h"
13 #include "clang/Basic/Version.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/Signals.h"
18 #include <google/protobuf/util/json_util.h>
19 #include <grpc++/grpc++.h>
26 static constexpr char Overview
[] = R
"(
27 This tool requests monitoring information (uptime, index freshness) from the
28 server and prints it to stdout.
31 llvm::cl::opt
<std::string
>
32 ServerAddress("server-address", llvm::cl::Positional
,
33 llvm::cl::desc("Address of the invoked server."),
41 int main(int argc
, char *argv
[]) {
42 using namespace clang::clangd::remote
;
43 llvm::cl::ParseCommandLineOptions(argc
, argv
, Overview
);
44 llvm::sys::PrintStackTraceOnErrorSignal(argv
[0]);
47 grpc::CreateChannel(ServerAddress
, 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("version", clang::getClangToolFullVersion("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
);
57 clang::clangd::elog("Can not request monitoring information ({0}): {1}\n",
58 Status
.error_code(), Status
.error_message());
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());
75 llvm::outs() << Output
;