2

我可能不理解 riemann/clojure 中的一些关键概念。我正在尝试从格式的事件解析字段:服务"aaa:1234.bbbb.cccc.ddddd",并使用函数“with”将新字段 pid 添加到事件中。任何人都可以向我解释为什么这段代码riemann.config会引发异常:

...
(let [index (default :ttl 300 (update-index (index)))]

 ; Inbound events will be passed to these streams:
 (streams
 index
 (where (service #"(\w+):(\d+)\.(\w+)\.(\w+)\.(\w+)")
   (with :pid (str/replace service #"(\w+):(\d+)\.(\w+)\.(\w+)\.(\w+)" "$2")
)
)
...

user=> (riemann.bin/reload!)
#error {
 :cause "Unable to resolve symbol: service in this context"
 :via
 [{:type clojure.lang.Compiler$CompilerException
 :message "java.lang.RuntimeException: Unable to resolve symbol: service in this context, compiling:(/etc/riemann/riemann.config:73:19)"
4

1 回答 1

1

我想(where (service ,,,))这只是where宏 for的语法糖(where* (fn [event] (let [service (:service event)] ,,,))),这就是为什么你不能service在 : 它的主体中使用它的原因where:它不是在那里定义的名称。

查看 with的文档,在我看来您应该使用smap

(where (service #"(\w+):(\d+)\.(\w+)\.(\w+)\.(\w+)")
   (smap (fn [e] (assoc e :pid (str/replace (:service e) #"(\w+):(\d+)\.(\w+)\.(\w+)\.(\w+)" "$2")))))
于 2017-11-16T11:06:16.453 回答