]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mds/CDentry: shorten path before logging
authorRishabh Dave <ridave@redhat.com>
Thu, 21 Aug 2025 11:51:48 +0000 (17:21 +0530)
committerRishabh Dave <ridave@redhat.com>
Wed, 1 Oct 2025 19:01:46 +0000 (00:31 +0530)
Printing full path in logs not only is unuseful but also reduces the
readability of logs (image path with 2000 components). Therefore, print
only 10 final components of the path.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
src/include/filepath.cc
src/include/filepath.h
src/mds/CDentry.cc

index 3844945ea47579eeaf3edd06bf49bd53fff305b1..ce2c29ea67429e84039bd5568d469be6e43c5a88 100644 (file)
@@ -41,6 +41,14 @@ void filepath::parse_bits() const {
   }
 }
 
+void filepath::set_trimmed() {
+  if (trimmed)
+    return;
+  // indicates that the path has been shortened.
+  path += "...";
+  trimmed = true;
+}
+
 void filepath::set_path(std::string_view s) {
   if (!s.empty() && s[0] == '/') {
     path = s.substr(1);
index 67be4a08759ab0fd1297d96d107b00e5931e8547..920ffcb95cb44cb10cbda6bc36517a7e58879a78 100644 (file)
@@ -38,6 +38,9 @@ namespace ceph { class Formatter; }
 class filepath {
   inodeno_t ino = 0;   // base inode.  ino=0 implies pure relative path.
   std::string path;     // relative path.
+  // tells get_path() whether it should prefix path with "..." to indicate that
+  // it was shortened.
+  bool trimmed = false;
 
   /** bits - path segments
    * this is ['a', 'b', 'c'] for both the aboslute and relative case.
@@ -82,6 +85,7 @@ class filepath {
   // accessors
   inodeno_t get_ino() const { return ino; }
   const std::string& get_path() const { return path; }
+  void set_trimmed();
   const char *c_str() const { return path.c_str(); }
 
   int length() const { return path.length(); }
index d373f82aa22ee48fe4c16efe5237254f4e911ecc..cc519fdcc50cbdca28aec015a8dbb43c41b9e3a2 100644 (file)
@@ -313,9 +313,25 @@ void CDentry::make_path_string(string& s, bool projected) const
 
 void CDentry::make_path(filepath& fp, bool projected) const
 {
-  ceph_assert(dir);
-  dir->inode->make_path(fp, projected);
-  fp.push_dentry(get_name());
+  /* path_comp_count = path components count
+   *
+   * Printing more than 10 components of a path not only is not useful but also it
+   * makes reading logs harder (imagine path with 2000 components). Therefore,
+   * shorten path.
+   */
+  static int path_comp_count = 1;
+  fp.set_trimmed();
+
+  if (path_comp_count <= 10) {
+    ceph_assert(dir);
+    ++path_comp_count;
+    dir->inode->make_path(fp, projected);
+    fp.push_dentry(get_name());
+  } else {
+    // resetting the counter
+    path_comp_count = 1;
+    return;
+  }
 }
 
 /*