10

Is it possible to launch a Ruby debugger from within the Logstash Ruby filter plugin? It would be very handy for debugging.

David P
  • 3,604
  • 3
  • 37
  • 54

1 Answers1

5

The good Logstash folks have thought of this already as they included pry into Logstash core. So all you have to do is to require pry in your ruby filter code as shown in the sample config below:

input { 
  file {
    path => "/tmp/myfile.csv"
    sincedb_path => "/dev/null"
    start_position => "beginning"
  }
}
filter {
  ruby {
    code => "require 'pry'
       ... your code ...

       # start a REPL session
       binding.pry

       ... your code ...
    "
 }
}

When Logstash runs, you'll get a REPL session in your terminal which looks like this and you'll be able to do whatever pry allows you to do.

[1] pry(#<LogStash::Filters::Ruby>)> 
Val
  • 207,596
  • 13
  • 358
  • 360
  • I can see it trying to launch the pry session, but it just doesn't break.. It just keeps going and doesn't give me the chance to enter the debug session. I think it might be due to the fact that I'm using stdin as my input plugin, and I pipe my CSV file into logstash. – David P Nov 10 '16 at 19:27
  • In my test I did use the stdin input as well and it worked. Can you show your logstash config ? – Val Nov 10 '16 at 19:28
  • simplified because of client work: `input { stdin { } } filter { csv { columns => ["field1", "field2"] separator => "," } ruby { init => " require 'pry' " code => " binding.pry " } ` – David P Nov 10 '16 at 19:33
  • and then I start it with: $ ESENV=development /usr/local/bin/logstash-2.3.4/bin/logstash -v --allow-env -f import.conf < /tmp/myfile.csv – David P Nov 10 '16 at 19:46
  • can you try to require pry in the code part and not the init part? – Val Nov 10 '16 at 19:57
  • `Pipeline main started [1] pry(#)> CSVROW1,CSVROW2,etc, SyntaxError: unexpected ','` but it keeps going for every row in the CSV file. – David P Nov 10 '16 at 20:40
  • Let me try your setup locally and get back to you. – Val Nov 10 '16 at 21:23
  • The issue seems to be related to [reading from stdin](http://stackoverflow.com/questions/32333962/pry-not-stopping-when-called-from-a-ruby-script-that-reads-from-stdin). Instead, if you use a `file` input, you'll get your REPL session as expected. I've updated my answer. Try it out. – Val Nov 11 '16 at 05:37