Nickolaos Kas <
redh...@gmail.com> writes:
> ...
> No point mentioning that the code crashes and burns with the error
> *clojure.lang.Compiler$CompilerException: java.lang.NullPointerException*
> which seems to be originating from my first *def* statement trying to
> define the plugin_inst variable. I am guessing this is more of a riemann
> issue rather than clojure given that the statement *(:plugin_instance
> event)* seems to return null.
>
> Given that there is a key in the event called :plugin_instances, what is
> wrong with the above code?
Hard to tell without a full trace. My first guess is that "where" macro,
trying to rewrite body by looking for "event" symbols, found something
unexpected.
Why not use "smap" directly? It was intended to adjust event and will
make code easier to test.
I'd approach maybe with something like this (please test it first):
(where (= (:plugin event) "docker")
(smap
(fn [ev]
;; Destructuring makes code readable. Also, some-> will check
;; that ':plugin_instance' key is present in a map or str/split can
;; throw exception.
(let [[first-name second-name] (some-> ev :plugin_instance (str/split #"\."))
container-name (when (and first-name
second-name
(< 4 (count second-name)))
(str first-name "." second-name))
;; get values for the given keys, but only those not nil
vals (remove nil? (map ev [:type :type_instance :ds_name]))
;; generate service name
service-name (str/join "." vals)]
(assoc ev
:service service-name
:host (or container-name (:host ev)))))
(smap rewrite-service graph)))
Now, because you already have "rewrite-service" called through "smap",
you can refactor initial code to a separate function.
(defn rewrite-service-host [ev]
(let [[first-name second-name]...
;; copy rest of the "(fn [ev]) body from above
)
;; initial code is now in two lines only
(where (= (:plugin event) "docker")
(smap (comp rewrite-service-host rewrite-service) graph))
Best,
Sanel