求助:Logstash 6.2.2中Ruby过滤器配置初始化常量失败问题
Hey there, let's work through this Logstash Ruby filter issue you're hitting—those cryptic constant errors are always frustrating, especially when you've already stripped down your config!
First, let's start with a rock-solid minimal Ruby filter configuration that should pass logstash.bat -t without issues in 6.2.2. Use this as a baseline to verify your setup:
input { stdin {} } filter { ruby { code => "event.set('new_field', 'success from ruby filter')" } } output { stdout { codec => rubydebug } }
Run logstash.bat -t with this config—if it passes, that means your core Logstash installation is working, and the problem lies in your original Ruby code or config structure.
Now, let's break down the common causes of that uninitialized constant error and how to fix them:
Invalid Ruby code referencing non-existent constants: That error message points to a missing
ConfigurationErrorconstant in the Logstash Ruby context. If your code tries to useraise ConfigurationErroror reference that constant directly, it won't exist here. Instead, use plain string error messages if you need to raise issues:# Bad: raises missing constant error # raise ConfigurationError, "Invalid data" # Good: works in Logstash's Ruby environment raise "Invalid data detected in event"Incorrect quote escaping in the
codeparameter: If your Ruby code uses double quotes, wrap the entirecodevalue in single quotes (or vice versa) to avoid syntax parsing issues. For example:# Correct: outer single quotes, inner double quotes ruby { code => 'event.set("message", event.get("message").upcase)' } # Also correct: outer double quotes, escaped inner double quotes ruby { code => "event.set(\"message\", event.get(\"message\").upcase)" }Missing or malformed config blocks: Logstash requires all configs to have
input,filter, andoutputsections. Even if you're just testing the filter, don't skip the other blocks—partial configs can trigger unexpected parsing errors like the one you're seeing.Using deprecated event manipulation methods: In 6.x versions, the recommended way to interact with events is
event.set()andevent.get(). Avoid using the olderevent['field']syntax, as it can sometimes cause context issues with Logstash's internal compiler.
If the minimal config still fails even on a fresh 6.2.2 install, double-check that you're running the command from the correct directory (the Logstash bin folder) and that there are no stray characters or syntax errors in your config file (like missing curly braces, commas, or mismatched quotes).
Once you get the minimal config working, gradually add back your custom Ruby logic, testing with logstash.bat -t after each change to pinpoint exactly which line is causing the error.
内容的提问来源于stack exchange,提问作者Andrew Wynham

