Nate finds that trouble comes in pairs.
failed to add sprinkle to donut 23948
.sprinkle fail reason: db timeout exceeded
.parse-details
function goes out the window, what can we do to get more context for each parsing?partition
function.step
argument varies how far you reach into the collection for the next chunk.(partition 2 1 (range 1 7))
yields ((1 2) (2 3) (3 4) (4 5) (5 6))
. Each element is paired with it's following element.nil
is returned, so our list becomes a sequence of sprinkle errors or nil
s.nil
s and summarize the sprinkle errors.partition
is lazy. It constructs chunks on demand.(partition 10 1 lines)
or (partition 100 1 lines)
?partition-infinity
?"Related episodes:
Clojure in this episode:
partition
->>
map
, filter
group-by
, frequencies
Code sample from this episode:
(ns devops.week-03
(:require
[devops.week-01 :refer [parse-line]]
[devops.week-02 :refer [process-log]]
))
(defn parse-sprinkle-error
[line-pairs]
(let [[first-line second-line] line-pairs
[_whole donut-id] (some->> first-line :log/message (re-matches #"failed to add sprinkle to donut (\d+)"))
[_whole error] (some->> second-line :log/message (re-matches #"sprinkle fail reason: (.*)"))]
(when (and donut-id error)
(merge first-line
{:kind :sprinkle
:sprinkle/donut-id donut-id
:sprinkle/error error}))))
(defn sprinkle-errors
[lines]
(->> lines
(partition 2 1)
(map parse-sprinkle-error)
(remove nil?)))
(defn sprinkle-errors-by-type
[errors]
(->> errors
(map :sprinkle/error)
(frequencies)))
(comment
(process-log "sample.log" #(->> % (map parse-line) sprinkle-errors sprinkle-errors-by-type))
)
Log file sample:
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | failed to add sprinkle to donut 23948
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | sprinkle fail reason: should never happen
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | failed to add sprinkle to donut 94238
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | sprinkle fail reason: timeout exceeded threshold
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | failed to add sprinkle to donut 24839
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | sprinkle fail reason: too many requests
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | failed to add sprinkle to donut 19238
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | sprinkle fail reason: should never happen
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | failed to add sprinkle to donut 50493
2019-05-14 16:48:55 | process-Poster | INFO | com.donutgram.poster | sprinkle fail reason: unknown state