Skip to content

Commit

Permalink
Report session pool statistics to Riemann.
Browse files Browse the repository at this point in the history
Since these are reported every session refresh, they're a good target
for use as a service heartbeat.

Refs #12.
  • Loading branch information
mhluongo committed Feb 10, 2015
1 parent 0cc6f5e commit 6a9586b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[org.bovinegenius/exploding-fish "0.3.4"]
[overtone/at-at "1.2.0"]
[prismatic/schema "0.3.0"]
[riemann-clojure-client "0.3.2"]
[camel-snake-kebab "0.2.5" :exclusions [org.clojure/clojure]]]
:scm {:name "git"
:url "http://github.com/cardforcoin/shale"}
Expand Down
15 changes: 15 additions & 0 deletions src/shale/riemann.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns shale.riemann
(:require [riemann.client :as riemann]
[shale.configurer :refer [config]]
[taoensso.timbre :as timblre :refer [debug]]))

(def client (when-let [client-config (config :riemann)]
(debug "Configuring Riemann client..." client-config)
(let [client-fn (condp = (:protocol client-config)
:tcp riemann/tcp-client
:udp riemann/udp-client
nil)]
(client-fn (dissoc client-config :protocol)))))

(if client
(debug "Riemann client configured." client))
41 changes: 39 additions & 2 deletions src/shale/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
[schema.core :as s]
[camel-snake-kebab.core :refer :all]
[taoensso.timbre :as timblre :refer [info warn error debug]]
[riemann.client :as riemann]
[shale.nodes :as nodes]
[shale.utils :refer :all]
[shale.redis :refer :all]
[shale.riemann]
[shale.webdriver :refer [new-webdriver resume-webdriver to-async]])
(:import org.openqa.selenium.WebDriverException
org.xbill.DNS.Type))
Expand Down Expand Up @@ -129,7 +131,11 @@
:and (every? #(matches-requirement % session-model) arg)
:or (some #(matches-requirement % session-model) arg))))

(declare view-model view-models resume-webdriver-from-id destroy-session)
(declare view-model
view-models
resume-webdriver-from-id
destroy-session
session-pool-stats)

(defn session-ids []
(with-car* (car/smembers session-set-key)))
Expand Down Expand Up @@ -279,7 +285,6 @@
(defn resume-webdriver-from-id [session-id]
(if-let [model (view-model session-id)]
(apply resume-webdriver

(map-indexed (fn [index e] (if (= index 2) {"browserName" e} e))
[(model :id)
(get-in model [:node :url])
Expand Down Expand Up @@ -316,12 +321,44 @@
(destroy-session session-id)))))
true)

(defn update-all
"Like update-in, but for multiple key paths.
(update-all {:a 1 :b 2 :c 3} [[:a] [:b]] inc) -> {:a 2 :b 3 :c 3}"
[v paths f]
(loop [m v
ps paths]
(if (> (count ps) 0)
(recur (update-in m (first ps) f) (rest ps))
m)))

(defn log-session-pool-stats! []
(prn "SENDING STATS TO RIEMANN!!!")
(when-let [client shale.riemann/client]
(prn "Riemann client" client)
(let [models (view-models nil)]
(riemann/send-event
client
(-> {:service "shale"
:state "ok"
:tags ["session" "pool"]
:metric (count models)
:browsers (frequencies (map :browser-name models))
:session-tags (->> (map :tags models)
(apply concat)
frequencies)
:reserved (->> (map :reserved models)
(filter boolean)
count)}
(update-all [[:browsers] [:session-tags] [:reserved]] pr-str))))))

(defn refresh-sessions [ids]
(with-car*
(debug "Refreshing sessions...")
(car/watch session-set-key)
(doseq [session-id (or ids (session-ids))]
(refresh-session session-id)))
(log-session-pool-stats!)
true)

(defn view-model [session-id]
Expand Down

0 comments on commit 6a9586b

Please sign in to comment.