You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by st...@apache.org on 2023/11/16 22:22:40 UTC

(solr) branch main updated: SOLR-17035 Add trace id to jetty thread names to improve debuggability (#2019)

This is an automated email from the ASF dual-hosted git repository.

stillalex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 9eb0466472c SOLR-17035 Add trace id to jetty thread names to improve debuggability (#2019)
9eb0466472c is described below

commit 9eb0466472c24d29a3e5bfbcb78f08730db9cefc
Author: Alex D <st...@apache.org>
AuthorDate: Thu Nov 16 14:22:35 2023 -0800

    SOLR-17035 Add trace id to jetty thread names to improve debuggability (#2019)
---
 solr/CHANGES.txt                                             | 2 ++
 solr/core/src/java/org/apache/solr/servlet/ServletUtils.java | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index cbf1e020914..fca3e1fecfa 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -101,6 +101,8 @@ Improvements
 * SOLR-16397: The rename-core v2 endpoint has been updated to be more REST-ful.
   RENAME is now available at `POST /api/cores/coreName/rename` (Sanjay Dutt via Jason Gerlowski)
 
+* SOLR-17035: Add trace id to jetty thread names to improve debuggability via stack traces (Alex Deparvu)
+
 Optimizations
 ---------------------
 (No changes)
diff --git a/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java b/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java
index 2de3b3f2582..83d88b65716 100644
--- a/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java
+++ b/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java
@@ -233,11 +233,17 @@ public abstract class ServletUtils {
     Context context = TraceUtils.extractContext(request);
     Span span = TraceUtils.startHttpRequestSpan(request, context);
 
+    final Thread currentThread = Thread.currentThread();
+    final String oldThreadName = currentThread.getName();
     try (var scope = context.with(span).makeCurrent()) {
       assert scope != null; // prevent javac warning about scope being unused
       TraceUtils.setSpan(request, span);
       TraceUtils.ifValidTraceId(
           span, s -> MDCLoggingContext.setTracerId(s.getSpanContext().getTraceId()));
+      String traceId = MDCLoggingContext.getTraceId();
+      if (traceId != null) {
+        currentThread.setName(oldThreadName + "-" + traceId);
+      }
       tracedExecution.run();
     } catch (ExceptionWhileTracing e) {
       if (e.e instanceof SolrAuthenticationException) {
@@ -256,6 +262,7 @@ public abstract class ServletUtils {
         throw new RuntimeException(e.e);
       }
     } finally {
+      currentThread.setName(oldThreadName);
       TraceUtils.setHttpStatus(span, response.getStatus());
       span.end();
     }