Ep 029: Problem Unknown: Log Lines

Play Episode

Nate is dropped in the middle of a huge log file and hunts for the source of the errors.

Related episodes:

Clojure in this episode:

Related links:

Code sample from this episode:

(ns devops.week-01
  (:require
    [clojure.java.io :as io]
    [clojure.string :as string]
    ))

;; General parsing

(def general-re #"(\d\d\d\d-\d\d-\d\d)\s+(\d\d:\d\d:\d\d)\s+\|\s+(\S+)\s+\|\s+(\S+)\s+\|\s+(\S+)\s+\|\s(.*)")

(defn parse-line
  [line]
  (when-let [[whole dt tm thread-name level ns message] (re-matches general-re line)]
    {:raw/line whole
     :log/date dt
     :log/time tm
     :log/thread thread-name
     :log/level level
     :log/namespace ns
     :log/message message
     }))

(defn general-parse
  [lines]
  (->> lines
       (map parse-line)
       (filter some?)))


;; Detailed parsing

(def detail-specs
  [[#"transaction failed while updating user ([^:]+): code 357"
    (fn [[_whole user]] {:kind :code-357 :code-357/user user})]
   ])

(defn try-detail-spec
  [message [re fn]]
  (when-some [matches (re-matches re message)]
    (fn matches)))

(defn parse-details
  [entry]
  (let [{:keys [log/message]} entry]
    (if-some [extra (->> detail-specs
                         (map (partial try-detail-spec message))
                         (filter some?)
                         (first))]
      (merge entry extra)
      entry)))


;; Log analysis

(defn lines
  [filename]
  (->> (slurp filename)
       (string/split-lines)))

(defn summarize
  [filename calc]
  (->> (lines filename)
       (general-parse)
       (map parse-details)
       (calc)))


;; data summarizing

(defn code-357-by-user
  [entries]
  (->> entries
       (filter #(= :code-357 (:kind %)))
       (map :code-357/user)
       (frequencies)))


(comment
  (summarize "sample.log" code-357-by-user)
  )

Log file sample:

2019-05-14 16:48:55 | process-Poster | INFO  | com.donutgram.poster | transaction failed while updating user joe: code 357
2019-05-14 16:48:56 | process-Poster | INFO  | com.donutgram.poster | transaction failed while updating user sally: code 357
2019-05-14 16:48:57 | process-Poster | INFO  | com.donutgram.poster | transaction failed while updating user joe: code 357