Skip to content

Commit 6c1b256

Browse files
committed
[clangd] Include Json Rpc Request ID in trace events
Included in a new field, `rid,` this allows us to associate each trace log with the incoming request that sent them. Helpful for associating insights from clangd logs with logging on an lsp client side - such as a vscode extension.
1 parent cb38133 commit 6c1b256

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
229229

230230
bool onCall(llvm::StringRef Method, llvm::json::Value Params,
231231
llvm::json::Value ID) override {
232-
WithContext HandlerContext(handlerContext());
232+
WithContext HandlerContext(handlerContext(llvm::to_string(ID)));
233233
// Calls can be canceled by the client. Add cancellation context.
234234
WithContext WithCancel(cancelableRequestContext(ID));
235235
trace::Span Tracer(Method, LSPLatency);
@@ -252,7 +252,7 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
252252

253253
bool onReply(llvm::json::Value ID,
254254
llvm::Expected<llvm::json::Value> Result) override {
255-
WithContext HandlerContext(handlerContext());
255+
WithContext HandlerContext(handlerContext(llvm::to_string(ID)));
256256

257257
Callback<llvm::json::Value> ReplyHandler = nullptr;
258258
if (auto IntID = ID.getAsInteger()) {
@@ -415,6 +415,13 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
415415
Server.Opts.Encoding.value_or(OffsetEncoding::UTF16));
416416
}
417417

418+
Context handlerContext(const std::string &ID) const {
419+
return Context::current()
420+
.derive(kCurrentOffsetEncoding,
421+
Server.Opts.Encoding.value_or(OffsetEncoding::UTF16))
422+
.derive(trace::EventTracer::kRequestId, ID);
423+
}
424+
418425
// We run cancelable requests in a context that does two things:
419426
// - allows cancellation using RequestCancelers[ID]
420427
// - cleans up the entry in RequestCancelers when it's no longer needed

clang-tools-extra/clangd/support/Trace.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace clang {
2424
namespace clangd {
2525
namespace trace {
2626

27+
Key<std::string> EventTracer::kRequestId;
28+
2729
namespace {
2830
// The current implementation is naive: each thread writes to Out guarded by Mu.
2931
// Perhaps we should replace this by something that disturbs performance less.
@@ -159,6 +161,10 @@ class JSONTracer : public EventTracer {
159161
Out.object([&] {
160162
Out.attribute("pid", 0);
161163
Out.attribute("ph", Phase);
164+
const auto *id = Context::current().get(kRequestId);
165+
if (id) {
166+
Out.attribute("rid", *id);
167+
}
162168
for (const auto &KV : Event)
163169
Out.attribute(KV.first, KV.second);
164170
});

clang-tools-extra/clangd/support/Trace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class EventTracer {
9999
/// Called whenever a metrics records a measurement.
100100
virtual void record(const Metric &Metric, double Value,
101101
llvm::StringRef Label) {}
102+
/// A key to store json request id in the context used in tracers
103+
static Key<std::string> kRequestId;
102104
};
103105

104106
/// Sets up a global EventTracer that consumes events produced by Span and

0 commit comments

Comments
 (0)