observability

o11y, monitoring, logging, tracing, alerting and higher level discussions
jumar 2020-06-17T03:55:36.071300Z

Thanks everyone for your comments! I remember we were using MDC years ago when I work solely with Java.

seancorfield 2020-06-17T04:44:33.073100Z

This discussion encouraged me to add a with-log-context macro to our code base today. It takes a context (usually) a hash map, and a body to be executed and stringizes the context (into a CloseableThreadContext). Very useful! Thank you all again for the nudge!

jumar 2020-06-17T06:01:35.073200Z

I guess it's pretty basic but would you mind sharing the macro?

seancorfield 2020-06-17T06:05:41.073400Z

Here's what I added

(defn- stringize-context
  "Given a logging context (that is hopefully a hash map), return a
  hash map with strings for keys and for values. If the context is
  not a hash map, return a hash map with a ctx string key and the
  context value as a string."
  [ctx]
  (if (map? ctx)
    (reduce-kv (fn [m k v]
                 (assoc m
                        (if (keyword? k) (name k) (str k))
                        (pr-str v)))
               {}
               ctx)
    {"ctx" (pr-str ctx)}))

(defmacro with-log-context
  "Given a hash map and a body, add the hash map to the log4j2 mapped
  diagnostic context, and execute the body."
  [ctx & body]
  `(with-open [_# (CloseableThreadContext/putAll (stringize-context ~ctx))]
     ~@body))

👍 2
seancorfield 2020-06-17T06:06:13.073600Z

Assuming :import of (org.apache.logging.log4j CloseableThreadContext)

sparkofreason 2020-06-17T20:48:04.075400Z

Any recommendations on parsing log file data? Looking to write a little utility to stream in logs from a kubernetes cluster and performe Clojuresque voodoo upon the data.

2020-06-17T20:50:37.076200Z

I think the best answer is to use logging that's meant to be machine readable, my company has had good luck with cambium json logging https://cambium-clojure.github.io/

2020-06-17T20:50:55.076700Z

oh - the logs aren't from clojure originally...

sparkofreason 2020-06-17T20:52:36.077800Z

Right. In fact, they're from a variety of sources in a variety of formats, now that I look at them, so I don't know if I'll find something out of the box to cover the spectrum. But maybe something as an example to get started would be helpful.

2020-06-17T20:54:33.078600Z

I guess in theory the spectrum goes from regex to instaparse(?) -- google shows me a few libs but I can't tell you if any are better than others

2020-06-17T20:54:52.078800Z

eg. https://github.com/dmillett/clash

sparkofreason 2020-06-17T20:57:41.079400Z

Thanks, this might do it, especially in conjunction with https://github.com/lambdaisland/regal.