05 February 2020

mongod-backtrace.png

When you are reviewing a backtrace in a mongod log file which is overwhelming like the one shown above, would you like to pretty-print it? If the answer is yes, how would you do it? Here is the one-liner I'm using.

$ cat mongod.log | sed -n '/BEGIN BACKTRACE/{n;p;}' | jq '.'
{
  "backtrace": [
    {
      "b": "5606DAE25000",
      "o": "1679911",
      "s": "_ZN5mongo15printStackTraceERSo"
    },
    {
      "b": "5606DAE25000",
      "o": "16791D5"
    },
    {
      "b": "5606DAE25000",
      "o": "20D37E6",
      "s": "_ZN10__cxxabiv111__terminateEPFvvE"
    },
...

Explanation of the above command:

  • sed -n '/BEGIN BACKTRACE/{n;p;}' : searches for the string "BEGIN BACKTRACE" and print the next line, which is the backtrace.
  • jq '.' : since the backtrace is a json document, we can jq to handle it and what jq '.' does is pretty print the input.

If you are familiar with jq, you'd already know actually there are many more things we can do than just format the backtrace. For example, we can print just the first 3 frames of each backtrace (in this example, there are two backtraces found in the log file):

$ cat mongod.log | sed -n '/BEGIN BACKTRACE/{n;p;}' | jq '.backtrace[0:3]'
[
  {
    "b": "5606DAE25000",
    "o": "1679911",
    "s": "_ZN5mongo15printStackTraceERSo"
  },
  {
    "b": "5606DAE25000",
    "o": "16791D5"
  },
  {
    "b": "5606DAE25000",
    "o": "20D37E6",
    "s": "_ZN10__cxxabiv111__terminateEPFvvE"
  }
]
[
  {
    "b": "5585CAE09000",
    "o": "1679911",
    "s": "_ZN5mongo15printStackTraceERSo"
  },
  {
    "b": "5585CAE09000",
    "o": "16791D5"
  },
  {
    "b": "5585CAE09000",
    "o": "20D37E6",
    "s": "_ZN10__cxxabiv111__terminateEPFvvE"
  }
]

We can also confirm the mongod version easily:

$ cat mongod.log | sed -n '/BEGIN BACKTRACE/{n;p;}' | jq '.processInfo.mongodbVersion'
"3.4.1"
"3.4.1"


blog comments powered by Disqus