[clang-tools-extra] [clangd] Include Json Rpc Request ID in trace events (PR #84067)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 5 12:54:44 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clangd

Author: dupes (tdupes)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/84067.diff


3 Files Affected:

- (modified) clang-tools-extra/clangd/ClangdLSPServer.cpp (+9-2) 
- (modified) clang-tools-extra/clangd/support/Trace.cpp (+6) 
- (modified) clang-tools-extra/clangd/support/Trace.h (+2) 


``````````diff
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index f29dadde2b86d5..632cdd04cf6fc4 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -229,7 +229,7 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
 
   bool onCall(llvm::StringRef Method, llvm::json::Value Params,
               llvm::json::Value ID) override {
-    WithContext HandlerContext(handlerContext());
+    WithContext HandlerContext(handlerContext(llvm::to_string(ID)));
     // Calls can be canceled by the client. Add cancellation context.
     WithContext WithCancel(cancelableRequestContext(ID));
     trace::Span Tracer(Method, LSPLatency);
@@ -252,7 +252,7 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
 
   bool onReply(llvm::json::Value ID,
                llvm::Expected<llvm::json::Value> Result) override {
-    WithContext HandlerContext(handlerContext());
+    WithContext HandlerContext(handlerContext(llvm::to_string(ID)));
 
     Callback<llvm::json::Value> ReplyHandler = nullptr;
     if (auto IntID = ID.getAsInteger()) {
@@ -415,6 +415,13 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
         Server.Opts.Encoding.value_or(OffsetEncoding::UTF16));
   }
 
+  Context handlerContext(const std::string &ID) const {
+    return Context::current()
+        .derive(kCurrentOffsetEncoding,
+                Server.Opts.Encoding.value_or(OffsetEncoding::UTF16))
+        .derive(trace::EventTracer::kRequestId, ID);
+  }
+
   // We run cancelable requests in a context that does two things:
   //  - allows cancellation using RequestCancelers[ID]
   //  - cleans up the entry in RequestCancelers when it's no longer needed
diff --git a/clang-tools-extra/clangd/support/Trace.cpp b/clang-tools-extra/clangd/support/Trace.cpp
index 419c2eee99ec84..f1244c8025bf37 100644
--- a/clang-tools-extra/clangd/support/Trace.cpp
+++ b/clang-tools-extra/clangd/support/Trace.cpp
@@ -24,6 +24,8 @@ namespace clang {
 namespace clangd {
 namespace trace {
 
+Key<std::string> EventTracer::kRequestId;
+
 namespace {
 // The current implementation is naive: each thread writes to Out guarded by Mu.
 // Perhaps we should replace this by something that disturbs performance less.
@@ -159,6 +161,10 @@ class JSONTracer : public EventTracer {
     Out.object([&] {
       Out.attribute("pid", 0);
       Out.attribute("ph", Phase);
+      const auto *id = Context::current().get(kRequestId);
+      if (id) {
+        Out.attribute("rid", *id);
+      }
       for (const auto &KV : Event)
         Out.attribute(KV.first, KV.second);
     });
diff --git a/clang-tools-extra/clangd/support/Trace.h b/clang-tools-extra/clangd/support/Trace.h
index 1bfc75b874d8a9..ae00ea322fb39a 100644
--- a/clang-tools-extra/clangd/support/Trace.h
+++ b/clang-tools-extra/clangd/support/Trace.h
@@ -99,6 +99,8 @@ class EventTracer {
   /// Called whenever a metrics records a measurement.
   virtual void record(const Metric &Metric, double Value,
                       llvm::StringRef Label) {}
+  /// A key to store json request id in the context used in tracers
+  static Key<std::string> kRequestId;
 };
 
 /// Sets up a global EventTracer that consumes events produced by Span and

``````````

</details>


https://github.com/llvm/llvm-project/pull/84067


More information about the cfe-commits mailing list